AppSync作为另一台GraphQl Server的代理

发布于 2025-01-31 11:20:09 字数 1190 浏览 4 评论 0原文

我有第三方为第三方提供的现有GraphQL服务器。我也有自己在EC2上运行的后端以提供API。

我正在尝试使用 aws-cdk 构建AppSync,以连接到第三方GraphQl和我的后端实例。 使用GraphQL Server,AppSync将充当仅转发查询的代理。我的问题是:

  1. 无论如何我们是否有加载远程模式并将其填充在AppSync及其模式?

  2. 如何使用AWS-CDK将请求转发到另一台GraphQl Server?我正在尝试这样的事情:

  private get _requestMappingTemplate(): string {
    return `
        {
          "version": "2018-05-29",
          "method": "GET",
          "resourcePath": $util.toJson("/graphql"),
          "params": {
            "headers": {
              "Authorization": "Bearer $ctx.request.headers.Authorization"
            },
            "body": {
                "query": "$util.escapeJavaScript($ctx.info.getSelectionSetGraphQL())"
            }
          }
        }`;
  }

但是从AWS doc > getSelectionsEtgraphql 返回选择集的字符串表示,格式为GraphQl Schema定义语言(SDL)。尽管没有将片段合并到选择集中,

是否可以设置AppSync以将请求转发到另一个GraphQl服务器?有什么最佳做法要遵循吗?

I have a existing graphql server provied by 3rd party. I also have my own backend running on EC2 to provide APIs.

I'm trying to build the appsync with aws-cdk for connecting to both 3rd party graphql and my backend instance also.
With the graphql server, appsync will act as proxy to forward queries only. My questions are:

  1. Do we have anyway to load remote schema and populate it in appsync along with its schema?

  2. How can we forward the requests to another graphql server using aws-cdk? I'm trying something like this:

  private get _requestMappingTemplate(): string {
    return `
        {
          "version": "2018-05-29",
          "method": "GET",
          "resourcePath": $util.toJson("/graphql"),
          "params": {
            "headers": {
              "Authorization": "Bearer $ctx.request.headers.Authorization"
            },
            "body": {
                "query": "$util.escapeJavaScript($ctx.info.getSelectionSetGraphQL())"
            }
          }
        }`;
  }

But from the aws doc, getSelectionSetGraphQL returns string representation of the selection set, formatted as GraphQL schema definition language (SDL). Although fragments aren't merged into the selection set

Is that possible to setup AppSync for forwarding request to another GraphQL servers? Any best practice to follow?

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

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

发布评论

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

评论(1

养猫人 2025-02-07 11:20:09

这要复杂得多。我仍在努力,到目前为止,我得到的最好的东西在下面给出。它仍然会删除查询参数,因此使用有限。

#* TODO: Add some more interesting info to the operation name, e.g. a timestamp *#

#set($operationName = $context.info.parentTypeName)

#set($payloadBody = {
"query": "$util.str.toLower($context.info.parentTypeName) $operationName { $context.info.fieldName $context.info.selectionSetGraphQL }",
"operationName": $operationName,
"variables": $context.info.variables
})

{
"version" : "2018-05-29",
"operation": "Invoke",
"payload":{
    "path": "/graphql",
    "httpMethod": "POST",
    "headers": $util.toJson($ctx.request.headers),
    "requestContext": {
      "authorizer": {
        "claims": $context.identity.claims
      }
    },
    "body": "$util.escapeJavaScript($util.toJson($payloadBody))",
    "isBase64Encoded": false
  },
}

It's quite a bit more complicated. I'm still working on it, and the best I got so far is given below. It still drops query arguments, so has limited use.

#* TODO: Add some more interesting info to the operation name, e.g. a timestamp *#

#set($operationName = $context.info.parentTypeName)

#set($payloadBody = {
"query": "$util.str.toLower($context.info.parentTypeName) $operationName { $context.info.fieldName $context.info.selectionSetGraphQL }",
"operationName": $operationName,
"variables": $context.info.variables
})

{
"version" : "2018-05-29",
"operation": "Invoke",
"payload":{
    "path": "/graphql",
    "httpMethod": "POST",
    "headers": $util.toJson($ctx.request.headers),
    "requestContext": {
      "authorizer": {
        "claims": $context.identity.claims
      }
    },
    "body": "$util.escapeJavaScript($util.toJson($payloadBody))",
    "isBase64Encoded": false
  },
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文