Skip to content

Commit 6d7c1d7

Browse files
adapapeddeee888
andauthored
feat: add includeExternalFragments config option (#10270)
This option allows you to include external fragments in the generated code. Closes #10269. Co-authored-by: Eddy Nguyen <[email protected]>
1 parent 20164e0 commit 6d7c1d7

File tree

4 files changed

+72
-1
lines changed

4 files changed

+72
-1
lines changed

.changeset/thin-shirts-repair.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-codegen/visitor-plugin-common': minor
3+
'@graphql-codegen/typescript-operations': minor
4+
---
5+
6+
feat: implement `includeExternalFragments: boolean` option

packages/plugins/other/visitor-plugin-common/src/base-visitor.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface ParsedConfig {
3838
inlineFragmentTypes: InlineFragmentTypeOptions;
3939
emitLegacyCommonJSImports: boolean;
4040
printFieldsOnNewLines: boolean;
41+
includeExternalFragments: boolean;
4142
}
4243

4344
export interface RawConfig {
@@ -391,6 +392,13 @@ export interface RawConfig {
391392
* without resorting to running tools like Prettier on the output.
392393
*/
393394
printFieldsOnNewLines?: boolean;
395+
396+
/**
397+
* @default false
398+
* @description Whether to include external fragments in the generated code. External fragments are not defined
399+
* in the same location as the operation definition.
400+
*/
401+
includeExternalFragments?: boolean;
394402
}
395403

396404
export class BaseVisitor<TRawConfig extends RawConfig = RawConfig, TPluginConfig extends ParsedConfig = ParsedConfig> {
@@ -415,6 +423,7 @@ export class BaseVisitor<TRawConfig extends RawConfig = RawConfig, TPluginConfig
415423
rawConfig.emitLegacyCommonJSImports === undefined ? true : !!rawConfig.emitLegacyCommonJSImports,
416424
extractAllFieldsToTypes: rawConfig.extractAllFieldsToTypes ?? false,
417425
printFieldsOnNewLines: rawConfig.printFieldsOnNewLines ?? false,
426+
includeExternalFragments: rawConfig.includeExternalFragments ?? false,
418427
...((additionalConfig || {}) as any),
419428
};
420429

packages/plugins/other/visitor-plugin-common/src/client-side-base-visitor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ export class ClientSideBaseVisitor<
558558
const graph = this.fragmentsGraph;
559559
const orderedDeps = graph.overallOrder();
560560
const localFragments = orderedDeps
561-
.filter(name => !graph.getNodeData(name).isExternal)
561+
.filter(name => !graph.getNodeData(name).isExternal || this.config.includeExternalFragments)
562562
.map(name => this._generateFragment(graph.getNodeData(name).node));
563563

564564
return localFragments.join('\n');

packages/plugins/other/visitor-plugin-common/tests/client-side-base-visitor.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,59 @@ describe('getImports', () => {
274274
});
275275
});
276276
});
277+
278+
describe('includeExternalFragments', () => {
279+
const schema = buildSchema(/* GraphQL */ `
280+
type Query {
281+
a: A
282+
}
283+
284+
type A {
285+
foo: String
286+
bar: String
287+
}
288+
`);
289+
290+
const document = parse(`
291+
query fooBarQuery {
292+
a {
293+
...ExternalA
294+
}
295+
}
296+
`);
297+
298+
const externalFragments = parse(`
299+
fragment ExternalA on A {
300+
foo
301+
bar
302+
}
303+
`)
304+
.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION)
305+
.map(fragmentDef => ({
306+
node: fragmentDef,
307+
name: fragmentDef.name.value,
308+
onType: fragmentDef.typeCondition.name.value,
309+
isExternal: true,
310+
}));
311+
312+
it('should not include external fragments', () => {
313+
const visitor = new ClientSideBaseVisitor(schema, externalFragments, {}, {});
314+
315+
visitor.OperationDefinition(document.definitions[0] as OperationDefinitionNode);
316+
317+
expect(visitor.fragments).toBe('');
318+
});
319+
320+
it('should include external fragments', () => {
321+
const visitor = new ClientSideBaseVisitor(
322+
schema,
323+
externalFragments,
324+
{
325+
includeExternalFragments: true,
326+
},
327+
{}
328+
);
329+
330+
expect(visitor.fragments).toContain('ExternalAFragment');
331+
});
332+
});

0 commit comments

Comments
 (0)