From 735fc9d9308504fd43a76006af4488e3f6651c13 Mon Sep 17 00:00:00 2001 From: Ilya Semenov Date: Fri, 4 Jun 2021 18:19:19 +0700 Subject: [PATCH] fix: support optional GraphQL arguments in filter --- src/filter.ts | 4 ++ src/tests/main.test.ts | 30 ++++----- tap-snapshots/src/tests/main.test.ts.test.cjs | 65 +++++++++++++++++++ 3 files changed, 84 insertions(+), 15 deletions(-) diff --git a/src/filter.ts b/src/filter.ts index 3f3e6dc..f619391 100644 --- a/src/filter.ts +++ b/src/filter.ts @@ -31,6 +31,10 @@ export function apply_filter({ const ThisModel = query.modelClass() const table_name = ThisModel.tableName for (const [field, value] of Object.entries(filter_obj)) { + if (value === undefined) { + // Support optional GraphQL arguments in filter + continue + } if (ThisModel.modifiers?.[field]) { // Call modifier query.modify(field, value) diff --git a/src/tests/main.test.ts b/src/tests/main.test.ts index ef984eb..4cde448 100644 --- a/src/tests/main.test.ts +++ b/src/tests/main.test.ts @@ -260,25 +260,25 @@ tap.test("Main", async (tap) => { tap.test("root filter", async (tap) => { tap.test("by field", async (tap) => { - tap.matchSnapshot( - await client.request( - gql` - { - posts(filter: { author_id: 2 }, take: 10) { - nodes { - id - text - author { - id - name - } - } + const query = gql` + query($author_id: Int) { + posts(filter: { author_id: $author_id }, take: 10) { + nodes { + id + text + author { + id + name } } - `, - ), + } + } + ` + tap.matchSnapshot( + await client.request(query, { author_id: 2 }), "author_id: 2", ) + tap.matchSnapshot(await client.request(query), "author_id not defined") }) tap.test("__in", async (tap) => { tap.matchSnapshot( diff --git a/tap-snapshots/src/tests/main.test.ts.test.cjs b/tap-snapshots/src/tests/main.test.ts.test.cjs index ad0109a..ac41e3c 100644 --- a/tap-snapshots/src/tests/main.test.ts.test.cjs +++ b/tap-snapshots/src/tests/main.test.ts.test.cjs @@ -231,6 +231,71 @@ Object { } ` +exports[`src/tests/main.test.ts TAP Main root filter by field > author_id not defined 1`] = ` +Object { + "posts": Object { + "nodes": Array [ + Object { + "author": Object { + "id": "1", + "name": "John", + }, + "id": "7", + "text": "This is a draft", + }, + Object { + "author": Object { + "id": "1", + "name": "John", + }, + "id": "6", + "text": "COVID vs Flu?", + }, + Object { + "author": Object { + "id": "2", + "name": "Mary", + }, + "id": "5", + "text": "More good news!", + }, + Object { + "author": Object { + "id": "2", + "name": "Mary", + }, + "id": "4", + "text": "Good news from China.", + }, + Object { + "author": Object { + "id": "2", + "name": "Mary", + }, + "id": "3", + "text": "Latest COVID figures.", + }, + Object { + "author": Object { + "id": "1", + "name": "John", + }, + "id": "2", + "text": "Is communism dead yet?", + }, + Object { + "author": Object { + "id": "1", + "name": "John", + }, + "id": "1", + "text": "Oil price rising.", + }, + ], + }, +} +` + exports[`src/tests/main.test.ts TAP Main root filter by field > author_id: 2 1`] = ` Object { "posts": Object {