@@ -19,7 +19,7 @@ import {
1919import gqlTag from 'graphql-tag' ;
2020import { BaseVisitor , ParsedConfig , RawConfig } from './base-visitor.js' ;
2121import { LoadedFragment , ParsedImport } from './types.js' ;
22- import { buildScalarsFromConfig , unique , flatten , getConfigValue , groupBy } from './utils.js' ;
22+ import { asTemplateLiteral , buildScalarsFromConfig , unique , flatten , getConfigValue , groupBy } from './utils.js' ;
2323import { FragmentImport , ImportDeclaration , generateFragmentImportStatement } from './imports.js' ;
2424
2525gqlTag . enableExperimentalFragmentVariables ( ) ;
@@ -350,10 +350,13 @@ export class ClientSideBaseVisitor<
350350 const fragmentNames = this . _extractFragments ( node , includeNestedFragments ) ;
351351 const fragments = this . _transformFragments ( fragmentNames ) ;
352352
353- const doc = this . _prepareDocument ( `
354- ${ print ( node ) . split ( '\\' ) . join ( '\\\\' ) /* Re-escape escaped values in GraphQL syntax */ }
355- ${ this . _includeFragments ( fragments ) } ` ) ;
353+ // Re-escape escaped values in GraphQL syntax.
354+ const docLiteralContents = print ( node ) . split ( '\\' ) . join ( '\\\\' ) ;
355+ const fragmentLiteralContents = this . _includeFragments ( fragments ) ;
356356
357+ const doc = this . _prepareDocument ( `
358+ ${ docLiteralContents }
359+ ${ fragmentLiteralContents } ` ) ;
357360 if ( this . config . documentMode === DocumentMode . documentNode ) {
358361 let gqlObj = gqlTag ( [ doc ] ) ;
359362
@@ -414,8 +417,9 @@ export class ClientSideBaseVisitor<
414417 }
415418
416419 if ( this . config . documentMode === DocumentMode . string ) {
420+ const escapedLiteralContents = asTemplateLiteral ( doc ) . slice ( 1 , - 1 ) ;
417421 if ( node . kind === Kind . FRAGMENT_DEFINITION ) {
418- return `new TypedDocumentString(\`${ doc } \`, ${ JSON . stringify ( { fragmentName : node . name . value } ) } )` ;
422+ return `new TypedDocumentString(\`${ escapedLiteralContents } \`, ${ JSON . stringify ( { fragmentName : node . name . value } ) } )` ;
419423 }
420424
421425 if ( this . _onExecutableDocumentNode && node . kind === Kind . OPERATION_DEFINITION ) {
@@ -425,16 +429,21 @@ export class ClientSideBaseVisitor<
425429 if ( this . _omitDefinitions === true ) {
426430 return `{${ `"__meta__":${ JSON . stringify ( meta ) } ,` . slice ( 0 , - 1 ) } }` ;
427431 }
428- return `new TypedDocumentString(\`${ doc } \`, ${ JSON . stringify ( meta ) } )` ;
432+ return `new TypedDocumentString(\`${ escapedLiteralContents } \`, ${ JSON . stringify ( meta ) } )` ;
429433 }
430434 }
431435
432- return `new TypedDocumentString(\`${ doc } \`)` ;
436+ return `new TypedDocumentString(\`${ escapedLiteralContents } \`)` ;
433437 }
434438
439+ const escapedLiteralContentsPrefix = asTemplateLiteral ( docLiteralContents ) . slice ( 1 , - 1 ) ;
440+ const escapedDoc = this . _prepareDocument ( `
441+ ${ escapedLiteralContentsPrefix }
442+ ${ fragmentLiteralContents } ` ) ;
443+
435444 const gqlImport = this . _parseImport ( this . config . gqlImport || 'graphql-tag' ) ;
436445
437- return ( gqlImport . propName || 'gql' ) + '`' + doc + '`' ;
446+ return ( gqlImport . propName || 'gql' ) + '`' + escapedDoc + '`' ;
438447 }
439448
440449 protected _getGraphQLCodegenMetadata (
0 commit comments