From f28d2510601732b969c36ebb54494a9207e11165 Mon Sep 17 00:00:00 2001 From: less Date: Fri, 10 Mar 2023 14:58:26 +0700 Subject: [PATCH] feat: support filter with "app" and "reason" fields --- src/graphql/helpers.ts | 24 +++++++++++++++++------- src/graphql/operations/proposals.ts | 1 + src/graphql/operations/votes.ts | 2 ++ src/graphql/schema.gql | 12 ++++++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/graphql/helpers.ts b/src/graphql/helpers.ts index 95470772..db1d9244 100644 --- a/src/graphql/helpers.ts +++ b/src/graphql/helpers.ts @@ -81,15 +81,29 @@ export function buildWhereQuery(fields, alias, where) { let query: any = ''; const params: any[] = []; Object.entries(fields).forEach(([field, type]) => { - if (where[field]) { + if (where[field] !== undefined) { query += `AND ${alias}.${field} = ? `; params.push(where[field]); } + + const fieldNot = where[`${field}_not`]; + if (fieldNot !== undefined) { + query += `AND ${alias}.${field} != ? `; + params.push(fieldNot); + } + const fieldIn = where[`${field}_in`] || []; if (fieldIn.length > 0) { query += `AND ${alias}.${field} IN (?) `; params.push(fieldIn); } + + const fieldNotIn = where[`${field}_not_in`] || []; + if (fieldNotIn.length > 0) { + query += `AND ${alias}.${field} NOT IN (?) `; + params.push(fieldNotIn); + } + if (type === 'number') { const fieldGt = where[`${field}_gt`]; const fieldGte = where[`${field}_gte`]; @@ -167,14 +181,10 @@ function checkRelatedSpacesNesting(requestedFields): void { function needsRelatedSpacesData(requestedFields): boolean { // id's of parent/children are already included in the result from fetchSpaces // an additional query is only needed if other fields are requested - if ( + return !( !(requestedFields.parent && Object.keys(requestedFields.parent).some(key => key !== 'id')) && !(requestedFields.children && Object.keys(requestedFields.children).some(key => key !== 'id')) - ) { - return false; - } - - return true; + ); } function mapRelatedSpacesToSpaces(spaces, relatedSpaces) { diff --git a/src/graphql/operations/proposals.ts b/src/graphql/operations/proposals.ts index 5eaf888b..9fe3e95d 100644 --- a/src/graphql/operations/proposals.ts +++ b/src/graphql/operations/proposals.ts @@ -14,6 +14,7 @@ export default async function (parent, args) { author: 'string', network: 'string', created: 'number', + app: 'string', start: 'number', end: 'number', type: 'string', diff --git a/src/graphql/operations/votes.ts b/src/graphql/operations/votes.ts index 2ad7b55b..1973b686 100644 --- a/src/graphql/operations/votes.ts +++ b/src/graphql/operations/votes.ts @@ -16,6 +16,8 @@ async function query(parent, args, context?, info?) { space: 'string', voter: 'string', proposal: 'string', + reason: 'string', + app: 'string', created: 'number', vp: 'number', vp_state: 'string' diff --git a/src/graphql/schema.gql b/src/graphql/schema.gql index 7877009a..648990ab 100644 --- a/src/graphql/schema.gql +++ b/src/graphql/schema.gql @@ -150,6 +150,10 @@ input ProposalWhere { validation: String type: String type_in: [String] + app: String + app_not: String + app_in: [String] + app_not_in: [String] created: Int created_in: [Int] created_gt: Int @@ -184,6 +188,14 @@ input VoteWhere { voter_in: [String] proposal: String proposal_in: [String] + reason: String + reason_not: String + reason_in: [String] + reason_not_in: [String] + app: String + app_not: String + app_in: [String] + app_not_in: [String] created: Int created_in: [Int] created_gt: Int