Is it possible to respond with graphql Union from a python lambda? How? It seems possible but I cannot get it over the line.
I am including a __typename attribute but most of the time I get this error:
{'errorType': 'BadRequestException',
'message': "Could not determine the exact type of MealMix. Missing __typename key on value.'"}
MealMix is my union, and the schema looks like:
type Meal { name: String }
type OtherMeal { name: String }
union MealMix = Meal | OtherMeal
type Query {
GetMealMix(identity_id: ID!, date: AWSDate!): [MealMix]
}
The query is:
query test_query {
GetMealMix(identity_id: "this", date: "2020-11-20") {
... on Meal {name}
... on OtherMeal {name}
}
}
From the lambda I am returning:
return [{' __typename': 'Meal',
'name': 'Kiwifruit, Zespri Gold, Raw'},
{' __typename': 'OtherMeal',
'name': 'The meal at the end of the universe'}]
The response template is the default: $util.toJson($ctx.result)
My googling seems to suggest I just need to include the __typename attribute, but there are no explicit Python examples to that effect. I'm asking first, am I flogging a dead horse (meaning this is not implemented and will never work), and second, if it does work, how exactly?
On one hand, I found examples where __typename, as you reference, may need to be included in your query.
On the other hand, It's also very interesting, because the AWS documentation mentions nothing about including __typename in your query, but does mention the use of interfaces so I wonder if that's the key to getting things working, that all types extend the same interface.
Try including __typename within Meal and OtherMeal fragments (you mentioned using __typename in your queries, but not sure where you were putting it).
query test_query {
GetMealMix(date: "2020-11-20") {
... on Meal {
__typename
name
}
... on OtherMeal {
__typename
name
}
}
}
All types included in union use the same interface, as demonstrated in a section of the documentation you shared titled "Type resolution example"
interface MealInterface { name: String }
type Meal implements MealInterface { name: String }
type OtherMeal implements MealInterface { name: String }
You return a hardcoded response, but I'm unsure if additional metadata is needed to process the GQL response on AWS Lambda. Try logging the response to determine if __typename is included. If __typename is not included, consider adding typename to id and use transformation recommended in the AWS documentation you shared:
#foreach ($result in $context.result)
## Extract type name from the id field.
#set( $typeName = $result.id.split("-")[0] )
#set( $ignore = $result.put("__typename", $typeName))
#end
$util.toJson($context.result)
Also, "this" in "identity_id" parameter may be causing caching issues due to the way GraphQL handles ID types (see: https://graphql.org/learn/schema/#scalar-types)
GraphQL __typename query example on AWS: https://github.com/LambdaSharp/AppSync-Challenge GraphQL Scalar types: https://graphql.org/learn/schema/#scalar-types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With