From f7bcc11f2f104dd6e4dcfcdcfba3738a024206a4 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 23 Feb 2024 19:47:00 +0000 Subject: [PATCH 1/3] Add missing check for defaultValues in input object fields --- src/variables.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/variables.ts b/src/variables.ts index 86e0c82a..6ba07bc7 100644 --- a/src/variables.ts +++ b/src/variables.ts @@ -12,9 +12,9 @@ type getInputObjectTypeRec< Rest, Introspection, (InputField extends { name: any; type: any } - ? InputField['type'] extends { kind: 'NON_NULL' } + ? InputField extends { defaultValue: undefined | null; type: { kind: 'NON_NULL' } } ? { [Name in InputField['name']]: unwrapType } - : { [Name in InputField['name']]?: unwrapType } + : { [Name in InputField['name']]?: unwrapType | null } : {}) & InputObject > From 1f2b65321cfe01844045950ecfdf1755e5dccae8 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 23 Feb 2024 19:53:24 +0000 Subject: [PATCH 2/3] Add tests --- src/__tests__/fixtures/simpleIntrospection.ts | 23 +++++++++++++++++++ src/__tests__/fixtures/simpleSchema.ts | 20 ++++++++++++++++ src/__tests__/variables.test-d.ts | 17 ++++++++++++++ 3 files changed, 60 insertions(+) diff --git a/src/__tests__/fixtures/simpleIntrospection.ts b/src/__tests__/fixtures/simpleIntrospection.ts index 41dd613b..6a5cb494 100644 --- a/src/__tests__/fixtures/simpleIntrospection.ts +++ b/src/__tests__/fixtures/simpleIntrospection.ts @@ -42,6 +42,29 @@ export type simpleIntrospection = { enumValues: null, possibleTypes: null, }, + { + kind: 'INPUT_OBJECT', + name: 'DefaultPayload', + fields: null, + inputFields: [ + { + name: 'value', + type: { + kind: 'NON_NULL', + name: null, + ofType: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + }, + defaultValue: 'DEFAULT', + } + ], + interfaces: null, + enumValues: null, + possibleTypes: null, + }, { kind: 'OBJECT', name: 'Query', diff --git a/src/__tests__/fixtures/simpleSchema.ts b/src/__tests__/fixtures/simpleSchema.ts index 660c2634..85c07194 100644 --- a/src/__tests__/fixtures/simpleSchema.ts +++ b/src/__tests__/fixtures/simpleSchema.ts @@ -243,6 +243,26 @@ export type simpleSchema = { ]; }; + DefaultPayload: { + kind: 'INPUT_OBJECT'; + name: 'DefaultPayload'; + inputFields: [ + { + name: 'value'; + type: { + kind: 'NON_NULL'; + name: null; + ofType: { + kind: 'SCALAR'; + name: 'String'; + ofType: null; + }; + }; + defaultValue: 'DEFAULT'; + }, + ]; + }; + LatestTodoResult: { kind: 'UNION'; name: 'LatestTodoResult'; diff --git a/src/__tests__/variables.test-d.ts b/src/__tests__/variables.test-d.ts index 9237d077..8816f884 100644 --- a/src/__tests__/variables.test-d.ts +++ b/src/__tests__/variables.test-d.ts @@ -46,6 +46,23 @@ test('allows optionals for default values', () => { expectTypeOf().toEqualTypeOf(); }); +test('allows optionals for default values inside input-objects', () => { + const query = ` + mutation ($input: DefaultPayload!) { + __typename + } + `; + + type doc = parseDocument; + type variables = getVariablesType; + + expectTypeOf().toEqualTypeOf<{ + input: { + value?: string | null | undefined; + }; + }>(); +}); + test('allows optionals for nullable values', () => { const query = ` mutation ($id: ID) { From a3858bf511be5ec00292e3560e591f6bd11ae588 Mon Sep 17 00:00:00 2001 From: Phil Pluckthun Date: Fri, 23 Feb 2024 19:54:10 +0000 Subject: [PATCH 3/3] Add changeset --- .changeset/strong-ducks-shop.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/strong-ducks-shop.md diff --git a/.changeset/strong-ducks-shop.md b/.changeset/strong-ducks-shop.md new file mode 100644 index 00000000..6b136ca2 --- /dev/null +++ b/.changeset/strong-ducks-shop.md @@ -0,0 +1,5 @@ +--- +'gql.tada': patch +--- + +Add missing support for input object fields with default values. Previously, input object fields with default values were still marked as required in variables.