问题描述
我有一个现有的GraphQL API,它公开了这样的模式:
type AType {
id: Int!
name: String!
children: [BType]
}
type BType {
id: Int!
name: String!
}
type Query {
a(id: ID): A
as: [A]
}
以及两者中的其他一些省略的标量字段。有几个这样的API端点,我想将它们全部聚集在一个AppSync实例下,以便前端可以使用单个架构查询一个端点以获取它们所需的所有数据。
我想做的是如果前台发送查询
as {
id,name,children {
id,name
}
}
它将完全中继到我现有的GraphQL端点。在AppSync页面上链接的AWS示例讨论了GraphQL端点,但唯一的链接代码示例是this,它显示了如下解析器:
#**
Make an arbitrary HTTP call when this field is listed in a query selection set.
The "relativePath" is the resource path relative to the root URL provided when you
created the HTTP data source. Pass a "params" object to forward headers,body,and query parameters to the HTTP endpoint.
*#
#if($context.identity.sub == $context.args.userId)
#set($payload = "query ListOrders {
listOrders(
userId: ""$context.args.userId""
orderDateTimeStatus: {
le : {
orderDateTime: ""$context.args.orderDateTime""
}
}
limit: 10
) {
items {
userId
status
orderDateTime
details
orderId
}
nextToken
}
}")
{
"version": "2018-05-29","method": "POST","resourcePath": "/graphql","params":{
"body": {
"query": "$util.escapeJavaScript($payload)"
},"headers":{
"Content-Type": "application/json","x-access-token" : "$context.request.headers.x-access-token"
}
}
}
#else
$utils.unauthorized()
#end
这仅使用传递给查询的参数,而不使用查询结构。如果front请求A
的子集,我只想请求该子集,而不是对所有随后被AppSync删除的请求。而且它甚至不包含嵌套的对象-我是否每次都必须获取所有子对象,以便稍后被AppSync忽略?
假设有4-5个微服务具有自己的GraphQL端点,这些端点在其架构中具有不同的查询。我想将请求1-1映射到它们。我将如何编写这样的解析器? AppSync确实没有一个好的操场环境,我可以在其中调试解析器,因此我不能只看$ctx
对象并对原始查询(AFAIK)的结构进行反向工程。
我发现this question只是问了这个问题,但是正如作者提到的那样,那里的解决方案无法按预期工作,而且似乎已经死了。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)