在nextjs中使用getserversideprops进行graphql-codegen突变
我想在getServersideProps
中执行GraphQl突变。我正在使用GraphQL-Codegen从我的GraphQl调用中生成代码。我用来执行突变的代码是:
const [getTokenMutation, _] = useGetTokenMutation()
const token = await getTokenMutation({
当我将工作代码从函数主体移动到getserversideprops
时,我会得到错误:错误:无效挂钩调用。钩子只能在功能组件的主体内部调用。这可能是出于以下原因之一...
解决此问题,我将其视为graphql-codegen-apollo-next-ssr
graphql-codegen的插件。不幸的是,这仅是为我定义的查询而不是为突变生成代码。
如何从GetServersideProps中执行突变?
I would like to perform a graphql mutation in getServerSideProps
. I am using graphql-codegen to generate code from my graphql call. The code I use to perform my mutation is:
const [getTokenMutation, _] = useGetTokenMutation()
const token = await getTokenMutation({
When I move the working code from the function body to getServerSideProps
I get the error: Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons ...
To solve this I looked in to the graphql-codegen-apollo-next-ssr
plugin for graphql-codegen. Unfortunately this only is generating code for the queries I have defined, not for the mutations.
How can I perform my mutation from getServerSideProps?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
服务器端功能是节点 - 不反应,因此您不能在这些功能中使用React Hook。
您需要在每个功能中调用Apollo客户端查询/突变。 next.js有多个阿波罗示例 - 这是一个。
apollo-client.js
somepage.js
The server-side function are Node - not React, so you can't use React hooks in those functions.
You need to call the Apollo client query/mutation in each function. Next.js has multiple Apollo examples - here is one.
apollo-client.js
SomePage.js