I have an Appsync Schema which defines a User type which itself contains a List of Notes:
type User {
id: ID!
name: String!
notes: [Note]
}
type Note {
id: ID!
text: String!
}
In the request mapping for the User, I am able to access the headers from the request, to access the Bearer token:
{
"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",
}
}
}
This works well and the header is passed to the data source.
The resolver for the User.notes field is a Lambda resolver which itself iteratively calls a similar API GET /prod/user/{userId}/notes/{noteId}. I am able to access the Parent resolvers response data using $ctx.source.... However, I also need to be able to pass through the Authorization header (e.g. $ctx.request.headers.Authorization).
I have tried this in the User response mapping, but it does not work:
#set($results = $ctx.result.body)
#set($headers = {
"Authorization": "Bearer $ctx.request.headers.Authorization"
})
#set($results.headers = $headers)
$results
My question is, how can I pass the request headers from a Parent resolver to a Child resolver? Perhaps another way to ask the question would be, given I have a Bearer token on the Graphql request header, how can I ensure it is always passed to any resolvers which are called?
ctx.request.headers.Authorizationtin your child resolver? Becauseresultsfield is only available to response mapping template. - Myzctx.requestis emptied before the child resolver is invoked. As a test I injected the entire$ctxinto the lambda function so I could log it out and there is nothing in there which contains the headers. This makes me think I'd need to do something with the parent resolver to cache the headers? But I don't know if that possible - stang