GraphQL 错误 FieldsConflict:字段具有不同的列表形状
GraphQL error FieldsConflict: fields have different list shapes
我正在使用具有以下(简化)架构的 AWS AppSync 的 GraphQL 服务器:
type Query {
getIssue(id: String!): Issue
}
type Issue {
id: String!
data: IssueData!
}
type Event {
id: String!
time: AWSDateTime!
status: [String]
}
type Payment {
id: String!
amount: Int!
status: String
}
union IssueData = Event | Payment
当我进行的查询包含 inline fragments 到 select status
作为 Event
或 Payment
类型的子项时 Issue/data
字段,我得到一个 FieldsConflict 错误:
query getIssue($id: String!) {
getIssue(id: $id) {
id
data {
... on Event {
time
status
}
... on Payment {
amount
status
}
}
}
}
Validation error of type FieldsConflict: status: fields have different list shapes @ 'getIssue/data'
这可能是由于 Event/status
字段返回一个字符串数组,而 Payment/status
字段 returns 返回一个字符串。
为什么 GraphQL 认为这是冲突?我应该如何构建我的查询以允许访问两种数据类型的状态字段?
请注意,我使用的是联合而不是扩展接口,因为 Issue
和 Payment
类型没有共同的数据结构。
来自spec:
If multiple field selections with the same response names are encountered during execution, the field and arguments to execute and the resulting value should be unambiguous. Therefore any two field selections which might both be encountered for the same object are only valid if they are equivalent.
您可以通过为一个或两个字段提供字段别名来解决此问题:
query getIssue($id: String!) {
getIssue(id: $id) {
id
data {
... on Event {
time
eventStatus: status
}
... on Payment {
amount
status
}
}
}
}
重命名架构中的一个或两个字段显然也可以解决问题。
我正在使用具有以下(简化)架构的 AWS AppSync 的 GraphQL 服务器:
type Query {
getIssue(id: String!): Issue
}
type Issue {
id: String!
data: IssueData!
}
type Event {
id: String!
time: AWSDateTime!
status: [String]
}
type Payment {
id: String!
amount: Int!
status: String
}
union IssueData = Event | Payment
当我进行的查询包含 inline fragments 到 select status
作为 Event
或 Payment
类型的子项时 Issue/data
字段,我得到一个 FieldsConflict 错误:
query getIssue($id: String!) {
getIssue(id: $id) {
id
data {
... on Event {
time
status
}
... on Payment {
amount
status
}
}
}
}
Validation error of type FieldsConflict: status: fields have different list shapes @ 'getIssue/data'
这可能是由于 Event/status
字段返回一个字符串数组,而 Payment/status
字段 returns 返回一个字符串。
为什么 GraphQL 认为这是冲突?我应该如何构建我的查询以允许访问两种数据类型的状态字段?
请注意,我使用的是联合而不是扩展接口,因为 Issue
和 Payment
类型没有共同的数据结构。
来自spec:
If multiple field selections with the same response names are encountered during execution, the field and arguments to execute and the resulting value should be unambiguous. Therefore any two field selections which might both be encountered for the same object are only valid if they are equivalent.
您可以通过为一个或两个字段提供字段别名来解决此问题:
query getIssue($id: String!) {
getIssue(id: $id) {
id
data {
... on Event {
time
eventStatus: status
}
... on Payment {
amount
status
}
}
}
}
重命名架构中的一个或两个字段显然也可以解决问题。