graphql-request icon indicating copy to clipboard operation
graphql-request copied to clipboard

Correct types for calling request with generics

Open GimpMaster opened this issue 1 year ago • 0 comments

I'm trying to debug a problem with @graphql-codegen/typescript-react-query

GraphQL - Codegen would call with typescript the request method like this

function fetcher<TData, TVariables>(client: GraphQLClient, query: string, variables: TVariables, headers: RequestInit['headers']) {
  return async (): Promise<TData> => client.request<TData, TVariables>(query, variables, headers);
}

This worked fine in 4.X.X versions, but in 5.0.0 it fails with the following error:

Error:(12, 79) TS2345: Argument of type '[TVariables | undefined, HeadersInit | undefined]' is not assignable to parameter of type 'TVariables extends Record<any, never> ? [variables?: TVariables | undefined, requestHeaders?: HeadersInit | undefined] : keyof RemoveIndex<TVariables> extends never ? [variables?: ...] : [variables: ...]'.

I'm having a hard time trying to figure out the correct combination of typescript magic to get the generics to pass in appropriately.

Any guidance so I can submit a PR to the codegen team would be MUCH welcomed.

Thanks

GimpMaster avatar Sep 10 '22 00:09 GimpMaster