Skip to content

Commit

Permalink
Allow for searching DAOs with id_in where queries (#443)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibetsprague authored Apr 14, 2020
1 parent f5cde9c commit d80b0c3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 52 deletions.
58 changes: 29 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@daostack/client",
"version": "0.2.66",
"version": "0.2.67",
"description": "",
"keywords": [],
"main": "dist/lib/index.js",
Expand Down
44 changes: 24 additions & 20 deletions src/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,20 @@ export interface IDAOQueryOptions extends ICommonQueryOptions {
}
}

export const DAOFieldsFragment = gql`
fragment DAOFields on DAO {
id
name
nativeReputation { id, totalSupply }
nativeToken { id, name, symbol, totalSupply }
numberOfQueuedProposals
numberOfPreBoostedProposals
numberOfBoostedProposals
register
reputationHoldersCount
}`

export class DAO implements IStateful<IDAOState> {
public static fragments = {
DAOFields: gql`
fragment DAOFields on DAO {
id
name
nativeReputation { id, totalSupply }
nativeToken { id, name, symbol, totalSupply }
numberOfQueuedProposals
numberOfPreBoostedProposals
numberOfBoostedProposals
register
reputationHoldersCount
}`
}

/**
* DAO.search(context, options) searches for DAO entities
Expand Down Expand Up @@ -88,7 +87,13 @@ export class DAO implements IStateful<IDAOState> {
options.where[key] = option.toLowerCase()
}

where += `${key}: "${options.where[key] as string}"\n`
if (Array.isArray(options.where[key])) {
// Support for operators like _in
const values = options.where[key].map((val: number) => '"' + val + '"')
where += `${key}: [${values.join(',')}]\n`
} else {
where += `${key}: "${options.where[key] as string}"\n`
}
}

let query
Expand All @@ -98,14 +103,13 @@ export class DAO implements IStateful<IDAOState> {
...DAOFields
}
}
${DAO.fragments.DAOFields}`
${DAOFieldsFragment}`
} else {
query = gql`query SearchDaoIds {
daos ${createGraphQlQuery(options, where)} {
id
}
}`

}

return context.getObservableList(
Expand All @@ -121,8 +125,8 @@ export class DAO implements IStateful<IDAOState> {
register: r.register,
reputation,
token,
tokenName: r.tokenName,
tokenSymbol: r.tokenSymbol
tokenName: r.nativeToken.name,
tokenSymbol: r.nativeToken.symbol
}, context)
} else {
return new DAO(r.id, context)
Expand Down Expand Up @@ -178,7 +182,7 @@ export class DAO implements IStateful<IDAOState> {
...DAOFields
}
}
${DAO.fragments.DAOFields}
${DAOFieldsFragment}
`

const itemMap = (item: any): IDAOState => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { Arc, IContractInfo } from './arc'
export { DAO, IDAOState, IDAOStaticState, IDAOQueryOptions } from './dao'
export { DAO, DAOFieldsFragment, IDAOState, IDAOStaticState, IDAOQueryOptions } from './dao'
export { IGenesisProtocolParams } from './genesisProtocol'
export { createApolloClient } from './graphnode'
export { Event, IEventState, IEventStaticState, IEventQueryOptions } from './event'
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export function createGraphQlQuery(options: ICommonQueryOptions, where: string =
if (where) {
queryString += `where: {
${where}
}`
}\n`
}
if (options.first) {
queryString += `first: ${options.first}\n`
Expand Down
5 changes: 5 additions & 0 deletions test/dao.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ describe('DAO', () => {
expect(result.length).toEqual(0)
result = await DAO.search(arc, {where: { register: 'registered'}}).pipe(first()).toPromise()
expect(result.length).toBeGreaterThan(0)

// test _in queries
const dao = await getTestDAO()
result = await DAO.search(arc, {where: { id_in: [dao.id] }}).pipe(first()).toPromise()
expect(result.length).toBeGreaterThan(0)
})

it('fetchAllData in DAO.search works', async () => {
Expand Down

0 comments on commit d80b0c3

Please sign in to comment.