Skip to content

Commit

Permalink
fix(ts-client): order independent input object defs
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Apr 14, 2024
1 parent 118d1d7 commit a71c9f8
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 4 deletions.
6 changes: 4 additions & 2 deletions src/Schema/Input/Input.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import type { MaybeThunk } from '../core/helpers.js'
import type { Any } from './typeGroups.js'

export * from './typeGroups.js'
export * from './types/InputObject.js'
export * from './types/List.js'
export * from './types/Nullable.js'

export const field = <$Type extends Any>(type: $Type): Field<$Type> => {
export const field = <$Type extends Any>(type: MaybeThunk<$Type>): Field<$Type> => {
return {
type: type,
// Thunks do not exist at the type level
type: type as any, // eslint-disable-line
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,16 @@ describe(`custom scalar`, () => {
dateArgInputObject: { $: { input: { idRequired: '', dateRequired: new Date(0), date: new Date(1) } } },
})
})
test(`nested input object field`, async () => {
const client = clientExpected((doc) => {
expect(doc.InputObjectNested.$.input.InputObject.dateRequired).toEqual(new Date(0).getTime())
expect(doc.InputObjectNested.$.input.InputObject.date).toEqual(new Date(1).getTime())
})
await client.query({
InputObjectNested: {
$: { input: { InputObject: { idRequired: '', dateRequired: new Date(0), date: new Date(1) } } },
},
})
})
})
})
3 changes: 2 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ const encodeCustomScalarsArgs = (indexArgs: Args<any>, valueArgs: SSValue.Args2)
)
}

