Appsync 请求 headers 如何传递给 child 解析器?
How can Appsync request headers by passed to a child resolver?
我有一个 Appsync 架构,它定义了一个用户类型,它本身包含一个注释列表:
type User {
id: ID!
name: String!
notes: [Note]
}
type Note {
id: ID!
text: String!
}
在 User
的请求映射中,我能够从请求访问 headers,以访问 Bearer 令牌:
{
"version": "2018-05-29",
"method": "GET",
"resourcePath": $util.toJson("/prod/user/$ctx.args.userId"),
"params":{
"headers":{
"Content-Type": "application/json",
"Authorization": "Bearer $ctx.request.headers.Authorization",
}
}
}
这很好用,header 被传递到数据源。
User.notes
字段的解析器是一个 Lambda 解析器,它本身迭代调用类似的 API GET /prod/user/{userId}/notes/{noteId}
。我可以使用 $ctx.source...
访问 Parent 解析器响应数据。但是,我还需要能够通过授权header(例如$ctx.request.headers.Authorization
)。
我已经在 User
响应映射中尝试过,但它不起作用:
#set($results = $ctx.result.body)
#set($headers = {
"Authorization": "Bearer $ctx.request.headers.Authorization"
})
#set($results.headers = $headers)
$results
我的问题是,如何将请求 headers 从 Parent 解析器传递到 Child 解析器?
也许另一种问这个问题的方法是,假设我在 Graphql 请求上有一个 Bearer 令牌 header,我如何确保它总是传递给任何被调用的解析器?
正如@Myz 在他们的评论中提到的那样,解决方案是使用管道解析器。
最后,我将 header 推入 $ctx.stash
object(它可用于管道解析器中的所有函数)——我这样做是使用:
$util.qr($ctx.stash.put("bearerToken", "Bearer $ctx.request.headers.Authorization"))
然后我可以使用以下方法在每个函数(即 child 解析器)中访问该值:"$ctx.stash.bearerToken"
添加备注user_id
type Note {
id: ID!
user_id: ID!
text: String!
}
并解决注释:[注释] 作者 $ctx.source.id
在 postgres 这个应用
{
"version": "2018-05-29",
"statements": [
"select * from notes where user_id = '$ctx.source.id'"
]
}
其中 $ctx.source.id link 到用户 -> id
我有一个 Appsync 架构,它定义了一个用户类型,它本身包含一个注释列表:
type User {
id: ID!
name: String!
notes: [Note]
}
type Note {
id: ID!
text: String!
}
在 User
的请求映射中,我能够从请求访问 headers,以访问 Bearer 令牌:
{
"version": "2018-05-29",
"method": "GET",
"resourcePath": $util.toJson("/prod/user/$ctx.args.userId"),
"params":{
"headers":{
"Content-Type": "application/json",
"Authorization": "Bearer $ctx.request.headers.Authorization",
}
}
}
这很好用,header 被传递到数据源。
User.notes
字段的解析器是一个 Lambda 解析器,它本身迭代调用类似的 API GET /prod/user/{userId}/notes/{noteId}
。我可以使用 $ctx.source...
访问 Parent 解析器响应数据。但是,我还需要能够通过授权header(例如$ctx.request.headers.Authorization
)。
我已经在 User
响应映射中尝试过,但它不起作用:
#set($results = $ctx.result.body)
#set($headers = {
"Authorization": "Bearer $ctx.request.headers.Authorization"
})
#set($results.headers = $headers)
$results
我的问题是,如何将请求 headers 从 Parent 解析器传递到 Child 解析器? 也许另一种问这个问题的方法是,假设我在 Graphql 请求上有一个 Bearer 令牌 header,我如何确保它总是传递给任何被调用的解析器?
正如@Myz 在他们的评论中提到的那样,解决方案是使用管道解析器。
最后,我将 header 推入 $ctx.stash
object(它可用于管道解析器中的所有函数)——我这样做是使用:
$util.qr($ctx.stash.put("bearerToken", "Bearer $ctx.request.headers.Authorization"))
然后我可以使用以下方法在每个函数(即 child 解析器)中访问该值:"$ctx.stash.bearerToken"
添加备注user_id
type Note {
id: ID!
user_id: ID!
text: String!
}
并解决注释:[注释] 作者 $ctx.source.id 在 postgres 这个应用
{
"version": "2018-05-29",
"statements": [
"select * from notes where user_id = '$ctx.source.id'"
]
}
其中 $ctx.source.id link 到用户 -> id