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: Make fragmentRefs in selection types a required property #18

Merged
merged 3 commits into from
Jan 17, 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/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
Loading