我应该运行“ apolloserver.executeoperation”在Next; s" s&quort;
我正在尝试找出在Next.js应用程序的getserversideprops
中运行GraphQl查询的最佳方法。
由于GraphQl Server在Next.js实例上运行,因此明显的解决方案是在我的页面的getserversideprops
函数上运行apolloserver.executeoperation()
。
但是,我担心此解决方案,因为:
- 文档
ececuteOperation
将其描述为集成测试功能。它的字面意思是“ ececuteOperation方法为通过请求管道运行操作提供了单个挂钩,从而在不启动HTTP服务器的情况下实现了最彻底的测试。 ”。但这并不是说它应该仅用于测试。 - 我在网上找到有关在Next.js上运行GraphQl的几乎所有在线指南。 -Next-JS-AC0B2E3E461“ rel =“ nofollow noreferrer”>示例)。但是,与我的GraphQl Server在同一家服务器上运行Apollo客户端似乎是显而易见的不必要的开销。
这使我认为我可能错过了明显的东西。
可以在我的next.js getServersideProps
上调用apolloserver.executeoperation
吗?
I'm trying to figure out the best way of running GraphQL queries in the getServerSideProps
of a Next.js app.
Since the GraphQL server is running on the same Next.js instance, the obvious solution is to run apolloServer.executeOperation()
on the getServerSideProps
function of my pages.
However, I'm worried about this solution because:
- The documentation of
executeOperation
describes it as an integration tests function. It literally says "The executeOperation method provides a single hook to run operations through the request pipeline, enabling the most thorough tests possible without starting up an HTTP server.". But it doesn't say it should only be used for testing. - Pretty much all online Guides I find online about running GraphQL on Next.js says I should use an apollo client (Example). However, running an Apollo Client on the same server as my GraphQL server seems like an obvious unnecessary overhead.
Which leads me to think I maybe missing something obvious.
Is it OK to call apolloServer.executeOperation
on my Next.js getServerSideProps
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们在
getStaticProps
上运行基本的async获取,在我们的下一个应用中,格式的响应将传递给home
组件,并用于设置Redux Store。我想,如果您要执行GraphQL请求,则需要在使用GraphQl客户端之前启动graphQl客户端 - 这是在呼叫链中为我们发生的,我想您。您可能可以执行GQL客户端设置服务器端,然后通过道具将对象传递到家里,但似乎并不是预期的使用。
我会说,如果您需要使用GQL服务器侧请求,请创建客户端GetServersideProps并在请求后将其关闭,请不要看到很多问题。
We run a basic async fetch on the
getStaticProps
, in our Next app, a formatted response gets passed to theHome
component and used to setup the redux store.I imagine that if you were to do a graphql request you would need to init the graphql client before you can use it - which happens later in the call chain for us, and i imagine you. You could maybe do your GQL client setup server side and pass the object by props to Home, but doesn't seem like thats the intended use.
I'd say if you need to server side request with GQL, create a client getServerSideProps and close it after your request, don't see much of an issue with that.