首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Urql身份验证: addAuthToOperation编译错误

Urql身份验证: addAuthToOperation编译错误
EN

Stack Overflow用户
提问于 2022-10-19 01:06:33
回答 1查看 12关注 0票数 0

尝试将授权头添加到GraphQL请求中,遵循https://formidable.com/open-source/urql/docs/advanced/authentication/的指示

代码语言:javascript
复制
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,
      },
    },
  });
},   

})

获取以下编译错误,无法确定我可能做错了什么。

代码语言:javascript
复制
(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

我正在使用以下版本的urql

代码语言:javascript
复制
"@urql/devtools": "^2.0.3",
"@urql/exchange-auth": "^1.0.0",
"@urql/exchange-graphcache": "^4.4.3",
"urql": "^2.2.3",
EN

回答 1

Stack Overflow用户

发布于 2022-10-19 06:23:37

问题是不匹配的版本的urql。一旦我把它们拿到最新版本,现在就修好了。

代码语言:javascript
复制
"urql": "^3.0.3",
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74118874

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档