Skip to content

Commit

Permalink
fix: return empty results on empty array filters (#765)
Browse files Browse the repository at this point in the history
* fix: empty array in graphql filters should trigger an error

* chore: convert all throw error to `PublicError`

* Revert "fix: empty array in graphql filters should trigger an error"

This reverts commit 0343bb2.

* fix: move the empty array check to `checkLimits`

* fix: return an empty response on empty array filter
  • Loading branch information
wa0x6e committed Nov 29, 2023
1 parent 1db7b01 commit 0a9c2d5
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/graphql/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ export function checkLimits(args: any = {}, type) {
const skipLimitReached = key === 'skip' && args[key] > limit;
const whereLimitReached = key.endsWith('_in') ? where[key]?.length > limit : where[key] > limit;
if (firstLimitReached || skipLimitReached || whereLimitReached)
throw new Error(`The \`${key}\` argument must not be greater than ${limit}`);
throw new PublicError(`The \`${key}\` argument must not be greater than ${limit}`);

if (['first', 'skip'].includes(key) && args[key] < 0) {
throw new Error(`The \`${key}\` argument must be positive`);
throw new PublicError(`The \`${key}\` argument must be positive`);
}
}

return true;
}

Expand Down Expand Up @@ -114,16 +115,24 @@ export function buildWhereQuery(fields, alias, where) {
params.push(fieldNot);
}

const fieldIn = where[`${field}_in`] || [];
if (fieldIn.length > 0) {
query += `AND ${alias}.${field} IN (?) `;
params.push(fieldIn);
const fieldIn = where[`${field}_in`];
if (Array.isArray(fieldIn)) {
if (fieldIn.length > 0) {
query += `AND ${alias}.${field} IN (?) `;
params.push(fieldIn);
} else {
query += 'AND 1=0 ';
}
}

const fieldNotIn = where[`${field}_not_in`] || [];
if (fieldNotIn.length > 0) {
query += `AND ${alias}.${field} NOT IN (?) `;
params.push(fieldNotIn);
const fieldNotIn = where[`${field}_not_in`];
if (Array.isArray(fieldNotIn)) {
if (fieldNotIn.length > 0) {
query += `AND ${alias}.${field} NOT IN (?) `;
params.push(fieldNotIn);
} else {
query += 'AND 1=0 ';
}
}

if (type === 'number') {
Expand Down

0 comments on commit 0a9c2d5

Please sign in to comment.