diff --git a/.changeset/fifty-poems-march.md b/.changeset/fifty-poems-march.md new file mode 100644 index 000000000..62e87269c --- /dev/null +++ b/.changeset/fifty-poems-march.md @@ -0,0 +1,6 @@ +--- +'@pothos/plugin-add-graphql': patch +'@pothos/core': patch +--- + +Improve typing of inputRefs and fix incorrectly normalized function properties of inputRef types diff --git a/packages/core/src/refs/input-object.ts b/packages/core/src/refs/input-object.ts index 3788b51cc..913fcefa9 100644 --- a/packages/core/src/refs/input-object.ts +++ b/packages/core/src/refs/input-object.ts @@ -2,6 +2,7 @@ import { InputFieldsFromShape, InputRef, + InputShapeFromFields, inputShapeKey, RecursivelyNormalizeNullableFields, SchemaTypes, @@ -33,17 +34,11 @@ export class ImplementableInputObjectRef< this.builder = builder; } - implement( - options: PothosSchemaTypes.InputObjectTypeOptions< - Types, - InputFieldsFromShape> - >, + implement>>( + options: PothosSchemaTypes.InputObjectTypeOptions, ) { - this.builder.inputType< - ImplementableInputObjectRef, - InputFieldsFromShape> - >(this, options); + this.builder.inputType, Fields>(this, options); - return this as InputObjectRef>; + return this as InputObjectRef>; } } diff --git a/packages/core/src/types/utils.ts b/packages/core/src/types/utils.ts index cb4d6d912..545e684a6 100644 --- a/packages/core/src/types/utils.ts +++ b/packages/core/src/types/utils.ts @@ -52,6 +52,8 @@ export type RecursivelyNormalizeNullableFields = undefined extends T ? RecursivelyNormalizeNullableFields> | null | undefined : T extends (infer L)[] ? RecursivelyNormalizeNullableFields[] + : T extends (...args: any[]) => unknown + ? T : T extends object ? Normalize< { diff --git a/packages/core/tests/__snapshots__/index.test.ts.snap b/packages/core/tests/__snapshots__/index.test.ts.snap index ca9449d33..f540f98ad 100644 --- a/packages/core/tests/__snapshots__/index.test.ts.snap +++ b/packages/core/tests/__snapshots__/index.test.ts.snap @@ -52,6 +52,7 @@ enum MyEnum { } input NestedListInput { + date: Date list: [[Int!]]! } diff --git a/packages/core/tests/examples/random-stuff.ts b/packages/core/tests/examples/random-stuff.ts index 81561977e..7f7cfbbbb 100644 --- a/packages/core/tests/examples/random-stuff.ts +++ b/packages/core/tests/examples/random-stuff.ts @@ -13,7 +13,7 @@ interface Types { Shaveable: { shaved: boolean }; }; Scalars: { - Date: { Input: string; Output: string }; + Date: { Input: Date | string; Output: string | Date }; }; Context: { userID: number }; } @@ -110,7 +110,7 @@ interface ExampleShape { ids: (number | string)[]; ids2?: number[]; enum?: MyEnum; - date?: string; + date?: string | Date; }; id?: number | string; ids: (number | string)[]; @@ -169,8 +169,13 @@ builder.objectType('User', { example2: t.arg({ type: Example2, required: true }), firstN: t.arg.id(), }, - resolve: (parent, args) => - Number.parseInt(String(args.example2.more.more.more.example.id), 10), + resolve: (parent, args) => { + const d: Date | string = args.example2.more.more.more.example.date!; + + console.log(d); + + return Number.parseInt(String(args.example2.more.more.more.example.id), 10); + }, }), // Using a union type related: t.field({ @@ -366,13 +371,14 @@ builder.subscriptionType({ builder.queryField('constructor', (t) => t.boolean({ resolve: () => true })); const NestedListInput = builder - .inputRef<{ list: (number[] | null)[] }>('NestedListInput') + .inputRef<{ list: (number[] | null)[]; data?: Date }>('NestedListInput') .implement({ fields: (t) => ({ list: t.field({ type: t.listRef(t.listRef('Int'), { required: false }), required: true, }), + date: t.field({ type: 'Date', required: false }), }), }); @@ -388,7 +394,12 @@ builder.queryField('nestedLists', (t) => type: NestedListInput, }), }, - resolve: (_, args) => args.input, + resolve: (_, args) => { + const date: Date | string | null | undefined = args.nestedListInput?.date; + void date; + + return args.input; + }, }), ); diff --git a/packages/plugin-add-graphql/src/schema-builder.ts b/packages/plugin-add-graphql/src/schema-builder.ts index ae96f31e4..2e03df498 100644 --- a/packages/plugin-add-graphql/src/schema-builder.ts +++ b/packages/plugin-add-graphql/src/schema-builder.ts @@ -288,7 +288,7 @@ proto.addGraphQLInput = function addGraphQLInput( ...options }: AddGraphQLInputTypeOptions, ) { - const ref = this.inputRef(name); + const ref = this.inputRef(name); return ref.implement({ ...options, @@ -321,5 +321,5 @@ proto.addGraphQLInput = function addGraphQLInput( return combinedFields as never; }, - }); + }) as never; };