使用GQLGEN的联合GraphQl上下文

发布于 2025-02-10 11:32:50 字数 54 浏览 2 评论 0原文

如何在已从联合GraphQl Gateway传递的GQLGEN子图中获取GraphQl上下文?

How do you get the graphql context in a gqlgen subgraph that has been passed down from the federated graphql gateway?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

娇纵 2025-02-17 11:32:50

您需要在网关中添加buildservice函数,该函数将JSON串起网关中的上下文,然后将其注入将发送到子图中的标头,然后在该子图中,您需要分析该标头并自己将其放在上下文中,以便您的决心可以访问它。这样的事情:

{    
  gateway: {
    buildService({ url }) {
      return new RemoteGraphQLDataSource({
        url,
        willSendRequest({ request, context }) {
          request.http.headers.set('X-GQL-Context', JSON.stringify(context))
        },
      })
    },
  },
}

You would need to add a buildService function in your gateway that json stringifies the context in the gateway and then injects that into a header that will be sent to the subgraph and then in the subgraph you will need to parse that header and put that into the context yourself so your resolves can access it. Something like this:

{    
  gateway: {
    buildService({ url }) {
      return new RemoteGraphQLDataSource({
        url,
        willSendRequest({ request, context }) {
          request.http.headers.set('X-GQL-Context', JSON.stringify(context))
        },
      })
    },
  },
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文