Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add includeExternalFragments config option #10270

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thin-shirts-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphql-codegen/visitor-plugin-common': minor
'@graphql-codegen/typescript-operations': minor
---

feat: implement `includeExternalFragments: boolean` option
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface ParsedConfig {
inlineFragmentTypes: InlineFragmentTypeOptions;
emitLegacyCommonJSImports: boolean;
printFieldsOnNewLines: boolean;
includeExternalFragments: boolean;
}

export interface RawConfig {
Expand Down Expand Up @@ -391,6 +392,13 @@ export interface RawConfig {
* without resorting to running tools like Prettier on the output.
*/
printFieldsOnNewLines?: boolean;

/**
* @default false
* @description Whether to include external fragments in the generated code. External fragments are not defined
* in the same location as the operation definition.
*/
includeExternalFragments?: boolean;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ export class ClientSideBaseVisitor<
const graph = this.fragmentsGraph;
const orderedDeps = graph.overallOrder();
const localFragments = orderedDeps
.filter(name => !graph.getNodeData(name).isExternal)
.filter(name => !graph.getNodeData(name).isExternal || this.config.includeExternalFragments)
.map(name => this._generateFragment(graph.getNodeData(name).node));

return localFragments.join('\n');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,59 @@ describe('getImports', () => {
});
});
});

describe('includeExternalFragments', () => {
const schema = buildSchema(/* GraphQL */ `
type Query {
a: A
}
type A {
foo: String
bar: String
}
`);

const document = parse(`
query fooBarQuery {
a {
...ExternalA
}
}
`);

const externalFragments = parse(`
fragment ExternalA on A {
foo
bar
}
`)
.definitions.filter(d => d.kind === Kind.FRAGMENT_DEFINITION)
.map(fragmentDef => ({
node: fragmentDef,
name: fragmentDef.name.value,
onType: fragmentDef.typeCondition.name.value,
isExternal: true,
}));

it('should not include external fragments', () => {
const visitor = new ClientSideBaseVisitor(schema, externalFragments, {}, {});

visitor.OperationDefinition(document.definitions[0] as OperationDefinitionNode);

expect(visitor.fragments).toBe('');
});

it('should include external fragments', () => {
const visitor = new ClientSideBaseVisitor(
schema,
externalFragments,
{
includeExternalFragments: true,
},
{}
);

expect(visitor.fragments).toContain('ExternalAFragment');
});
});
Loading