Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: Remove explicit return types from sdk methods #7592

Closed
Closed
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
16 changes: 16 additions & 0 deletions .changeset/shiny-dogs-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@graphql-codegen/typescript-graphql-request': major
'@graphql-codegen/website': patch
---

Do not specify return types of sdk methods explicitly.

Previously, the return type of all SDK methods was declared manually. This can
cause the return type to become outdated when graphql-request gets updated.
This already happened, as graphql-request removed the errors attribute from the
return type (see https://github.com/prisma-labs/graphql-request/issues/174).
Also, data was marked as possibly undefined which is wrong. data always exists
on a successful response (failed requests throw a ClientError).

A sideeffect of this change is that the extensionsType config became obsolete
and has been removed.
13 changes: 0 additions & 13 deletions packages/plugins/typescript/graphql-request/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,4 @@ 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;
}
10 changes: 2 additions & 8 deletions packages/plugins/typescript/graphql-request/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { RawGraphQLRequestPluginConfig } from './config';

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

const additionalExportedTypes = `
Expand All @@ -34,7 +33,6 @@ 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,19 +109,15 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor<
}
return `${operationName}(variables${optionalVariables ? '?' : ''}: ${
o.operationVariablesTypes
}, requestHeaders?: Dom.RequestInit["headers"]): Promise<{ data?: ${
o.operationResultType
} | undefined; extensions?: ${
this.config.extensionsType
}; headers: Dom.Headers; status: number; errors?: GraphQLError[] | undefined; }> {
}, requestHeaders?: Dom.RequestInit["headers"]) {
return withWrapper((wrappedRequestHeaders) => client.rawRequest<${
o.operationResultType
}>(${docArg}, variables, {...requestHeaders, ...wrappedRequestHeaders}), '${operationName}');
}`;
} else {
return `${operationName}(variables${optionalVariables ? '?' : ''}: ${
o.operationVariablesTypes
}, requestHeaders?: Dom.RequestInit["headers"]): Promise<${o.operationResultType}> {
}, requestHeaders?: Dom.RequestInit["headers"]) {
return withWrapper((wrappedRequestHeaders) => client.request<${
o.operationResultType
}>(${docVarName}, variables, {...requestHeaders, ...wrappedRequestHeaders}), '${operationName}');
Expand Down
Loading