【发布时间】:2020-09-29 23:01:14
【问题描述】:
我正在将 NextJS 与 Apollo 一起使用。文档不多说,但指向这个 repo:https://github.com/vercel/next.js/tree/canary/examples/with-apollo
使用此代码:
export async function getStaticProps() {
const apolloClient = initializeApollo()
await apolloClient.query({
query: ALL_POSTS_QUERY,
variables: allPostsQueryVars,
})
return {
props: {
initialApolloState: apolloClient.cache.extract(),
},
unstable_revalidate: 1,
}
}
我觉得这很丑,所以我尝试使用钩子:
const res = useQuery(ALL_POSTS_QUERY);
但我得到一个错误:
错误:无效的挂钩调用。 Hooks 只能在函数组件的主体内部调用。这可能是由于以下原因之一:
那么你不能在getStaticProps 和getServerSideProps 中使用钩子吗?如果是这样,是否有更好的语法来进行 GraphQL 查询?
【问题讨论】:
-
有这个库,但它提到 getInitialProps 这不是在 Next 9 github.com/lfades/next-with-apollo中获取数据的当前方式@