I'm creating a GraphQL schema using AWS AppSync and I want to use Union as mutation return. I'd like to write mutation this way:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values {
... on SteppedTariff {
endDate
}
... on SeasonalTimeTariff {
endDate
peakConsumption
}
}
}
}
But I'm getting this error:
Request failed with status code 400
The field values can be of the type SteppedTariff or SeasonalTimeTariff, depends the inputs of addUpdateTariff mutation. As far as I've searched, Union is only used in queries and I didn't find some documentation telling it cannot be used in a different way.
Am I missing something or I really cannot use Union this way?
Schema:
type Tariff {
id: ID!
type: TariffType!
values: [TariffValue!]
}
type SteppedTariff {
endDate: AWSDate
}
type SeasonalTimeTariff {
endDate: AWSDate
peakConsumption: Float
}
union TariffValue = SeasonalTimeTariff | SteppedTariff
Testing different schemas for Tariff values:
values: [String]Query:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values
}
}
Response:
{
"data": {
"addUpdateTariff": {
"id": "843eadcf-48bd-4d58-93ec-8bbe96db3635",
"type": "SeasonalTime",
"values": [
"{endDate=2019-02-02}"
]
}
}
}
values: [SeasonalTimeTariff]Query:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values {
endDate
}
}
}
Response:
{
"data": {
"addUpdateTariff": {
"id": "7f77c5d9-2b06-4bb8-a678-10996addc4e1",
"type": "SeasonalTime",
"values": [
{
"endDate": "2019-02-02"
}
]
}
}
}
You can definitely use Unions on the return types for mutations. Make sure you are including the field __typename on your values. Your resolver template should return something like this:
{
"id": 1,
"type": "?",
"values": [
{
"__typename": "StepTariff",
"endDate": "2019-01-01"
},
{
"__typename": "SeasonalTimeTariff",
"endDate": "2019-01-01",
"peakConsumption": 1.0
}
]
}
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