Skip to content

Commit 310f10c

Browse files
committed
fix(ts-client): order independent input object defs
1 parent 54d2810 commit 310f10c

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

src/generator/__snapshots__/files.test.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export namespace Root {
5959
input: $.Input.Nullable<InputObject.InputObjectNested>
6060
}>
6161
>
62+
InputObjectNestedNonNull: $.Field<
63+
$.Output.Nullable<$Scalar.ID>,
64+
$.Args<{
65+
input: InputObject.InputObjectNested
66+
}>
67+
>
6268
id: $.Field<$.Output.Nullable<$Scalar.ID>, null>
6369
idNonNull: $.Field<$Scalar.ID, null>
6470
string: $.Field<$.Output.Nullable<$Scalar.String>, null>
@@ -170,6 +176,10 @@ export namespace InputObject {
170176
InputObject: $.Input.Nullable<InputObject.InputObject>
171177
}>
172178
179+
export type InputObjectNestedNonNull = $.InputObject<'InputObjectNestedNonNull', {
180+
InputObject: InputObject.InputObject
181+
}>
182+
173183
export type InputObject = $.InputObject<'InputObject', {
174184
id: $.Input.Nullable<$Scalar.ID>
175185
idRequired: $Scalar.ID
@@ -298,6 +308,10 @@ export const InputObjectNested = $.InputObject(\`InputObjectNested\`, {
298308
InputObject: $.Input.field(() => $.Input.Nullable(InputObject)),
299309
})
300310
311+
export const InputObjectNestedNonNull = $.InputObject(\`InputObjectNestedNonNull\`, {
312+
InputObject: $.Input.field(() => InputObject),
313+
})
314+
301315
export const InputObject = $.InputObject(\`InputObject\`, {
302316
id: $.Input.field($.Input.Nullable($Scalar.ID)),
303317
idRequired: $.Input.field($Scalar.ID),
@@ -415,6 +429,7 @@ export const Query = $.Object$(\`Query\`, {
415429
dateArgNonNullListNonNull: $.field($.Output.Nullable($Scalar.Date), $.Args({ date: $.Input.List($Scalar.Date) })),
416430
dateArgInputObject: $.field($.Output.Nullable($Scalar.Date), $.Args({ input: $.Input.Nullable(InputObject) })),
417431
InputObjectNested: $.field($.Output.Nullable($Scalar.ID), $.Args({ input: $.Input.Nullable(InputObjectNested) })),
432+
InputObjectNestedNonNull: $.field($.Output.Nullable($Scalar.ID), $.Args({ input: InputObjectNested })),
418433
id: $.field($.Output.Nullable($Scalar.ID)),
419434
idNonNull: $.field($Scalar.ID),
420435
string: $.field($.Output.Nullable($Scalar.String)),

src/generator/code/schemaRuntime.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
isUnionType,
2020
} from 'graphql'
2121
import type { AnyClass, AnyGraphQLOutputField } from '../../lib/graphql.js'
22-
import { hasMutation, hasQuery, hasSubscription, unwrapToNonNull } from '../../lib/graphql.js'
22+
import { hasMutation, hasQuery, hasSubscription, unwrapToNamed, unwrapToNonNull } from '../../lib/graphql.js'
2323
import type { Config } from './code.js'
2424

2525
export const generateRuntimeSchema = (
@@ -116,10 +116,12 @@ const inputObject = (config: Config, type: GraphQLInputObjectType) => {
116116
})
117117
`
118118
}
119+
unwrapToNamed
119120

120121
const inputField = (config: Config, field: GraphQLInputField): string => {
121122
const type = buildType(`input`, config, field.type)
122-
return `$.Input.field(${isInputObjectType(field.type) ? `() => ${type}` : type})`
123+
const isNeedThunk = isInputObjectType(unwrapToNamed(field.type))
124+
return `$.Input.field(${isNeedThunk ? `() => ${type}` : type})`
123125
}
124126

125127
const outputField = (config: Config, field: AnyGraphQLOutputField): string => {

tests/ts/_/schema/generated/SchemaBuildtime.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ export namespace Root {
5656
input: $.Input.Nullable<InputObject.InputObjectNested>
5757
}>
5858
>
59+
InputObjectNestedNonNull: $.Field<
60+
$.Output.Nullable<$Scalar.ID>,
61+
$.Args<{
62+
input: InputObject.InputObjectNested
63+
}>
64+
>
5965
id: $.Field<$.Output.Nullable<$Scalar.ID>, null>
6066
idNonNull: $.Field<$Scalar.ID, null>
6167
string: $.Field<$.Output.Nullable<$Scalar.String>, null>
@@ -167,6 +173,10 @@ export namespace InputObject {
167173
InputObject: $.Input.Nullable<InputObject.InputObject>
168174
}>
169175

176+
export type InputObjectNestedNonNull = $.InputObject<'InputObjectNestedNonNull', {
177+
InputObject: InputObject.InputObject
178+
}>
179+
170180
export type InputObject = $.InputObject<'InputObject', {
171181
id: $.Input.Nullable<$Scalar.ID>
172182
idRequired: $Scalar.ID

tests/ts/_/schema/generated/SchemaRuntime.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ export const InputObjectNested = $.InputObject(`InputObjectNested`, {
77
InputObject: $.Input.field(() => $.Input.Nullable(InputObject)),
88
})
99

10+
export const InputObjectNestedNonNull = $.InputObject(`InputObjectNestedNonNull`, {
11+
InputObject: $.Input.field(() => InputObject),
12+
})
13+
1014
export const InputObject = $.InputObject(`InputObject`, {
1115
id: $.Input.field($.Input.Nullable($Scalar.ID)),
1216
idRequired: $.Input.field($Scalar.ID),
@@ -124,6 +128,7 @@ export const Query = $.Object$(`Query`, {
124128
dateArgNonNullListNonNull: $.field($.Output.Nullable($Scalar.Date), $.Args({ date: $.Input.List($Scalar.Date) })),
125129
dateArgInputObject: $.field($.Output.Nullable($Scalar.Date), $.Args({ input: $.Input.Nullable(InputObject) })),
126130
InputObjectNested: $.field($.Output.Nullable($Scalar.ID), $.Args({ input: $.Input.Nullable(InputObjectNested) })),
131+
InputObjectNestedNonNull: $.field($.Output.Nullable($Scalar.ID), $.Args({ input: InputObjectNested })),
127132
id: $.field($.Output.Nullable($Scalar.ID)),
128133
idNonNull: $.field($Scalar.ID),
129134
string: $.field($.Output.Nullable($Scalar.String)),

tests/ts/_/schema/schema.graphql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Query {
2020
# Note: It is important that the type `InputObjectNested` is defined before `InputObject` in the generated runtime schema.
2121
# This is to force the case of needing a thunk, to make sure our tests for it are actually testing the case.
2222
InputObjectNested(input: InputObjectNested): ID
23+
InputObjectNestedNonNull(input: InputObjectNested!): ID
2324
# Scalar
2425
id: ID
2526
idNonNull: ID!
@@ -78,6 +79,10 @@ input InputObjectNested {
7879
InputObject: InputObject
7980
}
8081

82+
input InputObjectNestedNonNull {
83+
InputObject: InputObject!
84+
}
85+
8186
input InputObject {
8287
id: ID
8388
idRequired: ID!

0 commit comments

Comments
 (0)