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

Include nested fragments in string document mode #9414

Merged
merged 7 commits into from
May 19, 2023
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
6 changes: 6 additions & 0 deletions .changeset/strange-eggs-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/client-preset': patch
---

Include nested fragments in string documentMode
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ export class ClientSideBaseVisitor<
protected _gql(node: FragmentDefinitionNode | OperationDefinitionNode): string {
const includeNestedFragments =
this.config.documentMode === DocumentMode.documentNode ||
this.config.documentMode === DocumentMode.string ||
(this.config.dedupeFragments && node.kind === 'OperationDefinition');
const fragmentNames = this._extractFragments(node, includeNestedFragments);
const fragments = this._transformFragments(fragmentNames);
Expand Down
89 changes: 89 additions & 0 deletions packages/presets/client/tests/client-preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2423,5 +2423,94 @@ export * from "./gql.js";`);
"
`);
});

it('correctly resolves nested fragments', async () => {
const result = await executeCodegen({
schema: [
/* GraphQL */ `
scalar Date

type Query {
video(id: ID!): Video!
}

interface Video {
id: ID!
title: String!
}

type Movie implements Video {
id: ID!
title: String!
releaseDate: Date!
collection: Collection
}

type Collection {
id: ID!
title: String!
}

type Episode implements Video {
id: ID!
title: String!
show: Show!
releaseDate: Date!
}

type Show {
id: ID!
title: String!
releaseDate: Date!
}
`,
],
documents: path.join(__dirname, 'fixtures/with-nested-fragment.ts'),
generates: {
'out1/': {
preset,
config: {
documentMode: 'string',
},
},
},
});

const graphqlFile = result.find(file => file.filename === 'out1/graphql.ts');
expect(graphqlFile.content).toBeSimilarStringTo(`
export const VideoDocument = new TypedDocumentString(\`
query Video($id: ID!) {
video(id: $id) {
...DetailsFragment
__typename
}
}
fragment EpisodeFragment on Episode {
id
title
show {
id
title
}
releaseDate
__typename
}
fragment MovieFragment on Movie {
id
title
collection {
id
}
releaseDate
__typename
}
fragment DetailsFragment on Video {
title
__typename
...MovieFragment
...EpisodeFragment
}\`) as unknown as TypedDocumentString<VideoQuery, VideoQueryVariables>;
`);
});
});
});
48 changes: 48 additions & 0 deletions packages/presets/client/tests/fixtures/with-nested-fragment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */

//@ts-ignore
const episodeFragment = gql(/* GraphQL */ `
fragment EpisodeFragment on Episode {
id
title
show {
id
title
}
releaseDate
__typename
}
`);

//@ts-ignore
const movieFragment = gql(/* GraphQL */ `
fragment MovieFragment on Movie {
id
title
collection {
id
}
releaseDate
__typename
}
`);

//@ts-ignore
const videoDetailsFragment = gql(/* GraphQL */ `
fragment DetailsFragment on Video {
title
__typename
...MovieFragment
...EpisodeFragment
}
`);

//@ts-ignore
const videoQueryDocument = gql(/* GraphQL */ `
query Video($id: ID!) {
video(id: $id) {
...DetailsFragment
__typename
}
}
`);