const encodeCustomScalarsArgValue = (indexArg: Schema.Input.Any, argValue: null | SSValue.Arg): any => {
const encodeCustomScalarsArgValue = (indexArgMaybeThunk: Schema.Input.Any, argValue: null | SSValue.Arg): any => {
if (argValue === null) return null // todo could check if index agrees is nullable.
const indexArg = readMaybeThunk(indexArgMaybeThunk)
if (indexArg.kind === `nullable`) {
return encodeCustomScalarsArgValue(indexArg.type, argValue)
}
Expand Down
15 changes: 15 additions & 0 deletions src/generator/__snapshots__/files.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export namespace Root {
input: $.Input.Nullable<InputObject.InputObject>
}>
>
InputObjectNested: $.Field<
$.Output.Nullable<$Scalar.ID>,
$.Args<{
input: $.Input.Nullable<InputObject.InputObjectNested>
}>
>
id: $.Field<$.Output.Nullable<$Scalar.ID>, null>
idNonNull: $.Field<$Scalar.ID, null>
string: $.Field<$.Output.Nullable<$Scalar.String>, null>
Expand Down Expand Up @@ -160,6 +166,10 @@ export namespace Enum {
// ------------------------------------------------------------ //
export namespace InputObject {
export type InputObjectNested = $.InputObject<'InputObjectNested', {
InputObject: $.Input.Nullable<InputObject.InputObject>
}>
export type InputObject = $.InputObject<'InputObject', {
id: $.Input.Nullable<$Scalar.ID>
idRequired: $Scalar.ID
Expand Down Expand Up @@ -284,6 +294,10 @@ import * as $Scalar from './Scalar.js'
export const ABCEnum = $.Enum(\`ABCEnum\`, [\`A\`, \`B\`, \`C\`])
export const InputObjectNested = $.InputObject(\`InputObjectNested\`, {
InputObject: $.Input.field(() => $.Input.Nullable(InputObject)),
})
export const InputObject = $.InputObject(\`InputObject\`, {
id: $.Input.field($.Input.Nullable($Scalar.ID)),
idRequired: $.Input.field($Scalar.ID),
Expand Down Expand Up @@ -400,6 +414,7 @@ export const Query = $.Object$(\`Query\`, {
),
dateArgNonNullListNonNull: $.field($.Output.Nullable($Scalar.Date), $.Args({ date: $.Input.List($Scalar.Date) })),
dateArgInputObject: $.field($.Output.Nullable($Scalar.Date), $.Args({ input: $.Input.Nullable(InputObject) })),
InputObjectNested: $.field($.Output.Nullable($Scalar.ID), $.Args({ input: $.Input.Nullable(InputObjectNested) })),
id: $.field($.Output.Nullable($Scalar.ID)),
idNonNull: $.field($Scalar.ID),
string: $.field($.Output.Nullable($Scalar.String)),
Expand Down
2 changes: 1 addition & 1 deletion src/generator/code/schemaRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const inputObject = (config: Config, type: GraphQLInputObjectType) => {

const inputField = (config: Config, field: GraphQLInputField): string => {
const type = buildType(`input`, config, field.type)
return `$.Input.field(${type})`
return `$.Input.field(${isInputObjectType(field.type) ? `() => ${type}` : type})`
}

const outputField = (config: Config, field: AnyGraphQLOutputField): string => {
Expand Down
10 changes: 10 additions & 0 deletions tests/ts/_/schema/generated/SchemaBuildtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export namespace Root {
input: $.Input.Nullable<InputObject.InputObject>
}>
>
InputObjectNested: $.Field<
$.Output.Nullable<$Scalar.ID>,
$.Args<{
input: $.Input.Nullable<InputObject.InputObjectNested>
}>
>
id: $.Field<$.Output.Nullable<$Scalar.ID>, null>
idNonNull: $.Field<$Scalar.ID, null>
string: $.Field<$.Output.Nullable<$Scalar.String>, null>
Expand Down Expand Up @@ -157,6 +163,10 @@ export namespace Enum {
// ------------------------------------------------------------ //

export namespace InputObject {
export type InputObjectNested = $.InputObject<'InputObjectNested', {
InputObject: $.Input.Nullable<InputObject.InputObject>
}>

export type InputObject = $.InputObject<'InputObject', {
id: $.Input.Nullable<$Scalar.ID>
idRequired: $Scalar.ID
Expand Down
5 changes: 5 additions & 0 deletions tests/ts/_/schema/generated/SchemaRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import * as $Scalar from './Scalar.js'

export const ABCEnum = $.Enum(`ABCEnum`, [`A`, `B`, `C`])

export const InputObjectNested = $.InputObject(`InputObjectNested`, {
InputObject: $.Input.field(() => $.Input.Nullable(InputObject)),
})

export const InputObject = $.InputObject(`InputObject`, {
id: $.Input.field($.Input.Nullable($Scalar.ID)),
idRequired: $.Input.field($Scalar.ID),
Expand Down Expand Up @@ -119,6 +123,7 @@ export const Query = $.Object$(`Query`, {
),
dateArgNonNullListNonNull: $.field($.Output.Nullable($Scalar.Date), $.Args({ date: $.Input.List($Scalar.Date) })),
dateArgInputObject: $.field($.Output.Nullable($Scalar.Date), $.Args({ input: $.Input.Nullable(InputObject) })),
InputObjectNested: $.field($.Output.Nullable($Scalar.ID), $.Args({ input: $.Input.Nullable(InputObjectNested) })),
id: $.field($.Output.Nullable($Scalar.ID)),
idNonNull: $.field($Scalar.ID),
string: $.field($.Output.Nullable($Scalar.String)),
Expand Down
15 changes: 15 additions & 0 deletions tests/ts/_/schema/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
scalar Date

type Query {
# Custom Scalar
date: Date
dateNonNull: Date!
dateList: [Date]
Expand All @@ -14,6 +15,11 @@ type Query {
dateArgNonNullList(date: [Date]!): Date
dateArgNonNullListNonNull(date: [Date!]!): Date
dateArgInputObject(input: InputObject): Date
# Input Object
# Note: It is important that the type `InputObjectNested` is defined before `InputObject` in the generated runtime schema.
# This is to force the case of needing a thunk, to make sure our tests for it are actually testing the case.
InputObjectNested(input: InputObjectNested): ID
# Scalar
id: ID
idNonNull: ID!
string: String
Expand All @@ -24,24 +30,29 @@ type Query {
stringWithListArgRequired(ints:[Int]!): String
stringWithArgInputObject(input:InputObject): String
stringWithArgInputObjectRequired(input:InputObject!): String
# List Scalar
listListIntNonNull: [[Int!]!]!
listListInt: [[Int]]
listInt: [Int]
listIntNonNull: [Int!]!
# Object
object: Object1
objectNonNull: Object1!
objectNested: ObjectNested
objectWithArgs(string:String, int:Int, float:Float, boolean:Boolean, id:ID): Object1
fooBarUnion: FooBarUnion
# Object List
objectList: [Object1]
objectListNonNull: [Object1!]!
"""
Query enum field documentation.
"""
abcEnum: ABCEnum
lowerCaseUnion: lowerCaseUnion
# Interface
interface: Interface
interfaceNonNull: Interface!
# Union
unionFooBar: FooBarUnion
unionObject: ObjectUnion
unionFooBarNonNull: FooBarUnion!
Expand All @@ -62,6 +73,10 @@ type DateObject2 {
date2: Date
}

input InputObjectNested {
InputObject: InputObject
}

input InputObject {
id: ID
idRequired: ID!
Expand Down

0 comments on commit a71c9f8

Please sign in to comment.