From e346f77e5a8dbb16f78cf94d86a1543ab9ec799a Mon Sep 17 00:00:00 2001 From: L Lllvvuu Date: Sat, 2 Mar 2024 14:49:59 -0800 Subject: [PATCH 1/5] fix(getInputObjectTypeRec): handle no default value --- src/__tests__/fixtures/simpleIntrospection.ts | 11 +++++++++++ src/__tests__/fixtures/simpleSchema.ts | 11 +++++++++++ src/__tests__/variables.test-d.ts | 4 ++-- src/variables.ts | 17 ++++++++++++++--- 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/__tests__/fixtures/simpleIntrospection.ts b/src/__tests__/fixtures/simpleIntrospection.ts index 6a5cb494..b3c833b9 100644 --- a/src/__tests__/fixtures/simpleIntrospection.ts +++ b/src/__tests__/fixtures/simpleIntrospection.ts @@ -28,6 +28,17 @@ export type simpleIntrospection = { }, defaultValue: null, }, + { + name: 'description', + type: { + kind: 'NON_NULL', + ofType: { + kind: 'SCALAR', + name: 'String', + ofType: null, + }, + }, + }, { name: 'complete', type: { diff --git a/src/__tests__/fixtures/simpleSchema.ts b/src/__tests__/fixtures/simpleSchema.ts index 85c07194..bf772c62 100644 --- a/src/__tests__/fixtures/simpleSchema.ts +++ b/src/__tests__/fixtures/simpleSchema.ts @@ -231,6 +231,17 @@ export type simpleSchema = { }; defaultValue: null; }, + { + name: 'description'; + type: { + kind: 'NON_NULL'; + ofType: { + kind: 'SCALAR'; + name: 'String'; + ofType: null; + }; + }; + }, { name: 'complete'; type: { diff --git a/src/__tests__/variables.test-d.ts b/src/__tests__/variables.test-d.ts index 0b4ea343..8fc6e039 100644 --- a/src/__tests__/variables.test-d.ts +++ b/src/__tests__/variables.test-d.ts @@ -29,7 +29,7 @@ describe('getVariablesType', () => { expectTypeOf().toEqualTypeOf<{ id: string | number; - input: { title: string; complete?: boolean | null }; + input: { title: string; complete?: boolean | null; description: string }; }>(); }); @@ -93,7 +93,7 @@ describe('getScalarType', () => { }); it('gets the type of an input object', () => { - type expected = { title: string; complete?: boolean | null }; + type expected = { title: string; complete?: boolean | null; description: string }; expectTypeOf>().toEqualTypeOf(); }); }); diff --git a/src/variables.ts b/src/variables.ts index fedbb97c..c36a23e8 100644 --- a/src/variables.ts +++ b/src/variables.ts @@ -12,9 +12,20 @@ type getInputObjectTypeRec< Rest, Introspection, (InputField extends { name: any; type: any } - ? InputField extends { defaultValue: undefined | null; type: { kind: 'NON_NULL' } } - ? { [Name in InputField['name']]: unwrapType } - : { [Name in InputField['name']]?: unwrapType | null } + ? InputField extends { type: { kind: 'NON_NULL' } } + ? InputField extends { defaultValue: infer DefaultValue } + ? DefaultValue extends undefined | null + ? { [Name in InputField['name']]: unwrapType } + : { + [Name in InputField['name']]?: unwrapType< + InputField['type'], + Introspection + > | null; + } + : { [Name in InputField['name']]: unwrapType } + : { + [Name in InputField['name']]?: unwrapType | null; + } : {}) & InputObject > From 1d06027dbf1bb9e8262023ac3c56a78a0f933dc5 Mon Sep 17 00:00:00 2001 From: L Date: Sat, 2 Mar 2024 15:55:44 -0700 Subject: [PATCH 2/5] chore: changeset --- .changeset/strange-nails-ring.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/strange-nails-ring.md diff --git a/.changeset/strange-nails-ring.md b/.changeset/strange-nails-ring.md new file mode 100644 index 00000000..0c446c80 --- /dev/null +++ b/.changeset/strange-nails-ring.md @@ -0,0 +1,5 @@ +--- +"@fake-scope/fake-pkg": patch +--- + +fix(getInputObjectTypeRec): Correctly infer type of field with no default value (should be non-optional and non-nullable) From 4f94a8ee31dce043aa2e86ef9037c92d5072a91e Mon Sep 17 00:00:00 2001 From: L Date: Sat, 2 Mar 2024 15:56:47 -0700 Subject: [PATCH 3/5] chore: update changset --- .changeset/strange-nails-ring.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/strange-nails-ring.md b/.changeset/strange-nails-ring.md index 0c446c80..8c9b3da7 100644 --- a/.changeset/strange-nails-ring.md +++ b/.changeset/strange-nails-ring.md @@ -1,5 +1,5 @@ --- -"@fake-scope/fake-pkg": patch +'gql.tada': patch --- fix(getInputObjectTypeRec): Correctly infer type of field with no default value (should be non-optional and non-nullable) From c21529a76921d63c5aea628f3cbc4a69ff57890c Mon Sep 17 00:00:00 2001 From: L Date: Sat, 2 Mar 2024 17:17:21 -0700 Subject: [PATCH 4/5] refactor: apply suggestion Co-authored-by: Phil Pluckthun --- src/variables.ts | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/variables.ts b/src/variables.ts index c36a23e8..754ea24f 100644 --- a/src/variables.ts +++ b/src/variables.ts @@ -12,20 +12,9 @@ type getInputObjectTypeRec< Rest, Introspection, (InputField extends { name: any; type: any } - ? InputField extends { type: { kind: 'NON_NULL' } } - ? InputField extends { defaultValue: infer DefaultValue } - ? DefaultValue extends undefined | null - ? { [Name in InputField['name']]: unwrapType } - : { - [Name in InputField['name']]?: unwrapType< - InputField['type'], - Introspection - > | null; - } - : { [Name in InputField['name']]: unwrapType } - : { - [Name in InputField['name']]?: unwrapType | null; - } + ? InputField extends { defaultValue?: undefined | null; type: { kind: 'NON_NULL' } } + ? { [Name in InputField['name']]: unwrapType } + : { [Name in InputField['name']]?: unwrapType | null } : {}) & InputObject > From bf1a7d13818e98ec92bf179d24d3c163e9efab97 Mon Sep 17 00:00:00 2001 From: L Date: Sat, 2 Mar 2024 17:17:31 -0700 Subject: [PATCH 5/5] docs: apply suggestion Co-authored-by: Phil Pluckthun --- .changeset/strange-nails-ring.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/strange-nails-ring.md b/.changeset/strange-nails-ring.md index 8c9b3da7..8e8474f3 100644 --- a/.changeset/strange-nails-ring.md +++ b/.changeset/strange-nails-ring.md @@ -2,4 +2,4 @@ 'gql.tada': patch --- -fix(getInputObjectTypeRec): Correctly infer type of field with no default value (should be non-optional and non-nullable) +Handle inference of input object fields with missing `defaultValue` properties in introspection.