AWS AppSync GraphQL - 使用联合作为突变 return 导致错误
AWS AppSync GraphQL - using union as mutation return causes error
我正在使用 AWS AppSync 创建 GraphQL 架构,我想使用 Union
作为变更 return。我想这样写突变:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values {
... on SteppedTariff {
endDate
}
... on SeasonalTimeTariff {
endDate
peakConsumption
}
}
}
}
但是我收到这个错误:
Request failed with status code 400
字段 values
可以是 SteppedTariff
或 SeasonalTimeTariff
类型,取决于 addUpdateTariff
突变的输入。据我搜索,Union
仅用于查询,我没有找到一些文档告诉它不能以不同的方式使用。
我是不是遗漏了什么或者我真的不能这样使用 Union
?
架构:
type Tariff {
id: ID!
type: TariffType!
values: [TariffValue!]
}
type SteppedTariff {
endDate: AWSDate
}
type SeasonalTimeTariff {
endDate: AWSDate
peakConsumption: Float
}
union TariffValue = SeasonalTimeTariff | SteppedTariff
测试关税值的不同模式:
values: [String]
查询:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values
}
}
响应:
{
"data": {
"addUpdateTariff": {
"id": "843eadcf-48bd-4d58-93ec-8bbe96db3635",
"type": "SeasonalTime",
"values": [
"{endDate=2019-02-02}"
]
}
}
}
values: [SeasonalTimeTariff]
查询:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values {
endDate
}
}
}
响应:
{
"data": {
"addUpdateTariff": {
"id": "7f77c5d9-2b06-4bb8-a678-10996addc4e1",
"type": "SeasonalTime",
"values": [
{
"endDate": "2019-02-02"
}
]
}
}
}
您绝对可以在 return 类型上使用 Union 来实现突变。确保在您的值中包含字段 __typename
。您的解析器模板应该 return 像这样:
{
"id": 1,
"type": "?",
"values": [
{
"__typename": "StepTariff",
"endDate": "2019-01-01"
},
{
"__typename": "SeasonalTimeTariff",
"endDate": "2019-01-01",
"peakConsumption": 1.0
}
]
}
我正在使用 AWS AppSync 创建 GraphQL 架构,我想使用 Union
作为变更 return。我想这样写突变:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values {
... on SteppedTariff {
endDate
}
... on SeasonalTimeTariff {
endDate
peakConsumption
}
}
}
}
但是我收到这个错误:
Request failed with status code 400
字段 values
可以是 SteppedTariff
或 SeasonalTimeTariff
类型,取决于 addUpdateTariff
突变的输入。据我搜索,Union
仅用于查询,我没有找到一些文档告诉它不能以不同的方式使用。
我是不是遗漏了什么或者我真的不能这样使用 Union
?
架构:
type Tariff {
id: ID!
type: TariffType!
values: [TariffValue!]
}
type SteppedTariff {
endDate: AWSDate
}
type SeasonalTimeTariff {
endDate: AWSDate
peakConsumption: Float
}
union TariffValue = SeasonalTimeTariff | SteppedTariff
测试关税值的不同模式:
values: [String]
查询:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values
}
}
响应:
{
"data": {
"addUpdateTariff": {
"id": "843eadcf-48bd-4d58-93ec-8bbe96db3635",
"type": "SeasonalTime",
"values": [
"{endDate=2019-02-02}"
]
}
}
}
values: [SeasonalTimeTariff]
查询:
mutation addUpdateTariff($tariff: TariffInput!, $seasonalTimeTariff: [SeasonalTimeTariffInput!]) {
addUpdateTariff(tariff: $tariff, seasonalTimeTariff: $seasonalTimeTariff) {
id
type
values {
endDate
}
}
}
响应:
{
"data": {
"addUpdateTariff": {
"id": "7f77c5d9-2b06-4bb8-a678-10996addc4e1",
"type": "SeasonalTime",
"values": [
{
"endDate": "2019-02-02"
}
]
}
}
}
您绝对可以在 return 类型上使用 Union 来实现突变。确保在您的值中包含字段 __typename
。您的解析器模板应该 return 像这样:
{
"id": 1,
"type": "?",
"values": [
{
"__typename": "StepTariff",
"endDate": "2019-01-01"
},
{
"__typename": "SeasonalTimeTariff",
"endDate": "2019-01-01",
"peakConsumption": 1.0
}
]
}