尝试将Authorization标头添加到GraphQL请求,遵循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,
},
},
});
},
})
出现以下编译错误,无法弄清楚我可能做错了什么。
(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
"@urql/devtools": "^2.0.3",
"@urql/exchange-auth": "^1.0.0",
"@urql/exchange-graphcache": "^4.4.3",
"urql": "^2.2.3",
问题在于urql版本不匹配。一旦我把它们更新到最新版本,它就被修复了。
"urql": "^3.0.3",