Skip to content

Commit 28f8346

Browse files
authored
šŸ› fix: honor importOperationTypesFrom in graphql-request generator (#7115)
1 parent d84afec commit 28f8346

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

ā€Ž.changeset/fuzzy-bears-speak.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript-graphql-request': patch
3+
---
4+
5+
Honor importOperationTypesFrom config option

ā€Žpackages/plugins/typescript/graphql-request/src/visitor.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
2323
RawGraphQLRequestPluginConfig,
2424
GraphQLRequestPluginConfig
2525
> {
26+
private _externalImportPrefix: string;
2627
private _operationsToInclude: {
2728
node: OperationDefinitionNode;
2829
documentVariableName: string;
@@ -47,6 +48,8 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
4748
if (this.config.rawRequest && this.config.documentMode !== DocumentMode.string) {
4849
this._additionalImports.push(`import { print } from 'graphql'`);
4950
}
51+
52+
this._externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : '';
5053
}
5154

5255
public OperationDefinition(node: OperationDefinitionNode) {
@@ -72,6 +75,9 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
7275
operationResultType: string,
7376
operationVariablesTypes: string
7477
): string {
78+
operationResultType = this._externalImportPrefix + operationResultType;
79+
operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;
80+
7581
this._operationsToInclude.push({
7682
node,
7783
documentVariableName,

ā€Žpackages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,5 +348,19 @@ async function test() {
348348
`(Operations.Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query');`
349349
);
350350
});
351+
352+
it('#7114 - honor importOperationTypesFrom', async () => {
353+
const config = { importOperationTypesFrom: 'Types' };
354+
const docs = [{ location: '', document: basicDoc }];
355+
const result = (await plugin(schema, docs, config, {
356+
outputFile: 'graphql.ts',
357+
})) as Types.ComplexPluginOutput;
358+
const output = await validate(result, config, docs, schema, '');
359+
360+
expect(output).toContain(`Types.FeedQuery`);
361+
expect(output).toContain(`Types.Feed2Query`);
362+
expect(output).toContain(`Types.Feed3Query`);
363+
expect(output).toContain(`Types.Feed4Query`);
364+
});
351365
});
352366
});

0 commit comments

Comments
 (0)