Skip to content

Commit 8a6739d

Browse files
committed
Allow null and undefined as filter value
1 parent e18f7c0 commit 8a6739d

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

.changeset/empty-jobs-yell.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"objection-graphql-resolver": patch
3+
"orchid-graphql": patch
4+
---
5+
6+
Allow null and undefined as filter value.

packages/base/src/filter/filter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export type FilterConfig = boolean
66

77
export type FieldFilterScalarValue = null | string | number | boolean
88
export type FieldFilterValue = FieldFilterScalarValue | Exclude<FieldFilterScalarValue, null>[]
9-
export type FilterValue = { [property: string]: FieldFilterValue }
9+
export type FilterValue = { [property: string]: FieldFilterValue } | null | undefined
1010

1111
export type FilterQueryOptions<Orm extends OrmAdapter, Context = unknown> = {
1212
modifiers?: FilterModifiers<Orm, Context>

packages/objection/tests/filter/filter.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { QueryBuilder } from "objection"
22
import { Model, ref } from "objection"
3-
import type { FilterModifiers } from "objection-graphql-resolver"
3+
import type { FilterModifiers, FilterValue } from "objection-graphql-resolver"
44
import { filterQuery } from "objection-graphql-resolver"
55
import { expect, test } from "vitest"
66

@@ -220,3 +220,33 @@ test("filter by parametrized modifier (search)", async () => {
220220
filterQuery(PostModel.query().select("text"), { search: "news" }, { modifiers }),
221221
).resolves.toMatchInlineSnapshot(`[]`)
222222
})
223+
224+
test("no filter", async () => {
225+
await expect(
226+
filterQuery(PostModel.query().count(), null),
227+
).resolves.toMatchInlineSnapshot(`
228+
[
229+
{
230+
"count(*)": 6,
231+
},
232+
]
233+
`)
234+
await expect(
235+
filterQuery(PostModel.query().count(), undefined),
236+
).resolves.toMatchInlineSnapshot(`
237+
[
238+
{
239+
"count(*)": 6,
240+
},
241+
]
242+
`)
243+
})
244+
245+
test("invalid filter type", async () => {
246+
expect(
247+
() => filterQuery(PostModel.query().count(), true as unknown as FilterValue),
248+
).toThrowError("Invalid filter")
249+
expect(
250+
() => filterQuery(PostModel.query().count(), "published" as unknown as FilterValue),
251+
).toThrowError("Invalid filter")
252+
})

packages/orchid/tests/filter/filter.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { FilterModifiers } from "orchid-graphql"
1+
import type { FilterModifiers, FilterValue } from "orchid-graphql"
22
import { filterQuery } from "orchid-graphql"
33
import { expect, test } from "vitest"
44

@@ -175,3 +175,21 @@ test("filter by parametrized modifier (search)", async () => {
175175
]
176176
`)
177177
})
178+
179+
test("no filter", async () => {
180+
await expect(
181+
filterQuery(db.post.count(), null),
182+
).resolves.toMatchInlineSnapshot(`6`)
183+
await expect(
184+
filterQuery(db.post.count(), undefined),
185+
).resolves.toMatchInlineSnapshot(`6`)
186+
})
187+
188+
test("invalid filter type", async () => {
189+
expect(
190+
() => filterQuery(db.post.count(), true as unknown as FilterValue),
191+
).toThrowError("Invalid filter")
192+
expect(
193+
() => filterQuery(db.post.count(), "published" as unknown as FilterValue),
194+
).toThrowError("Invalid filter")
195+
})

0 commit comments

Comments
 (0)