File tree Expand file tree Collapse file tree
packages/presets/near-operation-file Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -325,7 +325,16 @@ export const preset: Types.OutputPreset<NearOperationFileConfig> = {
325325 const artifacts : Array < Types . GenerateOptions > = [ ] ;
326326
327327 for ( const [ filename , record ] of filePathsMap . entries ( ) ) {
328+ const fragmentNames = new Set < string > ( ) ;
328329 let fragmentImportsArr = record . fragmentImports ;
330+ const externalFragments = record . externalFragments . filter ( fragment => {
331+ if ( fragmentNames . has ( fragment . name ) ) {
332+ return false ;
333+ }
334+
335+ fragmentNames . add ( fragment . name ) ;
336+ return true ;
337+ } ) ;
329338
330339 if ( importAllFragmentsFrom ) {
331340 fragmentImportsArr = record . fragmentImports . map < ImportDeclaration < FragmentImport > > ( t => {
@@ -377,7 +386,7 @@ export const preset: Types.OutputPreset<NearOperationFileConfig> = {
377386 // are exported from operations file
378387 exportFragmentSpreadSubTypes : true ,
379388 namespacedImportName : importTypesNamespace ,
380- externalFragments : record . externalFragments ,
389+ externalFragments,
381390 fragmentImports : fragmentImportsArr ,
382391 } ;
383392
Original file line number Diff line number Diff line change @@ -85,6 +85,53 @@ describe('near-operation-file preset', () => {
8585 ] ;
8686
8787 describe ( 'Issues' , ( ) => {
88+ it ( 'dedupes repeated external fragments when multiple documents are merged into the same output file' , async ( ) => {
89+ const result = await executePreset ( {
90+ baseOutputDir : './src/' ,
91+ config : { } ,
92+ presetConfig : {
93+ cwd : '/some/deep/path' ,
94+ baseTypesPath : 'types.ts' ,
95+ fileName : 'types' ,
96+ } ,
97+ schemaAst : schemaNode ,
98+ schema : getCachedDocumentNodeFromSchema ( schemaNode ) ,
99+ documents : [
100+ {
101+ location : '/some/deep/path/src/graphql/query-a.graphql' ,
102+ document : parse ( /* GraphQL */ `
103+ query QueryA {
104+ user {
105+ ...UserFields
106+ }
107+ }
108+ ` ) ,
109+ } ,
110+ {
111+ location : '/some/deep/path/src/graphql/query-b.graphql' ,
112+ document : parse ( /* GraphQL */ `
113+ query QueryB {
114+ user {
115+ ...UserFields
116+ }
117+ }
118+ ` ) ,
119+ } ,
120+ {
121+ location : '/some/deep/path/src/graphql/user-fragment.graphql' ,
122+ document : fragmentAst ,
123+ } ,
124+ ] ,
125+ plugins : [ ] ,
126+ pluginMap : { } ,
127+ } ) ;
128+
129+ expect ( result ) . toHaveLength ( 1 ) ;
130+ expect ( result [ 0 ] . filename ) . toBe ( '/some/deep/path/src/graphql/types.generated.ts' ) ;
131+ expect ( result [ 0 ] . config . externalFragments ) . toHaveLength ( 1 ) ;
132+ expect ( result [ 0 ] . config . externalFragments [ 0 ] . name ) . toBe ( 'UserFields' ) ;
133+ } ) ;
134+
88135 it ( '#5002 - error when inline fragment does not specify the name of the type' , async ( ) => {
89136 const testSchema = parse ( /* GraphQL */ `
90137 scalar Date
You can’t perform that action at this time.
0 commit comments