Skip to content

Commit 2966686

Browse files
izumin5210n1ru4l
andauthored
fix(typescript-operations): Generate $fragmentName for fragment subtypes for fragment masking (#7813)
* Generate ` $fragmentName` for fragment subtypes for fragment masking * Add changeset * 🚮 Co-authored-by: Laurin Quast <laurinquast@googlemail.com> * Add dev-test `gql-tag-operations-masking-star-wars` for complex usecases Co-authored-by: Laurin Quast <laurinquast@googlemail.com>
1 parent 33d13e1 commit 2966686

File tree

11 files changed

+2665
-1
lines changed

11 files changed

+2665
-1
lines changed

‎.changeset/two-dolphins-deliver.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': patch
3+
'@graphql-codegen/typescript-operations': patch
4+
---
5+
6+
Generate $fragmentName for fragment subtypes for fragment masking

‎dev-test/codegen.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,3 +417,9 @@ generates:
417417
preset: gql-tag-operations-preset
418418
presetConfig:
419419
fragmentMasking: true
420+
./dev-test/gql-tag-operations-masking-star-wars/gql:
421+
schema: ./dev-test/gql-tag-operations-masking-star-wars/schema.json
422+
documents: './dev-test/gql-tag-operations-masking-star-wars/src/**/*.tsx'
423+
preset: gql-tag-operations-preset
424+
presetConfig:
425+
fragmentMasking: true
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
2+
3+
export type FragmentType<TDocumentType extends DocumentNode<any, any>> = TDocumentType extends DocumentNode<
4+
infer TType,
5+
any
6+
>
7+
? TType extends { ' $fragmentName': infer TKey }
8+
? TKey extends string
9+
? { ' $fragmentRefs': { [key in TKey]: TType } }
10+
: never
11+
: never
12+
: never;
13+
14+
export function useFragment<TType>(
15+
_documentNode: DocumentNode<TType, any>,
16+
fragmentType: FragmentType<DocumentNode<TType, any>>
17+
): TType {
18+
return fragmentType as any;
19+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* eslint-disable */
2+
import * as graphql from './graphql';
3+
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
4+
5+
const documents = {
6+
'\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n':
7+
graphql.HeroDetailsWithFragmentDocument,
8+
'\n fragment HeroDetails on Character {\n __typename\n name\n ... on Human {\n height\n }\n ... on Droid {\n primaryFunction\n }\n }\n':
9+
graphql.HeroDetailsFragmentDoc,
10+
};
11+
12+
export function gql(
13+
source: '\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n'
14+
): typeof documents['\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n'];
15+
export function gql(
16+
source: '\n fragment HeroDetails on Character {\n __typename\n name\n ... on Human {\n height\n }\n ... on Droid {\n primaryFunction\n }\n }\n'
17+
): typeof documents['\n fragment HeroDetails on Character {\n __typename\n name\n ... on Human {\n height\n }\n ... on Droid {\n primaryFunction\n }\n }\n'];
18+
19+
export function gql(source: string): unknown;
20+
export function gql(source: string) {
21+
return (documents as any)[source] ?? {};
22+
}
23+
24+
export type DocumentType<TDocumentNode extends DocumentNode<any, any>> = TDocumentNode extends DocumentNode<
25+
infer TType,
26+
any
27+
>
28+
? TType
29+
: never;

0 commit comments

Comments
 (0)