如何将graphql Union 从python lambda 返回到AWS appsync?

Joh*_*Mee 8 python aws-appsync aws-serverless

是否可以Union从 python lambda用 graphql 进行响应?如何? 这似乎是可能的,但我无法超越它。

我包含了一个__typename属性,但大多数情况下我都会收到此错误:

{'errorType': 'BadRequestException', 
 'message': "Could not determine the exact type of MealMix. Missing __typename key on value.'"}
Run Code Online (Sandbox Code Playgroud)

MealMix 是我的工会,架构如下所示:

type Meal { name: String }
type OtherMeal { name: String }
union MealMix = Meal | OtherMeal
type Query {
    GetMealMix(identity_id: ID!, date: AWSDate!): [MealMix]
}
Run Code Online (Sandbox Code Playgroud)

查询是:

        query test_query {
            GetMealMix(identity_id: "this", date: "2020-11-20") {
                ... on Meal {name}
                ... on OtherMeal {name}
            }
        }
Run Code Online (Sandbox Code Playgroud)

我从 lambda 返回:

return [{' __typename': 'Meal',
         'name': 'Kiwifruit, Zespri Gold, Raw'},
        {' __typename': 'OtherMeal',
         'name': 'The meal at the end of the universe'}]
Run Code Online (Sandbox Code Playgroud)

响应模板是默认的: $util.toJson($ctx.result)

我的谷歌搜索似乎表明我只需要包含该__typename属性,但没有明确的 Python 示例。我首先问,我是在鞭打死马吗(意味着这没有实施并且永远不会奏效),其次,如果它确实有效,具体如何?

小智 -1

您可以按照下面的方式编写。它应该对你有用。

schema {
    query: Query
}

type Query {
    GetMealMix(identity_id: ID!, date: AWSDate!): [MealMix]
}

union MealMix = Meal | OtherMeal

type Meal {
    name: String
}

type OtherMeal {
    name: String
}

query {
    GetMealMix(identity_id: "this", date: "2020-11-20") {
        ... on Meal {name}
        ... on OtherMeal {name}
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以参考以下网址: GraphQL 中的接口和联合