Skip to content

Commit

Permalink
Include nested fragments in string document mode (#9414)
Browse files Browse the repository at this point in the history
  • Loading branch information
beerose committed May 19, 2023
1 parent 4a87e88 commit ca02ad1
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/strange-eggs-attack.md
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': patch
'@graphql-codegen/client-preset': patch
---

Include nested fragments in string documentMode
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
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
@@ -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
}
}
`);

0 comments on commit ca02ad1

Please sign in to comment.