【问题标题】:GraphQL not null field of nullable object returning errorGraphQL not null 可空对象的字段返回错误
【发布时间】:2019-08-06 10:16:50
【问题描述】:

以下模式在可选的对象中包含非空字段(允许整个对象为空)。 它定义了一个具有可选字段 Child 的 Parent 对象列表 - 一些父母被允许拥有 null Child。

type People {
people : [Parent]
}

type Parent {
child : Child
}

type Child {
key : String!
}

以下 GraphQL 查询返回预期的 Parent 对象列表(一些具有 null Child 值)。 但它也会返回附加到结果的错误。 这是 GraphQL 中的错误吗(孩子是可选的)?还是预期的行为?

Cannot return null for non-nullable type: 'String' within parent 'Child'

【问题讨论】:

    标签: graphql


    【解决方案1】:

    这是预期的行为。问题不在于某些child 字段为空,而是某些Childkey 返回空——这就是错误中提到的String。您不会在数据中看到 null 键;相反,有问题的子字段将只返回null。这是因为 GraphQL 错误“冒泡”到下一个可为空的父字段,如 spec 中所述:

    由于 Non-Null 类型的字段不能为 null,因此字段错误会被传播以由父字段处理。如果父字段可能为 null 则解析为 null,否则如果为 Non-Null 类型,则字段错误进一步传播到其父字段。

    【讨论】:

    猜你喜欢
    • 2021-03-18
    • 2019-05-28
    • 2021-10-29
    • 2021-11-27
    • 2021-10-24
    • 2021-10-07
    • 1970-01-01
    • 2022-01-02
    • 2019-10-25
    相关资源
    最近更新 更多