From d80b0c36c3ced43d72b4f120af5b7c7494637d57 Mon Sep 17 00:00:00 2001 From: Tibet Sprague Date: Tue, 14 Apr 2020 09:14:24 -0700 Subject: [PATCH] Allow for searching DAOs with id_in where queries (#443) --- package-lock.json | 58 +++++++++++++++++++++++------------------------ package.json | 2 +- src/dao.ts | 44 +++++++++++++++++++---------------- src/index.ts | 2 +- src/utils.ts | 2 +- test/dao.spec.ts | 5 ++++ 6 files changed, 61 insertions(+), 52 deletions(-) diff --git a/package-lock.json b/package-lock.json index f6f94642..68783013 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "@daostack/client", - "version": "0.2.66", + "version": "0.2.67", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1821,7 +1821,7 @@ }, "array-equal": { "version": "1.0.0", - "resolved": "https://registry.npmjs.org/array-equal/-/array-equal-1.0.0.tgz", + "resolved": "http://registry.npmjs.org/array-equal/-/array-equal-1.0.0.tgz", "integrity": "sha1-jCpe8kcv2ep0KwTHenUJO6J1fJM=", "dev": true }, @@ -2208,7 +2208,7 @@ }, "browserify-aes": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/browserify-aes/-/browserify-aes-1.2.0.tgz", "integrity": "sha512-+7CHXqGuspUn/Sl5aO7Ea0xWGAtETPXNSAjHo48JfLdPWcMng33Xe4znFvQweqc/uzk5zSOI3H52CYnjCfb5hA==", "requires": { "buffer-xor": "^1.0.3", @@ -2242,7 +2242,7 @@ }, "browserify-rsa": { "version": "4.0.1", - "resolved": "https://registry.npmjs.org/browserify-rsa/-/browserify-rsa-4.0.1.tgz", + "resolved": "http://registry.npmjs.org/browserify-rsa/-/browserify-rsa-4.0.1.tgz", "integrity": "sha1-IeCr+vbyApzy+vsTNWenAdQTVSQ=", "requires": { "bn.js": "^4.1.0", @@ -2683,7 +2683,7 @@ }, "strip-ansi": { "version": "3.0.1", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "resolved": "http://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", "dev": true, "requires": { @@ -2975,7 +2975,7 @@ }, "create-hash": { "version": "1.2.0", - "resolved": "https://registry.npmjs.org/create-hash/-/create-hash-1.2.0.tgz", + "resolved": "http://registry.npmjs.org/create-hash/-/create-hash-1.2.0.tgz", "integrity": "sha512-z00bCGNHDG8mHAkP7CtT1qVu+bFQUPjYq/4Iv3C3kWjTFV10zIjfSoeqXo9Asws8gwSHDGj/hl2u4OGIjapeCg==", "requires": { "cipher-base": "^1.0.1", @@ -2987,7 +2987,7 @@ }, "create-hmac": { "version": "1.1.7", - "resolved": "https://registry.npmjs.org/create-hmac/-/create-hmac-1.1.7.tgz", + "resolved": "http://registry.npmjs.org/create-hmac/-/create-hmac-1.1.7.tgz", "integrity": "sha512-MJG9liiZ+ogc4TzUwuvbER1JRdgvUFSB5+VR/g5h82fGaIRWMWddtKBHi7/sVhfjQZ6SehlyhvQYrcYkaUIpLg==", "requires": { "cipher-base": "^1.0.3", @@ -3199,12 +3199,12 @@ "dependencies": { "file-type": { "version": "3.9.0", - "resolved": "https://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz", + "resolved": "http://registry.npmjs.org/file-type/-/file-type-3.9.0.tgz", "integrity": "sha1-JXoHg4TR24CHvESdEH1SpSZyuek=" }, "get-stream": { "version": "2.3.1", - "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-2.3.1.tgz", + "resolved": "http://registry.npmjs.org/get-stream/-/get-stream-2.3.1.tgz", "integrity": "sha1-Xzj5PzRgCWZu4BUKBUFn+Rvdld4=", "requires": { "object-assign": "^4.0.1", @@ -3313,7 +3313,7 @@ }, "diffie-hellman": { "version": "5.0.3", - "resolved": "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", + "resolved": "http://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz", "integrity": "sha512-kqag/Nl+f3GwyK25fhUMYj81BUOrZ9IuJsjIcDE5icNM9FJHAVm3VcUDxdLPoQtTuUylWm6ZIknYJwwaPxsUzg==", "requires": { "bn.js": "^4.1.0", @@ -5696,7 +5696,7 @@ }, "strip-ansi": { "version": "3.0.1", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "resolved": "http://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", "dev": true, "requires": { @@ -6515,7 +6515,7 @@ }, "is-obj": { "version": "1.0.1", - "resolved": "https://registry.npmjs.org/is-obj/-/is-obj-1.0.1.tgz", + "resolved": "http://registry.npmjs.org/is-obj/-/is-obj-1.0.1.tgz", "integrity": "sha1-PkcprB9f3gJc19g6iW2rn09n2w8=", "dev": true }, @@ -7903,7 +7903,7 @@ }, "chalk": { "version": "1.1.3", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "resolved": "http://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", "dev": true, "requires": { @@ -7935,7 +7935,7 @@ }, "strip-ansi": { "version": "3.0.1", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "resolved": "http://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", "dev": true, "requires": { @@ -8213,7 +8213,7 @@ }, "media-typer": { "version": "0.3.0", - "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", + "resolved": "http://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, "mem": { @@ -8555,7 +8555,7 @@ }, "ncp": { "version": "1.0.1", - "resolved": "https://registry.npmjs.org/ncp/-/ncp-1.0.1.tgz", + "resolved": "http://registry.npmjs.org/ncp/-/ncp-1.0.1.tgz", "integrity": "sha1-0VNn5cuHQyuhF9K/gP30Wuz7QkY=", "dev": true }, @@ -8621,7 +8621,7 @@ }, "semver": { "version": "5.3.0", - "resolved": "https://registry.npmjs.org/semver/-/semver-5.3.0.tgz", + "resolved": "http://registry.npmjs.org/semver/-/semver-5.3.0.tgz", "integrity": "sha1-myzl094C0XxgEq0yaqa00M9U+U8=", "dev": true }, @@ -9000,7 +9000,7 @@ }, "os-homedir": { "version": "1.0.2", - "resolved": "https://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", + "resolved": "http://registry.npmjs.org/os-homedir/-/os-homedir-1.0.2.tgz", "integrity": "sha1-/7xJiDNuDoM94MFox+8VISGqf7M=", "dev": true }, @@ -9017,7 +9017,7 @@ }, "os-tmpdir": { "version": "1.0.2", - "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "resolved": "http://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=", "dev": true }, @@ -9168,7 +9168,7 @@ }, "path-is-absolute": { "version": "1.0.1", - "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "resolved": "http://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", "dev": true }, @@ -9626,7 +9626,7 @@ }, "query-string": { "version": "5.1.1", - "resolved": "https://registry.npmjs.org/query-string/-/query-string-5.1.1.tgz", + "resolved": "http://registry.npmjs.org/query-string/-/query-string-5.1.1.tgz", "integrity": "sha512-gjWOsm2SoGlgLEdAGt7a6slVOk9mGiXmPFMqrEhLQ68rhQuBnpfs3+EmlvqKyxnCo9/PPlF+9MtY02S1aFg+Jw==", "requires": { "decode-uri-component": "^0.2.0", @@ -10343,7 +10343,7 @@ }, "safe-regex": { "version": "1.1.0", - "resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", + "resolved": "http://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", "integrity": "sha1-QKNmnzsHfR6UPURinhV91IAjvy4=", "dev": true, "requires": { @@ -10390,7 +10390,7 @@ }, "scrypt-js": { "version": "2.0.3", - "resolved": "https://registry.npmjs.org/scrypt-js/-/scrypt-js-2.0.3.tgz", + "resolved": "http://registry.npmjs.org/scrypt-js/-/scrypt-js-2.0.3.tgz", "integrity": "sha1-uwBAvgMEPamgEqLOqfyfhSz8h9Q=" }, "scrypt.js": { @@ -10539,7 +10539,7 @@ }, "sha.js": { "version": "2.4.11", - "resolved": "https://registry.npmjs.org/sha.js/-/sha.js-2.4.11.tgz", + "resolved": "http://registry.npmjs.org/sha.js/-/sha.js-2.4.11.tgz", "integrity": "sha512-QMEp5B7cftE7APOjk5Y6xgrbWu+WkLVQwk8JNjZ8nKRciZaByEW6MubieAiToS7+dwvrjGhH8jRXz3MVd0AYqQ==", "requires": { "inherits": "^2.0.1", @@ -10898,7 +10898,7 @@ }, "staged-git-files": { "version": "1.1.1", - "resolved": "https://registry.npmjs.org/staged-git-files/-/staged-git-files-1.1.1.tgz", + "resolved": "http://registry.npmjs.org/staged-git-files/-/staged-git-files-1.1.1.tgz", "integrity": "sha512-H89UNKr1rQJvI1c/PIR3kiAMBV23yvR7LItZiV74HWZwzt7f3YHuujJ9nJZlt58WlFox7XQsOahexwk7nTe69A==", "dev": true }, @@ -11151,7 +11151,7 @@ }, "strip-eof": { "version": "1.0.0", - "resolved": "https://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", + "resolved": "http://registry.npmjs.org/strip-eof/-/strip-eof-1.0.0.tgz", "integrity": "sha1-u0P/VZim6wXYm1n80SnJgzE2Br8=", "dev": true }, @@ -11395,7 +11395,7 @@ }, "through": { "version": "2.3.8", - "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz", "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" }, "time-stamp": { @@ -12671,7 +12671,7 @@ }, "wrap-ansi": { "version": "2.1.0", - "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", + "resolved": "http://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.1.0.tgz", "integrity": "sha1-2Pw9KE3QV5T+hJc8rs3Rz4JP3YU=", "dev": true, "requires": { @@ -12707,7 +12707,7 @@ }, "strip-ansi": { "version": "3.0.1", - "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "resolved": "http://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", "dev": true, "requires": { diff --git a/package.json b/package.json index 00d83c03..7d2b8934 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@daostack/client", - "version": "0.2.66", + "version": "0.2.67", "description": "", "keywords": [], "main": "dist/lib/index.js", diff --git a/src/dao.ts b/src/dao.ts index 9ae845d6..0e512a44 100644 --- a/src/dao.ts +++ b/src/dao.ts @@ -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 { - 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 @@ -88,7 +87,13 @@ export class DAO implements IStateful { 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 @@ -98,14 +103,13 @@ export class DAO implements IStateful { ...DAOFields } } - ${DAO.fragments.DAOFields}` + ${DAOFieldsFragment}` } else { query = gql`query SearchDaoIds { daos ${createGraphQlQuery(options, where)} { id } }` - } return context.getObservableList( @@ -121,8 +125,8 @@ export class DAO implements IStateful { 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) @@ -178,7 +182,7 @@ export class DAO implements IStateful { ...DAOFields } } - ${DAO.fragments.DAOFields} + ${DAOFieldsFragment} ` const itemMap = (item: any): IDAOState => { diff --git a/src/index.ts b/src/index.ts index 01381f9f..8b98437a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' diff --git a/src/utils.ts b/src/utils.ts index 593a5803..06a7f8e2 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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` diff --git a/test/dao.spec.ts b/test/dao.spec.ts index 0e9cf872..1c7e08fe 100644 --- a/test/dao.spec.ts +++ b/test/dao.spec.ts @@ -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 () => {