Skip to content

Commit

Permalink
Fix an bug in how inputRef types are normalized
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Jul 24, 2023
1 parent ab9cb70 commit 425435a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-poems-march.md
Original file line number Diff line number Diff line change
@@ -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
15 changes: 5 additions & 10 deletions packages/core/src/refs/input-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import {
InputFieldsFromShape,
InputRef,
InputShapeFromFields,
inputShapeKey,
RecursivelyNormalizeNullableFields,
SchemaTypes,
Expand Down Expand Up @@ -33,17 +34,11 @@ export class ImplementableInputObjectRef<
this.builder = builder;
}

implement(
options: PothosSchemaTypes.InputObjectTypeOptions<
Types,
InputFieldsFromShape<RecursivelyNormalizeNullableFields<T>>
>,
implement<Fields extends InputFieldsFromShape<RecursivelyNormalizeNullableFields<T>>>(
options: PothosSchemaTypes.InputObjectTypeOptions<Types, Fields>,
) {
this.builder.inputType<
ImplementableInputObjectRef<Types, T>,
InputFieldsFromShape<RecursivelyNormalizeNullableFields<T>>
>(this, options);
this.builder.inputType<ImplementableInputObjectRef<Types, T>, Fields>(this, options);

return this as InputObjectRef<RecursivelyNormalizeNullableFields<T>>;
return this as InputObjectRef<InputShapeFromFields<Fields>>;
}
}
2 changes: 2 additions & 0 deletions packages/core/src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export type RecursivelyNormalizeNullableFields<T> = undefined extends T
? RecursivelyNormalizeNullableFields<NonNullable<T>> | null | undefined
: T extends (infer L)[]
? RecursivelyNormalizeNullableFields<L>[]
: T extends (...args: any[]) => unknown
? T
: T extends object
? Normalize<
{
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ enum MyEnum {
}
input NestedListInput {
date: Date
list: [[Int!]]!
}
Expand Down
23 changes: 17 additions & 6 deletions packages/core/tests/examples/random-stuff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down Expand Up @@ -110,7 +110,7 @@ interface ExampleShape {
ids: (number | string)[];
ids2?: number[];
enum?: MyEnum;
date?: string;
date?: string | Date;
};
id?: number | string;
ids: (number | string)[];
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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 }),
}),
});

Expand All @@ -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;
},
}),
);

Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-add-graphql/src/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ proto.addGraphQLInput = function addGraphQLInput<Shape extends {}>(
...options
}: AddGraphQLInputTypeOptions<SchemaTypes, Shape>,
) {
const ref = this.inputRef<never>(name);
const ref = this.inputRef<Shape>(name);

return ref.implement({
...options,
Expand Down Expand Up @@ -321,5 +321,5 @@ proto.addGraphQLInput = function addGraphQLInput<Shape extends {}>(

return combinedFields as never;
},
});
}) as never;
};

1 comment on commit 425435a

@vercel
Copy link

@vercel vercel bot commented on 425435a Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.