Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(typescript-operations): Generate
$fragmentName
for fragment su…
…btypes 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>
- Loading branch information
1 parent
33d13e1
commit 2966686
Showing
11 changed files
with
2,665 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@graphql-codegen/visitor-plugin-common': patch | ||
'@graphql-codegen/typescript-operations': patch | ||
--- | ||
|
||
Generate $fragmentName for fragment subtypes for fragment masking |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
dev-test/gql-tag-operations-masking-star-wars/gql/fragment-masking.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'; | ||
|
||
export type FragmentType<TDocumentType extends DocumentNode<any, any>> = TDocumentType extends DocumentNode< | ||
infer TType, | ||
any | ||
> | ||
? TType extends { ' $fragmentName': infer TKey } | ||
? TKey extends string | ||
? { ' $fragmentRefs': { [key in TKey]: TType } } | ||
: never | ||
: never | ||
: never; | ||
|
||
export function useFragment<TType>( | ||
_documentNode: DocumentNode<TType, any>, | ||
fragmentType: FragmentType<DocumentNode<TType, any>> | ||
): TType { | ||
return fragmentType as any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* eslint-disable */ | ||
import * as graphql from './graphql'; | ||
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core'; | ||
|
||
const documents = { | ||
'\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n': | ||
graphql.HeroDetailsWithFragmentDocument, | ||
'\n fragment HeroDetails on Character {\n __typename\n name\n ... on Human {\n height\n }\n ... on Droid {\n primaryFunction\n }\n }\n': | ||
graphql.HeroDetailsFragmentDoc, | ||
}; | ||
|
||
export function gql( | ||
source: '\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n' | ||
): typeof documents['\n query HeroDetailsWithFragment($episode: Episode) {\n hero(episode: $episode) {\n ...HeroDetails\n }\n }\n']; | ||
export function gql( | ||
source: '\n fragment HeroDetails on Character {\n __typename\n name\n ... on Human {\n height\n }\n ... on Droid {\n primaryFunction\n }\n }\n' | ||
): 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']; | ||
|
||
export function gql(source: string): unknown; | ||
export function gql(source: string) { | ||
return (documents as any)[source] ?? {}; | ||
} | ||
|
||
export type DocumentType<TDocumentNode extends DocumentNode<any, any>> = TDocumentNode extends DocumentNode< | ||
infer TType, | ||
any | ||
> | ||
? TType | ||
: never; |
Oops, something went wrong.
2966686
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
graphql-code-generator – ./
graphql-code-generator-theguild.vercel.app
graphql-code-generator.vercel.app
graphql-code-generator-git-master-theguild.vercel.app
www.graphql-code-generator.com
graphql-code-generator.com
www.graphql-codegen.com
graphql-codegen.com