【发布时间】:2020-07-12 07:16:58
【问题描述】:
我正在尝试向useMutation 明确提供客户。一切正常,除了打字稿似乎看到类型不匹配。
Type 'DefaultClient<unknown>' is not assignable to type 'ApolloClient<object>'.
客户端与apollo的token authentication documentation中显示的客户端非常相似
import { ApolloClient } from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';
const httpLink = createHttpLink({
uri: '/graphql',
});
const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = localStorage.getItem('token');
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
}
}
});
export const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
});
我真正要做的就是导入客户端并将其作为选项提供给 useMutation。
import {useMutation} from '@apollo/react-hooks'
import {client} from './client'
const MY_QUERY = `
...
`
const [myQuery] = useMutation(MY_QUERY, {
client: client,
onCompleted: () => {
// do some stuff..
}
})
useMutation 似乎期望推断出的类型是另一种类型。我该如何解决这种不匹配?
【问题讨论】:
标签: typescript apollo react-apollo react-apollo-hooks