我正在使用Next.js版本 9.3.0 和GraphQL. 为了获得 Next.js 优化的好处,我们将每个页面包装在 中withApollo,因此在页面内部我们可以使用useQuery, useMutation。我面临的问题是我需要在Header component页面外部使用突变,ApolloClient因为应用程序未包含在withApollo.
我得到的错误是这个Could not find "client" in the context or passed in as an option. Wrap the root component in an <ApolloProvider>, or pass an ApolloClient instance in via options.
Header组件在组件内部,如下Layout所示:
<>
<Meta />
<Header/>
{children} // pages which are composed with withApollo
<Footer />
</>
而不是在没有useQuery的组件中使用很容易,您可以使用 withApollo
import { createApolloFetch } from 'apollo-fetch';
const fetch = createApolloFetch({
uri: process.env.GRAPHQL_SERVER,
});
const userInfoQuery = `{
userInfo {
loggedIn
basketCount
wishListCount
}
}`;
const userInfoData = fetch({
query: userInfoQuery,
});
useMutation在不是由 组成的组件中是否有替代解决方案withApollo?
欢迎任何建议,干杯