问题描述
我在一个EC2实例上运行一台graphql服务器.我也正在运行AWS appsync,但目前仅与几个lambda集成.
I have a graphql server on one of my EC2 instances running. I also have AWS appsync running, but at the moment it's integrated only with couple of lambdas.
我想将Appsync与graphql服务器连接,因此Appsync将充当特定查询/突变的代理.
I'd like to connect my Appsync with graphql server, so Appsync will behave as a proxy for specific queries/mutations.
所以从客户端看,它看起来像这样:
So from client side, it will look like this:
- 客户端向appsync发送查询,可以说它看起来像这样:
{
user {
id
}
}
- Appsync定义了一个
user
查询,它已配置为代理该查询的graphql服务器,而无需进行任何更改 - Graphql服务器能够处理以下查询:
- Appsync has defined a
user
query, it's configured to proxy the query the graphql server, without any changes - Graphql server is able to handle following query:
{
user {
id
}
}
并返回结果:
"data": {
"user": {
"id": "123456789"
}
}
- 最终,Appsync代理将响应返回给客户端
我能够以给定方案可能的方式配置Appsync吗?是使用Appsync的正确模式吗?
Am I able to configure Appsync in a way that given scenario is possible? Is it the right pattern to use Appsync for?
我能够通过AppSync代理我的请求,并使用以下解析程序配置到达我的graphql服务器:
I was able to proxy my request through AppSync, and reach my graphql server, with following resolver configuration:
{
"version": "2018-05-29",
"method": "POST",
"resourcePath": "/graphql",
"params":{
"body": {
"query": "$util.escapeJavaScript($ctx.info.getSelectionSetGraphQL())"
}
}
}
但这仍然无法按预期工作-与文档,我至少看到了两个问题:
But this still does not work as expected - it's different from description in docs, and I see at least two issues:
- 如果我有一个带有参数和嵌套薪水的查询,
$ctx.info.getSelectionSetGraphQL()
会剪掉查询名称和参数部分,只给我嵌套有效载荷,所以此查询:
- If I have a query with arguments and nested paylod,
$ctx.info.getSelectionSetGraphQL()
cuts out the query name and arguments part, and gives me only nested payload, so this query:
{
user(id: "1") {
picture {
url
}
}
一旦我打$ctx.info.getSelectionSetGraphQL()
,就会变成以下一个:
becomes following one, once I call $ctx.info.getSelectionSetGraphQL()
:
{
picture {
url
}
}
但是我想使用整个查询,与文档中所述的相同:
But I'd like to use whole query, same as described in docs:
"selectionSetGraphQL": "{\n getPost(id: $postId) {\n postId\n title\n secondTitle: title\n content\n author(id: $authorId) {\n authorId\n name\n }\n secondAuthor(id: \"789\") {\n authorId\n }\n ... on Post {\n inlineFrag: comments {\n id\n }\n }\n ... postFrag\n }\n}"
- 可以说我有一个查询,并且我已经在appsync中为
user
查询定义了解析器,该查询看起来像这样:
- Lets say I have a query, and I've defined resolver for
user
query in appsync, the query looks like this:
{
user(id: "1") {
picture {
url
}
}
我通过appsync调用了我的graphql后端,在我的graphql日志中,我看到记录了以下响应:
and I call my graphql backend through appsync, in my graphql logs I see following response logged:
{
"data: {
"user": {
"picture": {
"url": "example.com/link/to/a/picture"
}
}
}
}
但是appsync会返回给定的响应给我
but appsync returns me given response instead:
{
"data: {
"user": {
"picture": null
}
}
}
我已经知道,appsync期望我为user
和picture
查询定义解析器. Appsync希望先调用user
,然后再调用picture
查询,但是我想做的只是通过appsync代理响应,而不调用除user
之外的任何其他查询.一切都在我的graphql后端中进行了评估,应该放在响应主体中,但是appsync希望再评估一次.
I've figured out, that appsync expects from me that I will define resolvers for both user
and picture
query. Appsync wants to first call user
and then picture
query, but what I wanna do is simply proxy the response through appsync, without calling any additional queries except user
. Everything is evaluated in my graphql backend and should be just put to the response body, but appsync wants to evaluate everything one more time.
是否有针对1.和2.的解决方案?
Is there any solution for no 1. and 2.?
推荐答案
撰写本文时,您可以使用$ctx.info
对象从解析器中获取查询的选择集,并通过HTTP解析器将相关数据发送到您的下游服务. 转到此处并查找在$ ctx对象上的信息"字段中.为此,您需要在AppSync API中镜像下游API的架构.
As of writing, you can use the $ctx.info
object to get the selection set of queries from within the resolvers and send the relevant data via an HTTP resolver to your downstream service. Go here and look for the info field on the $ctx object. To make this work, you will need to mirror the schema of your downstream API within your AppSync API.
感谢您提出来.团队知道这些用例,这有助于确定优先级.
Thanks for bringing this up. The team is aware of these use cases and this helps prioritization.
这篇关于通过AWS Appsync对graphql服务器的代理请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!