Skip to content

Commit

Permalink
fix: Make fragmentRefs in selection types a required property (0no-co#18
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kitten authored Jan 17, 2024
1 parent 018823f commit ddc98ee
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-bulldogs-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'gql.tada': patch
---

Make `$tada.fragmentRefs` property required. Previously, this was optional (to mirror what GCG’s client-preset does). However, this can lead to invalid checks in `readFragment`, as it would be able to match types that don’t actually match the fragment refs.
13 changes: 13 additions & 0 deletions src/__tests__/api.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ describe('mirrorFragmentTypeRec', () => {
});

describe('readFragment', () => {
it('should not unmask empty objects', () => {
type fragment = parseDocument<`
fragment Fields on Todo {
id
}
`>;

type document = getDocumentNode<fragment, schema>;
// @ts-expect-error
const result = readFragment({} as document, {});
expectTypeOf<typeof result>().toBeNever();
});

it('unmasks regular fragments', () => {
type fragment = parseDocument<`
fragment Fields on Todo {
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/selection.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ test('infers fragment spreads for fragment refs', () => {
type actual = getDocumentType<query, schema, extraFragments>;

type expected = {
[$tada.fragmentRefs]?: {
[$tada.fragmentRefs]: {
Fields: extraFragments['Fields'][$tada.fragmentId];
};
};
Expand All @@ -235,7 +235,7 @@ test('marks undefined fragments with special fragment ref error', () => {
type actual = getDocumentType<query, schema>;

type expected = {
[$tada.fragmentRefs]?: {
[$tada.fragmentRefs]: {
Fields: 'Undefined Fragment';
};
};
Expand Down
7 changes: 6 additions & 1 deletion src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,12 @@ type fragmentOfTypeRec<Document extends DocumentDefDecorationLike> =
function readFragment<
const Document extends DocumentDefDecorationLike & DocumentDecoration<any, any>,
const Fragment extends fragmentOfTypeRec<Document>,
>(_document: Document, fragment: Fragment): mirrorFragmentTypeRec<Fragment, ResultOf<Document>> {
>(
_document: Document,
fragment: Fragment
): fragmentOfTypeRec<Document> extends Fragment
? never
: mirrorFragmentTypeRec<Fragment, ResultOf<Document>> {
return fragment as any;
}

Expand Down
4 changes: 2 additions & 2 deletions src/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ type getFragmentsOfDocumentsRec<Documents> = Documents extends readonly [
: {};

type makeFragmentRef<Definition extends FragmentDefDecorationLike> = obj<{
[$tada.fragmentRefs]?: {
[$tada.fragmentRefs]: {
[Name in Definition['name']['value']]: Definition[$tada.fragmentId];
};
}>;

type makeUndefinedFragmentRef<FragmentName extends string> = {
[$tada.fragmentRefs]?: {
[$tada.fragmentRefs]: {
[Name in FragmentName]: 'Undefined Fragment';
};
};
Expand Down
2 changes: 1 addition & 1 deletion website/src/content/docs/get-started/writing-graphql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ const result: ResultOf<typeof TodosQuery> = {
todos: [
{
id: 'ExampleID',
[$tada.fragmentRefs]?: {
[$tada.fragmentRefs]: {
TodoItem: unique symbol;
};
},
Expand Down

0 comments on commit ddc98ee

Please sign in to comment.