0

trying to add Authorization header to GraphQL requests, following instructions from https://formidable.com/open-source/urql/docs/advanced/authentication/

type AuthState = {
  token:string
};


return authExchange<AuthState>({

 addAuthToOperation: ({ authState, operation }) => {
  if (!authState || !authState.token) {
    return operation;
  }

  const fetchOptions =
    typeof operation.context.fetchOptions === 'function'
      ? operation.context.fetchOptions()
      : operation.context.fetchOptions || {};

  //
  // complains at the following line.
  //        
  return makeOperation(
    operation.kind, 
    operation, {
    ...operation.context,
    fetchOptions: {
      ...fetchOptions,
      headers: {
        ...fetchOptions.headers,
        Authorization: authState.token,
      },
    },
  });
},   

})

Getting the following compile error, unable to figure out what i could be doing wrong.

(alias) makeOperation<any, AnyVariables>(kind: OperationType, request: GraphQLRequest<any, AnyVariables>, context: OperationContext): Operation<...> (+1 overload)
import makeOperation
No overload matches this call.
  Overload 1 of 2, '(kind: OperationType, request: GraphQLRequest<any, AnyVariables>, context: OperationContext): Operation<any, AnyVariables>', gave the following error.
    Argument of type '{ fetchOptions: { headers: { Authorization: string; length: number; toString(): string; toLocaleString(): string; pop(): [string, string]; push(...items: [string, string][]): number; concat(...items: ConcatArray<...>[]): [...][]; concat(...items: ([...] | ConcatArray<...>)[]): [...][]; ... 28 more ...; [Symbol.unsco...' is not assignable to parameter of type 'OperationContext'.
      Types of property '_instance' are incompatible.
        Type 'OperationInstance' is not assignable to type '[]'.
  Overload 2 of 2, '(kind: OperationType, request: Operation<any, AnyVariables>, context?: OperationContext): Operation<any, AnyVariables>', gave the following error.
    Argument of type 'import("/Users/vineelnalla/work/ledgers/web/node_modules/@urql/exchange-auth/node_modules/@urql/core/dist/types/types").Operation<any, import("/Users/vineelnalla/work/ledgers/web/node_modules/@urql/exchange-auth/node_modules/@urql/core/dist/types/types").AnyVariables>' is not assignable to parameter of type 'import("/Users/vineelnalla/work/ledgers/web/node_modules/@urql/core/dist/types/types").Operation<any, import("/Users/vineelnalla/work/ledgers/web/node_modules/@urql/exchange-auth/node_modules/@urql/core/dist/types/types").AnyVariables>'.
      The types of 'context._instance' are incompatible between these types.
        Type 'OperationInstance' is not assignable to type '[]'.ts(2769)
No quick fixes available

I am using the following versions of urql

"@urql/devtools": "^2.0.3",
"@urql/exchange-auth": "^1.0.0",
"@urql/exchange-graphcache": "^4.4.3",
"urql": "^2.2.3",

1 Answers1

0

the issue was with mismatched versions of urql. its now fixed once i got them to the latest.

"urql": "^3.0.3",