Skip to content

Commit

Permalink
Fix unused parameter in generated code which caused TS errors for use…
Browse files Browse the repository at this point in the history
…rs of the package. (#613)

When using the default behavior (ie: config `rawRequest` as `false`), the generated code has a `E` unused variable which make TS yelling when TS options `noUnusedParameters` is `true`.

The fix is put that variable onlye when `rawRequest` is `true`.
  • Loading branch information
j0k3r authored Feb 6, 2024
1 parent 6fe40f7 commit ae45d38
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-avocados-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-codegen/typescript-generic-sdk': patch
---

Fix unused parameter in generated code which caused TS errors for users of the package.
20 changes: 15 additions & 5 deletions packages/plugins/typescript/generic-sdk/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,27 @@ export class GenericSdkVisitor extends ClientSideBaseVisitor<

const documentNodeType =
this.config.documentMode === DocumentMode.string ? 'string' : 'DocumentNode';
const resultData = this.config.rawRequest ? 'ExecutionResult<R, E>' : 'R';
const returnType = `Promise<${resultData}> | ${
usingObservable ? 'Observable' : 'AsyncIterable'
}<${resultData}>`;

return `export type Requester<C = {}, E = unknown> = <R, V>(doc: ${documentNodeType}, vars?: V, options?: C) => ${returnType}
if (this.config.rawRequest) {
return `export type Requester<C = {}, E = unknown> = <R, V>(doc: ${documentNodeType}, vars?: V, options?: C) => Promise<ExecutionResult<R, E>> | ${
usingObservable ? 'Observable' : 'AsyncIterable'
}<ExecutionResult<R, E>>
export function getSdk<C, E>(requester: Requester<C, E>) {
return {
${allPossibleActions.join(',\n')}
};
}
export type Sdk = ReturnType<typeof getSdk>;`;
}

return `export type Requester<C = {}> = <R, V>(doc: ${documentNodeType}, vars?: V, options?: C) => Promise<R> | ${
usingObservable ? 'Observable' : 'AsyncIterable'
}<R>
export function getSdk<C>(requester: Requester<C>) {
return {
${allPossibleActions.join(',\n')}
};
}
export type Sdk = ReturnType<typeof getSdk>;`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ export const Feed4Document = gql\`
}
}
\`;
export type Requester<C = {}, E = unknown> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<R> | AsyncIterable<R>
export function getSdk<C, E>(requester: Requester<C, E>) {
export type Requester<C = {}> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<R> | AsyncIterable<R>
export function getSdk<C>(requester: Requester<C>) {
return {
feed(variables?: FeedQueryVariables, options?: C): Promise<FeedQuery> {
return requester<FeedQuery, FeedQueryVariables>(FeedDocument, variables, options) as Promise<FeedQuery>;
Expand Down Expand Up @@ -486,8 +486,8 @@ export const FeedLiveDocument = gql\`
}
}
\`;
export type Requester<C = {}, E = unknown> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<R> | AsyncIterable<R>
export function getSdk<C, E>(requester: Requester<C, E>) {
export type Requester<C = {}> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<R> | AsyncIterable<R>
export function getSdk<C>(requester: Requester<C>) {
return {
feed(variables?: FeedQueryVariables, options?: C): Promise<FeedQuery> {
return requester<FeedQuery, FeedQueryVariables>(FeedDocument, variables, options) as Promise<FeedQuery>;
Expand Down Expand Up @@ -726,8 +726,8 @@ export const FeedLiveDocument = gql\`
}
}
\`;
export type Requester<C = {}, E = unknown> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<R> | Observable<R>
export function getSdk<C, E>(requester: Requester<C, E>) {
export type Requester<C = {}> = <R, V>(doc: DocumentNode, vars?: V, options?: C) => Promise<R> | Observable<R>
export function getSdk<C>(requester: Requester<C>) {
return {
feed(variables?: FeedQueryVariables, options?: C): Promise<FeedQuery> {
return requester<FeedQuery, FeedQueryVariables>(FeedDocument, variables, options) as Promise<FeedQuery>;
Expand Down Expand Up @@ -978,8 +978,8 @@ export const Feed4Document = \`
}
}
\`;
export type Requester<C = {}, E = unknown> = <R, V>(doc: string, vars?: V, options?: C) => Promise<R> | AsyncIterable<R>
export function getSdk<C, E>(requester: Requester<C, E>) {
export type Requester<C = {}> = <R, V>(doc: string, vars?: V, options?: C) => Promise<R> | AsyncIterable<R>
export function getSdk<C>(requester: Requester<C>) {
return {
feed(variables?: FeedQueryVariables, options?: C): Promise<FeedQuery> {
return requester<FeedQuery, FeedQueryVariables>(FeedDocument, variables, options) as Promise<FeedQuery>;
Expand Down

0 comments on commit ae45d38

Please sign in to comment.