我应该运行“ apolloserver.executeoperation”在Next; s" s&quort;

发布于 2025-01-24 16:17:01 字数 844 浏览 1 评论 0原文

我正在尝试找出在Next.js应用程序的getserversideprops中运行GraphQl查询的最佳方法。

由于GraphQl Server在Next.js实例上运行,因此明显的解决方案是在我的页面的getserversideprops函数上运行apolloserver.executeoperation()

但是,我担心此解决方案,因为:

  1. 文档ececuteOperation 将其描述为集成测试功能。它的字面意思是“ ececuteOperation方法为通过请求管道运行操作提供了单个挂钩,从而在不启动HTTP服务器的情况下实现了最彻底的测试。 ”。但这并不是说它应该仅用于测试。
  2. 我在网上找到有关在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:

  1. 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.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞陪衬 2025-01-31 16:17:01

我们在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 the Home 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文