Skip to content

Commit d80b0c3

Browse files
authored
Allow for searching DAOs with id_in where queries (#443)
1 parent f5cde9c commit d80b0c3

File tree

6 files changed

+61
-52
lines changed

6 files changed

+61
-52
lines changed

package-lock.json

Lines changed: 29 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@daostack/client",
3-
"version": "0.2.66",
3+
"version": "0.2.67",
44
"description": "",
55
"keywords": [],
66
"main": "dist/lib/index.js",

src/dao.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,20 @@ export interface IDAOQueryOptions extends ICommonQueryOptions {
4646
}
4747
}
4848

49+
export const DAOFieldsFragment = gql`
50+
fragment DAOFields on DAO {
51+
id
52+
name
53+
nativeReputation { id, totalSupply }
54+
nativeToken { id, name, symbol, totalSupply }
55+
numberOfQueuedProposals
56+
numberOfPreBoostedProposals
57+
numberOfBoostedProposals
58+
register
59+
reputationHoldersCount
60+
}`
61+
4962
export class DAO implements IStateful<IDAOState> {
50-
public static fragments = {
51-
DAOFields: gql`
52-
fragment DAOFields on DAO {
53-
id
54-
name
55-
nativeReputation { id, totalSupply }
56-
nativeToken { id, name, symbol, totalSupply }
57-
numberOfQueuedProposals
58-
numberOfPreBoostedProposals
59-
numberOfBoostedProposals
60-
register
61-
reputationHoldersCount
62-
}`
63-
}
6463

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

91-
where += `${key}: "${options.where[key] as string}"\n`
90+
if (Array.isArray(options.where[key])) {
91+
// Support for operators like _in
92+
const values = options.where[key].map((val: number) => '"' + val + '"')
93+
where += `${key}: [${values.join(',')}]\n`
94+
} else {
95+
where += `${key}: "${options.where[key] as string}"\n`
96+
}
9297
}
9398

9499
let query
@@ -98,14 +103,13 @@ export class DAO implements IStateful<IDAOState> {
98103
...DAOFields
99104
}
100105
}
101-
${DAO.fragments.DAOFields}`
106+
${DAOFieldsFragment}`
102107
} else {
103108
query = gql`query SearchDaoIds {
104109
daos ${createGraphQlQuery(options, where)} {
105110
id
106111
}
107112
}`
108-
109113
}
110114

111115
return context.getObservableList(
@@ -121,8 +125,8 @@ export class DAO implements IStateful<IDAOState> {
121125
register: r.register,
122126
reputation,
123127
token,
124-
tokenName: r.tokenName,
125-
tokenSymbol: r.tokenSymbol
128+
tokenName: r.nativeToken.name,
129+
tokenSymbol: r.nativeToken.symbol
126130
}, context)
127131
} else {
128132
return new DAO(r.id, context)
@@ -178,7 +182,7 @@ export class DAO implements IStateful<IDAOState> {
178182
...DAOFields
179183
}
180184
}
181-
${DAO.fragments.DAOFields}
185+
${DAOFieldsFragment}
182186
`
183187

184188
const itemMap = (item: any): IDAOState => {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { Arc, IContractInfo } from './arc'
2-
export { DAO, IDAOState, IDAOStaticState, IDAOQueryOptions } from './dao'
2+
export { DAO, DAOFieldsFragment, IDAOState, IDAOStaticState, IDAOQueryOptions } from './dao'
33
export { IGenesisProtocolParams } from './genesisProtocol'
44
export { createApolloClient } from './graphnode'
55
export { Event, IEventState, IEventStaticState, IEventQueryOptions } from './event'

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function createGraphQlQuery(options: ICommonQueryOptions, where: string =
115115
if (where) {
116116
queryString += `where: {
117117
${where}
118-
}`
118+
}\n`
119119
}
120120
if (options.first) {
121121
queryString += `first: ${options.first}\n`

test/dao.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ describe('DAO', () => {
3737
expect(result.length).toEqual(0)
3838
result = await DAO.search(arc, {where: { register: 'registered'}}).pipe(first()).toPromise()
3939
expect(result.length).toBeGreaterThan(0)
40+
41+
// test _in queries
42+
const dao = await getTestDAO()
43+
result = await DAO.search(arc, {where: { id_in: [dao.id] }}).pipe(first()).toPromise()
44+
expect(result.length).toBeGreaterThan(0)
4045
})
4146

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

0 commit comments

Comments
 (0)