gpt4 book ai didi

GraphQL 错误 FieldsConflict : fields have different list shapes

转载 作者:行者123 更新时间:2023-12-05 08:52:30 25 4
gpt4 key购买 nike

我正在使用具有以下(简化)架构的 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 的查询时要在 Issue/data 字段中选择 status 作为 EventPayment 类型的子项,我得到一个 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 字段返回单个字符串造成的。

为什么 GraphQL 认为这是冲突?我应该如何构建我的查询以允许访问两种数据类型的状态字段?

请注意,我使用的是联合而不是扩展接口(interface),因为 IssuePayment 类型没有通用的数据结构。

最佳答案

来自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
}
}
}
}

重命名架构中的一个或两个字段显然也可以解决问题。

关于GraphQL 错误 FieldsConflict : fields have different list shapes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56695262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com