Skip to content

Commit

Permalink
fix: Protect against missing typename fields on base types (#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten authored Aug 18, 2024
1 parent d9d78a7 commit ca8b441
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/three-jobs-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gql.tada": patch
---

Add `__typename` narrowing of unmasked interface fragment spreads, which could otherwise lead to confusion. This usually is relevant when the parent selection set forgets to include a `__typename` selection.
62 changes: 62 additions & 0 deletions src/__tests__/api.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,68 @@ describe('graphql()', () => {
}>();
});

// See: https://github.com/0no-co/gql.tada/issues/365
it('should create a fragment type of unmasked interface fragments on object types', () => {
const interfaceFragment = graphql(`
fragment Fields on ITodo @_unmask {
__typename
id
}
`);

const objectFragment = graphql(
`
fragment Object on SmallTodo @_unmask {
maxLength
...Fields
}
`,
[interfaceFragment]
);

const standaloneFragment = graphql(`
fragment Object on SmallTodo @_unmask {
maxLength
...Fields
}
fragment Fields on ITodo @_unmask {
__typename
id
}
`);

// NOTE: BigTodo's fields shouldn't be included here
const nestedFragment = graphql(`
fragment Object on SmallTodo @_unmask {
maxLength
... on ITodo {
id
... on BigTodo {
wallOfText
}
}
}
`);

expectTypeOf<FragmentOf<typeof objectFragment>>().toEqualTypeOf<{
__typename: 'SmallTodo';
id: string;
maxLength: number | null;
}>();

expectTypeOf<FragmentOf<typeof standaloneFragment>>().toEqualTypeOf<{
__typename: 'SmallTodo';
id: string;
maxLength: number | null;
}>();

expectTypeOf<FragmentOf<typeof nestedFragment>>().toEqualTypeOf<{
id: string;
maxLength: number | null;
}>();
});

it('should preserve object literal types for variables', () => {
const mutation = graphql(`
mutation ($input: TodoPayload!) {
Expand Down
5 changes: 4 additions & 1 deletion src/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ type getFragmentSelection<
: Node extends { kind: Kind.FRAGMENT_SPREAD; name: any }
? Node['name']['value'] extends keyof Fragments
? Fragments[Node['name']['value']] extends { [$tada.ref]: any }
? Fragments[Node['name']['value']][$tada.ref]
? Type extends { kind: 'INTERFACE'; name: any }
? /* This protects against various edge cases where users forget to select `__typename` (See `getSelection`) */
Fragments[Node['name']['value']][$tada.ref] & { __typename?: PossibleType }
: Fragments[Node['name']['value']][$tada.ref]
: getPossibleTypeSelectionRec<
Fragments[Node['name']['value']]['selectionSet']['selections'],
PossibleType,
Expand Down

0 comments on commit ca8b441

Please sign in to comment.