Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add missing case for input object fields with default value #73

Merged
merged 3 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/strong-ducks-shop.md
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 23 additions & 0 deletions src/__tests__/fixtures/simpleIntrospection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
20 changes: 20 additions & 0 deletions src/__tests__/fixtures/simpleSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
17 changes: 17 additions & 0 deletions src/__tests__/variables.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@ test('allows optionals for default values', () => {
expectTypeOf<variables['id']>().toEqualTypeOf<string | number | undefined>();
});

test('allows optionals for default values inside input-objects', () => {
const query = `
mutation ($input: DefaultPayload!) {
__typename
}
`;

type doc = parseDocument<typeof query>;
type variables = getVariablesType<doc, schema>;

expectTypeOf<variables>().toEqualTypeOf<{
input: {
value?: string | null | undefined;
};
}>();
});

test('allows optionals for nullable values', () => {
const query = `
mutation ($id: ID) {
Expand Down
4 changes: 2 additions & 2 deletions src/variables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<InputField['type'], Introspection> }
: { [Name in InputField['name']]?: unwrapType<InputField['type'], Introspection> }
: { [Name in InputField['name']]?: unwrapType<InputField['type'], Introspection> | null }
: {}) &
InputObject
>
Expand Down
Loading