File tree Expand file tree Collapse file tree 4 files changed +144
-0
lines changed
plugins/other/visitor-plugin-common/src Expand file tree Collapse file tree 4 files changed +144
-0
lines changed Original file line number Diff line number Diff line change 1+ ---
2+ ' @graphql-codegen/visitor-plugin-common ' : patch
3+ ' @graphql-codegen/client-preset ' : patch
4+ ---
5+
6+ Include nested fragments in string documentMode
Original file line number Diff line number Diff line change @@ -358,6 +358,7 @@ export class ClientSideBaseVisitor<
358358 protected _gql ( node : FragmentDefinitionNode | OperationDefinitionNode ) : string {
359359 const includeNestedFragments =
360360 this . config . documentMode === DocumentMode . documentNode ||
361+ this . config . documentMode === DocumentMode . string ||
361362 ( this . config . dedupeFragments && node . kind === 'OperationDefinition' ) ;
362363 const fragmentNames = this . _extractFragments ( node , includeNestedFragments ) ;
363364 const fragments = this . _transformFragments ( fragmentNames ) ;
Original file line number Diff line number Diff line change @@ -2423,5 +2423,94 @@ export * from "./gql.js";`);
24232423 "
24242424 ` ) ;
24252425 } ) ;
2426+
2427+ it ( 'correctly resolves nested fragments' , async ( ) => {
2428+ const result = await executeCodegen ( {
2429+ schema : [
2430+ /* GraphQL */ `
2431+ scalar Date
2432+
2433+ type Query {
2434+ video(id: ID!): Video!
2435+ }
2436+
2437+ interface Video {
2438+ id: ID!
2439+ title: String!
2440+ }
2441+
2442+ type Movie implements Video {
2443+ id: ID!
2444+ title: String!
2445+ releaseDate: Date!
2446+ collection: Collection
2447+ }
2448+
2449+ type Collection {
2450+ id: ID!
2451+ title: String!
2452+ }
2453+
2454+ type Episode implements Video {
2455+ id: ID!
2456+ title: String!
2457+ show: Show!
2458+ releaseDate: Date!
2459+ }
2460+
2461+ type Show {
2462+ id: ID!
2463+ title: String!
2464+ releaseDate: Date!
2465+ }
2466+ ` ,
2467+ ] ,
2468+ documents : path . join ( __dirname , 'fixtures/with-nested-fragment.ts' ) ,
2469+ generates : {
2470+ 'out1/' : {
2471+ preset,
2472+ config : {
2473+ documentMode : 'string' ,
2474+ } ,
2475+ } ,
2476+ } ,
2477+ } ) ;
2478+
2479+ const graphqlFile = result . find ( file => file . filename === 'out1/graphql.ts' ) ;
2480+ expect ( graphqlFile . content ) . toBeSimilarStringTo ( `
2481+ export const VideoDocument = new TypedDocumentString(\`
2482+ query Video($id: ID!) {
2483+ video(id: $id) {
2484+ ...DetailsFragment
2485+ __typename
2486+ }
2487+ }
2488+ fragment EpisodeFragment on Episode {
2489+ id
2490+ title
2491+ show {
2492+ id
2493+ title
2494+ }
2495+ releaseDate
2496+ __typename
2497+ }
2498+ fragment MovieFragment on Movie {
2499+ id
2500+ title
2501+ collection {
2502+ id
2503+ }
2504+ releaseDate
2505+ __typename
2506+ }
2507+ fragment DetailsFragment on Video {
2508+ title
2509+ __typename
2510+ ...MovieFragment
2511+ ...EpisodeFragment
2512+ }\`) as unknown as TypedDocumentString<VideoQuery, VideoQueryVariables>;
2513+ ` ) ;
2514+ } ) ;
24262515 } ) ;
24272516} ) ;
Original file line number Diff line number Diff line change 1+ /* eslint-disable @typescript-eslint/ban-ts-comment */
2+
3+ //@ts -ignore
4+ const episodeFragment = gql ( /* GraphQL */ `
5+ fragment EpisodeFragment on Episode {
6+ id
7+ title
8+ show {
9+ id
10+ title
11+ }
12+ releaseDate
13+ __typename
14+ }
15+ ` ) ;
16+
17+ //@ts -ignore
18+ const movieFragment = gql ( /* GraphQL */ `
19+ fragment MovieFragment on Movie {
20+ id
21+ title
22+ collection {
23+ id
24+ }
25+ releaseDate
26+ __typename
27+ }
28+ ` ) ;
29+
30+ //@ts -ignore
31+ const videoDetailsFragment = gql ( /* GraphQL */ `
32+ fragment DetailsFragment on Video {
33+ title
34+ __typename
35+ ...MovieFragment
36+ ...EpisodeFragment
37+ }
38+ ` ) ;
39+
40+ //@ts -ignore
41+ const videoQueryDocument = gql ( /* GraphQL */ `
42+ query Video($id: ID!) {
43+ video(id: $id) {
44+ ...DetailsFragment
45+ __typename
46+ }
47+ }
48+ ` ) ;
You can’t perform that action at this time.
0 commit comments