Skip to content

Commit

Permalink
πŸ› fix: honor importOperationTypesFrom in graphql-request generator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chadly committed Jul 5, 2022
1 parent d84afec commit 28f8346
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-bears-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-graphql-request': patch
---

Honor importOperationTypesFrom config option
6 changes: 6 additions & 0 deletions packages/plugins/typescript/graphql-request/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
RawGraphQLRequestPluginConfig,
GraphQLRequestPluginConfig
> {
private _externalImportPrefix: string;
private _operationsToInclude: {
node: OperationDefinitionNode;
documentVariableName: string;
Expand All @@ -47,6 +48,8 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
if (this.config.rawRequest && this.config.documentMode !== DocumentMode.string) {
this._additionalImports.push(`import { print } from 'graphql'`);
}

this._externalImportPrefix = this.config.importOperationTypesFrom ? `${this.config.importOperationTypesFrom}.` : '';
}

public OperationDefinition(node: OperationDefinitionNode) {
Expand All @@ -72,6 +75,9 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
operationResultType: string,
operationVariablesTypes: string
): string {
operationResultType = this._externalImportPrefix + operationResultType;
operationVariablesTypes = this._externalImportPrefix + operationVariablesTypes;

this._operationsToInclude.push({
node,
documentVariableName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -348,5 +348,19 @@ async function test() {
`(Operations.Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query');`
);
});

it('#7114 - honor importOperationTypesFrom', async () => {
const config = { importOperationTypesFrom: 'Types' };
const docs = [{ location: '', document: basicDoc }];
const result = (await plugin(schema, docs, config, {
outputFile: 'graphql.ts',
})) as Types.ComplexPluginOutput;
const output = await validate(result, config, docs, schema, '');

expect(output).toContain(`Types.FeedQuery`);
expect(output).toContain(`Types.Feed2Query`);
expect(output).toContain(`Types.Feed3Query`);
expect(output).toContain(`Types.Feed4Query`);
});
});
});

1 comment on commit 28f8346

@vercel
Copy link

@vercel vercel bot commented on 28f8346 Jul 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.