Skip to content

Commit

Permalink
fix: always inline and deduplicate fragment definitions (#8971)
Browse files Browse the repository at this point in the history
  • Loading branch information
n1ru4l authored Feb 16, 2023
1 parent 1af3bf6 commit 6b6fe3c
Show file tree
Hide file tree
Showing 21 changed files with 639 additions and 234 deletions.
5 changes: 5 additions & 0 deletions .changeset/rare-rats-rhyme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/visitor-plugin-common': patch
---

Always inline referenced fragments within their document. This prevents issues with duplicated fragments or missing fragments.
6 changes: 6 additions & 0 deletions .changeset/rotten-lizards-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/client-preset': patch
'@graphql-codegen/typed-document-node': patch
---

Allow passing fragment documents to APIs like Apollos `readFragment`
192 changes: 187 additions & 5 deletions dev-test/githunt/typed-document-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,59 @@ export const FeedEntryFragmentDoc = {
],
},
},
...VoteButtonsFragmentDoc.definitions,
...RepoInfoFragmentDoc.definitions,
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'VoteButtons' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Entry' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'score' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'vote' },
selectionSet: {
kind: 'SelectionSet',
selections: [{ kind: 'Field', name: { kind: 'Name', value: 'vote_value' } }],
},
},
],
},
},
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'RepoInfo' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Entry' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'createdAt' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'repository' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'description' } },
{ kind: 'Field', name: { kind: 'Name', value: 'stargazers_count' } },
{ kind: 'Field', name: { kind: 'Name', value: 'open_issues_count' } },
],
},
},
{
kind: 'Field',
name: { kind: 'Name', value: 'postedBy' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'html_url' } },
{ kind: 'Field', name: { kind: 'Name', value: 'login' } },
],
},
},
],
},
},
],
} as unknown as DocumentNode<FeedEntryFragment, unknown>;
export const OnCommentAddedDocument = {
Expand Down Expand Up @@ -629,7 +680,30 @@ export const CommentDocument = {
],
},
},
...CommentsPageCommentFragmentDoc.definitions,
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'CommentsPageComment' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Comment' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'postedBy' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'login' } },
{ kind: 'Field', name: { kind: 'Name', value: 'html_url' } },
],
},
},
{ kind: 'Field', name: { kind: 'Name', value: 'createdAt' } },
{ kind: 'Field', name: { kind: 'Name', value: 'content' } },
],
},
},
],
} as unknown as DocumentNode<CommentQuery, CommentQueryVariables>;
export const CurrentUserForProfileDocument = {
Expand Down Expand Up @@ -721,7 +795,92 @@ export const FeedDocument = {
],
},
},
...FeedEntryFragmentDoc.definitions,
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'VoteButtons' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Entry' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'score' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'vote' },
selectionSet: {
kind: 'SelectionSet',
selections: [{ kind: 'Field', name: { kind: 'Name', value: 'vote_value' } }],
},
},
],
},
},
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'RepoInfo' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Entry' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'createdAt' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'repository' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'description' } },
{ kind: 'Field', name: { kind: 'Name', value: 'stargazers_count' } },
{ kind: 'Field', name: { kind: 'Name', value: 'open_issues_count' } },
],
},
},
{
kind: 'Field',
name: { kind: 'Name', value: 'postedBy' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'html_url' } },
{ kind: 'Field', name: { kind: 'Name', value: 'login' } },
],
},
},
],
},
},
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'FeedEntry' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Entry' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
{ kind: 'Field', name: { kind: 'Name', value: 'commentCount' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'repository' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'full_name' } },
{ kind: 'Field', name: { kind: 'Name', value: 'html_url' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'owner' },
selectionSet: {
kind: 'SelectionSet',
selections: [{ kind: 'Field', name: { kind: 'Name', value: 'avatar_url' } }],
},
},
],
},
},
{ kind: 'FragmentSpread', name: { kind: 'Name', value: 'VoteButtons' } },
{ kind: 'FragmentSpread', name: { kind: 'Name', value: 'RepoInfo' } },
],
},
},
],
} as unknown as DocumentNode<FeedQuery, FeedQueryVariables>;
export const SubmitRepositoryDocument = {
Expand Down Expand Up @@ -806,7 +965,30 @@ export const SubmitCommentDocument = {
],
},
},
...CommentsPageCommentFragmentDoc.definitions,
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'CommentsPageComment' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Comment' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'id' } },
{
kind: 'Field',
name: { kind: 'Name', value: 'postedBy' },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: 'login' } },
{ kind: 'Field', name: { kind: 'Name', value: 'html_url' } },
],
},
},
{ kind: 'Field', name: { kind: 'Name', value: 'createdAt' } },
{ kind: 'Field', name: { kind: 'Name', value: 'content' } },
],
},
},
],
} as unknown as DocumentNode<SubmitCommentMutation, SubmitCommentMutationVariables>;
export const VoteDocument = {
Expand Down
29 changes: 28 additions & 1 deletion dev-test/gql-tag-operations-masking-star-wars/gql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,33 @@ export const HeroDetailsWithFragmentDocument = {
],
},
},
...HeroDetailsFragmentDoc.definitions,
{
kind: 'FragmentDefinition',
name: { kind: 'Name', value: 'HeroDetails' },
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Character' } },
selectionSet: {
kind: 'SelectionSet',
selections: [
{ kind: 'Field', name: { kind: 'Name', value: '__typename' } },
{ kind: 'Field', name: { kind: 'Name', value: 'name' } },
{
kind: 'InlineFragment',
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Human' } },
selectionSet: {
kind: 'SelectionSet',
selections: [{ kind: 'Field', name: { kind: 'Name', value: 'height' } }],
},
},
{
kind: 'InlineFragment',
typeCondition: { kind: 'NamedType', name: { kind: 'Name', value: 'Droid' } },
selectionSet: {
kind: 'SelectionSet',
selections: [{ kind: 'Field', name: { kind: 'Name', value: 'primaryFunction' } }],
},
},
],
},
},
],
} as unknown as DocumentNode<HeroDetailsWithFragmentQuery, HeroDetailsWithFragmentQueryVariables>;
Loading

0 comments on commit 6b6fe3c

Please sign in to comment.