Skip to content

Commit 830c1d3

Browse files
authored
fix: Fixes unhandled exception caused by null as a valid argument (typed-graphql-builder#41)
Fixes typed-graphql-builder#40 Includes a new test case on test-x.good.ts which uses the Hasura API where nulls can frequently be used as arguments to `_set`. Examples regenerated.
1 parent 4463249 commit 830c1d3

File tree

6 files changed

+31
-5
lines changed

6 files changed

+31
-5
lines changed

examples/api-sw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
213213
case 'boolean':
214214
return JSON.stringify(args)
215215
default:
216+
if (args == null) return 'null'
216217
if (VariableName in (args as any)) {
217218
if (!argVarType) throw new Error('Cannot use variabe as sole unnamed field argument')
218219
const variable = args as Variable<any, any>
@@ -222,7 +223,6 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
222223
}
223224
if (Array.isArray(args))
224225
return '[' + args.map(arg => stringifyArgs(arg, argTypes, argVarType)).join(',') + ']'
225-
if (args == null) return 'null'
226226
const wrapped = (content: string) => (argVarType ? '{' + content + '}' : content)
227227
return wrapped(
228228
Array.from(Object.entries(args))

examples/countries-schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
213213
case 'boolean':
214214
return JSON.stringify(args)
215215
default:
216+
if (args == null) return 'null'
216217
if (VariableName in (args as any)) {
217218
if (!argVarType) throw new Error('Cannot use variabe as sole unnamed field argument')
218219
const variable = args as Variable<any, any>
@@ -222,7 +223,6 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
222223
}
223224
if (Array.isArray(args))
224225
return '[' + args.map(arg => stringifyArgs(arg, argTypes, argVarType)).join(',') + ']'
225-
if (args == null) return 'null'
226226
const wrapped = (content: string) => (argVarType ? '{' + content + '}' : content)
227227
return wrapped(
228228
Array.from(Object.entries(args))

examples/x.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
213213
case 'boolean':
214214
return JSON.stringify(args)
215215
default:
216+
if (args == null) return 'null'
216217
if (VariableName in (args as any)) {
217218
if (!argVarType) throw new Error('Cannot use variabe as sole unnamed field argument')
218219
const variable = args as Variable<any, any>
@@ -222,7 +223,6 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
222223
}
223224
if (Array.isArray(args))
224225
return '[' + args.map(arg => stringifyArgs(arg, argTypes, argVarType)).join(',') + ']'
225-
if (args == null) return 'null'
226226
const wrapped = (content: string) => (argVarType ? '{' + content + '}' : content)
227227
return wrapped(
228228
Array.from(Object.entries(args))

examples/zeus.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
213213
case 'boolean':
214214
return JSON.stringify(args)
215215
default:
216+
if (args == null) return 'null'
216217
if (VariableName in (args as any)) {
217218
if (!argVarType) throw new Error('Cannot use variabe as sole unnamed field argument')
218219
const variable = args as Variable<any, any>
@@ -222,7 +223,6 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
222223
}
223224
if (Array.isArray(args))
224225
return '[' + args.map(arg => stringifyArgs(arg, argTypes, argVarType)).join(',') + ']'
225-
if (args == null) return 'null'
226226
const wrapped = (content: string) => (argVarType ? '{' + content + '}' : content)
227227
return wrapped(
228228
Array.from(Object.entries(args))

src/preamble.src.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
216216
case 'boolean':
217217
return JSON.stringify(args)
218218
default:
219+
if (args == null) return 'null'
219220
if (VariableName in (args as any)) {
220221
if (!argVarType) throw new Error('Cannot use variabe as sole unnamed field argument')
221222
const variable = args as Variable<any, any>
@@ -225,7 +226,6 @@ function fieldToQuery(prefix: string, field: $Field<any, any, any>) {
225226
}
226227
if (Array.isArray(args))
227228
return '[' + args.map(arg => stringifyArgs(arg, argTypes, argVarType)).join(',') + ']'
228-
if (args == null) return 'null'
229229
const wrapped = (content: string) => (argVarType ? '{' + content + '}' : content)
230230
return wrapped(
231231
Array.from(Object.entries(args))

test/examples/test-x.good.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ let orderByTestString = `query ($myvar: order_by!, $optional: order_by) {
7474
}
7575
}`
7676

77+
const nullableArgument = mutation(m => [
78+
m.updateBooking(
79+
{
80+
pk_columns: { id: $$('id') },
81+
_set: {
82+
bookedAt: null,
83+
},
84+
},
85+
r => [r.id]
86+
),
87+
])
88+
89+
const nullableArgumentString = `mutation ($id: uuid!) {
90+
updateBooking(pk_columns: {id: $id}, _set: {bookedAt: null}) {
91+
id
92+
}
93+
}`
94+
7795
export default [
7896
verify({
7997
query: orderByTest,
@@ -96,4 +114,12 @@ export default [
96114
variables: { bc: { name: 'hello' } },
97115
schemaPath: 'x.graphql',
98116
}),
117+
verify({
118+
query: nullableArgument,
119+
variables: {
120+
id: 'abc',
121+
},
122+
schemaPath: 'x.graphql',
123+
string: nullableArgumentString,
124+
}),
99125
]

0 commit comments

Comments
 (0)