From bfd503bef12c1fc5b589cfa0dfbc352aaf2cac9a Mon Sep 17 00:00:00 2001 From: An Xie Date: Thu, 23 Nov 2023 20:29:38 -0700 Subject: [PATCH 1/2] feat: [graphql-request] pass variables to wrapper function --- .../typescript/graphql-request/src/visitor.ts | 8 +- .../graphql-request.spec.ts.snap | 120 +++++++++--------- .../tests/graphql-request.spec.ts | 8 +- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/packages/plugins/typescript/graphql-request/src/visitor.ts b/packages/plugins/typescript/graphql-request/src/visitor.ts index cf49ed847..2bcdb9f90 100644 --- a/packages/plugins/typescript/graphql-request/src/visitor.ts +++ b/packages/plugins/typescript/graphql-request/src/visitor.ts @@ -16,7 +16,7 @@ export interface GraphQLRequestPluginConfig extends ClientSideBasePluginConfig { } const additionalExportedTypes = ` -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; `; export class GraphQLRequestVisitor extends ClientSideBaseVisitor< @@ -138,7 +138,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor< }; headers: Headers; status: number; }> { return withWrapper((wrappedRequestHeaders) => client.rawRequest<${ o.operationResultType - }>(${docArg}, variables, {...requestHeaders, ...wrappedRequestHeaders}), '${operationName}', '${operationType}'); + }>(${docArg}, variables, {...requestHeaders, ...wrappedRequestHeaders}), '${operationName}', '${operationType}', variables); }`; } return `${operationName}(variables${optionalVariables ? '?' : ''}: ${ @@ -146,7 +146,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor< }, requestHeaders?: GraphQLClientRequestHeaders): Promise<${o.operationResultType}> { return withWrapper((wrappedRequestHeaders) => client.request<${ o.operationResultType - }>(${docVarName}, variables, {...requestHeaders, ...wrappedRequestHeaders}), '${operationName}', '${operationType}'); + }>(${docVarName}, variables, {...requestHeaders, ...wrappedRequestHeaders}), '${operationName}', '${operationType}', variables); }`; }) .filter(Boolean) @@ -154,7 +154,7 @@ export class GraphQLRequestVisitor extends ClientSideBaseVisitor< return `${additionalExportedTypes} -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); ${extraVariables.join('\n')} export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { diff --git a/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap b/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap index aaeaecc5f..1ab4b4841 100644 --- a/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap +++ b/packages/plugins/typescript/graphql-request/tests/__snapshots__/graphql-request.spec.ts.snap @@ -238,24 +238,24 @@ export const Feed4Document = \` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } @@ -524,24 +524,24 @@ export const Feed4Document = gql\` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } @@ -802,24 +802,24 @@ export const Feed4Document = \` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } @@ -1082,10 +1082,10 @@ export const Feed4Document = gql\` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); const FeedDocumentString = print(FeedDocument); const Feed2DocumentString = print(Feed2Document); const Feed3DocumentString = print(Feed3Document); @@ -1093,16 +1093,16 @@ const Feed4DocumentString = print(Feed4Document); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: FeedQuery; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed2Query; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed3Query; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed4Query; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } @@ -1365,24 +1365,24 @@ export const Feed4Document = \` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: FeedQuery; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed2Query; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed3Query; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed4Query; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } @@ -1645,24 +1645,24 @@ export const Feed4Document = gql\` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } @@ -1926,10 +1926,10 @@ export const Feed4Document = gql\` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); const FeedDocumentString = print(FeedDocument); const Feed2DocumentString = print(Feed2Document); const Feed3DocumentString = print(Feed3Document); @@ -1937,16 +1937,16 @@ const Feed4DocumentString = print(Feed4Document); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: FeedQuery; errors?: GraphQLError[]; extensions?: unknown; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed2Query; errors?: GraphQLError[]; extensions?: unknown; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed3Query; errors?: GraphQLError[]; extensions?: unknown; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed4Query; errors?: GraphQLError[]; extensions?: unknown; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } @@ -2210,10 +2210,10 @@ export const Feed4Document = gql\` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); const FeedDocumentString = print(FeedDocument); const Feed2DocumentString = print(Feed2Document); const Feed3DocumentString = print(Feed3Document); @@ -2221,16 +2221,16 @@ const Feed4DocumentString = print(Feed4Document); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: FeedQuery; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(FeedDocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed2Query; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed2DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed3Query; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed3DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise<{ data: Feed4Query; errors?: GraphQLError[]; extensions?: any; headers: Headers; status: number; }> { - return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.rawRequest(Feed4DocumentString, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } @@ -2493,24 +2493,24 @@ export const Feed4Document = gql\` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } @@ -2773,24 +2773,24 @@ export const Feed4Document = gql\` } \`; -export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string) => Promise; +export type SdkFunctionWrapper = (action: (requestHeaders?:Record) => Promise, operationName: string, operationType?: string, variables?: any) => Promise; -const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType) => action(); +const defaultWrapper: SdkFunctionWrapper = (action, _operationName, _operationType, variables) => action(); export function getSdk(client: GraphQLClient, withWrapper: SdkFunctionWrapper = defaultWrapper) { return { feed(variables?: FeedQueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables); }, feed2(variables: Feed2QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables); }, feed3(variables?: Feed3QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables); }, feed4(variables?: Feed4QueryVariables, requestHeaders?: GraphQLClientRequestHeaders): Promise { - return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query'); + return withWrapper((wrappedRequestHeaders) => client.request(Feed4Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed4', 'query', variables); } }; } diff --git a/packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts b/packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts index 1f663391b..699bc1f63 100644 --- a/packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts +++ b/packages/plugins/typescript/graphql-request/tests/graphql-request.spec.ts @@ -93,7 +93,7 @@ async function test() { const output = await validate(result, config, docs, schema, usage); expect(result.content).toContain( - `(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query');`, + `(FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables);`, ); expect(output).toMatchSnapshot(); }); @@ -407,13 +407,13 @@ async function test() { expect(output).toContain(`import * as Operations from './operations';`); expect(output).toContain( - `(Operations.FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query');`, + `(Operations.FeedDocument, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed', 'query', variables);`, ); expect(output).toContain( - `(Operations.Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query');`, + `(Operations.Feed2Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed2', 'query', variables);`, ); expect(output).toContain( - `(Operations.Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query');`, + `(Operations.Feed3Document, variables, {...requestHeaders, ...wrappedRequestHeaders}), 'feed3', 'query', variables);`, ); }); From f4e830f9206e3f5f7119e881fc0ad18bce4be543 Mon Sep 17 00:00:00 2001 From: Saihajpreet Singh Date: Mon, 18 Dec 2023 15:47:41 -0500 Subject: [PATCH 2/2] Create rude-cameras-whisper.md --- .changeset/rude-cameras-whisper.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/rude-cameras-whisper.md diff --git a/.changeset/rude-cameras-whisper.md b/.changeset/rude-cameras-whisper.md new file mode 100644 index 000000000..d92e027e4 --- /dev/null +++ b/.changeset/rude-cameras-whisper.md @@ -0,0 +1,5 @@ +--- +"@graphql-codegen/typescript-graphql-request": patch +--- + +pass variables to wrapper function