Skip to content

Commit

Permalink
feat: support filter with "app" and "reason" fields
Browse files Browse the repository at this point in the history
  • Loading branch information
bonustrack committed Mar 10, 2023
1 parent f0cef9a commit f28d251
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/graphql/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`];
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions src/graphql/operations/proposals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default async function (parent, args) {
author: 'string',
network: 'string',
created: 'number',
app: 'string',
start: 'number',
end: 'number',
type: 'string',
Expand Down
2 changes: 2 additions & 0 deletions src/graphql/operations/votes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 12 additions & 0 deletions src/graphql/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f28d251

Please sign in to comment.