Skip to content

Commit

Permalink
fix: support optional GraphQL arguments in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSemenov committed Jun 4, 2021
1 parent c834886 commit 735fc9d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 15 additions & 15 deletions src/tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
65 changes: 65 additions & 0 deletions tap-snapshots/src/tests/main.test.ts.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 735fc9d

Please sign in to comment.