Skip to content

Commit

Permalink
allow customisation for extensions type (#6969)
Browse files Browse the repository at this point in the history
* Allow customization for extensions type

This introduces a new configuration for the `graphql-request` plugin for
typescript. It allows to use `unknown` (or a custom type) instead of
`any` for the `extensions` parameter without breaking changes.

* Add changeset

* Add tests for extensionsType config
  • Loading branch information
dodomorandi authored Nov 5, 2021
1 parent 2c4081f commit 3b87f04
Show file tree
Hide file tree
Showing 5 changed files with 652 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-seas-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-graphql-request': minor
---

Add the extensionsType config in order to change the default type for extensions when rawRequest is true.
13 changes: 13 additions & 0 deletions packages/plugins/typescript/graphql-request/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,17 @@ export interface RawGraphQLRequestPluginConfig extends RawClientSideBasePluginCo
* ```
*/
rawRequest?: boolean;

/**
* @description Allows you to override the type for extensions when `rawRequest` is enabled.
* @default any
*
* @exampleMarkdown
* ```yml
* config:
* rawRequest: true
* extensionsType: unknown
* ```
*/
extensionsType?: string;
}
4 changes: 3 additions & 1 deletion packages/plugins/typescript/graphql-request/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { RawGraphQLRequestPluginConfig } from './config';

export interface GraphQLRequestPluginConfig extends ClientSideBasePluginConfig {
rawRequest: boolean;
extensionsType: string;
}

const additionalExportedTypes = `
Expand All @@ -33,6 +34,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
constructor(schema: GraphQLSchema, fragments: LoadedFragment[], rawConfig: RawGraphQLRequestPluginConfig) {
super(schema, fragments, rawConfig, {
rawRequest: getConfigValue(rawConfig.rawRequest, false),
extensionsType: getConfigValue(rawConfig.extensionsType, "any"),
});

autoBind(this);
Expand Down Expand Up @@ -111,7 +113,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
o.operationVariablesTypes
}, requestHeaders?: Dom.RequestInit["headers"]): Promise<{ data?: ${
o.operationResultType
} | undefined; extensions?: any; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> {
} | undefined; extensions?: ${this.config.extensionsType}; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> {
return withWrapper((wrappedRequestHeaders) => client.rawRequest<${
o.operationResultType
}>(${docArg}, variables, {...requestHeaders, ...wrappedRequestHeaders}), '${operationName}');
Expand Down
Loading

0 comments on commit 3b87f04

Please # to comment.