GraphQL 查询 - 抑制非空错误和 return 部分数据
GraphQL query - suppress non-null error and return partial data
我正在使用 AppSync 和 DynamoDB。对于结果省略非空字段的 graphql 查询,有没有办法抑制非空错误和 return 部分数据?例如,如果我 运行 以下查询:
query GetPerson {
getPerson(id: "123") {
name
active
}
}
在我的 AppSync 解析器中,我有逻辑决定是否 return active
的值。如果我决定不 return active
,那么我会得到以下响应:
{
"data": {
"getPerson": null
},
"errors": [
{
"path": [
"getPerson",
"active"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'Boolean' within parent 'Person' (/getPerson/active)"
}
]
}
因为在我的架构中 active
字段是非空的。有没有什么可以抑制这个错误和 return 部分数据(即 name
的值)?我希望得到这样的回复:
{
"data": {
"getPerson": {
"name": "Jane Doe"
}
},
"errors": [
{
"path": [
"getPerson",
"active"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'Boolean' within parent 'Person' (/getPerson/active)"
}
]
}
没有。非空字段不应该 return null.
如果请求该字段并将其解析为 null,GraphQL 将 return 出错。因为该字段不能为 null,所以 GraphQL 将为父字段 return null。如果该字段也是非空的,那么 that 字段的父字段将为 return null... 依此类推,直到它命中可为空的父字段或根 (即 data
)。 spec:
中描述了此行为
If an error is thrown while resolving a field, it should be treated as though the field returned null, and an error must be added to the "errors" list in the response.
If the result of resolving a field is null (either because the function to resolve the field returned null or because an error occurred), and that field is of a Non-Null type, then a field error is thrown. The error must be added to the "errors" list in the response...
Since Non-Null type fields cannot be null, field errors are propagated to be handled by the parent field. If the parent field may be null then it resolves to null, otherwise if it is a Non-Null type, the field error is further propagated to it’s parent field...
If all fields from the root of the request to the source of the field error return Non-Null types, then the "data" entry in the response should be null.
如果 可能 active
为 null,则不应在您的架构中将其设置为不可为 null。
我正在使用 AppSync 和 DynamoDB。对于结果省略非空字段的 graphql 查询,有没有办法抑制非空错误和 return 部分数据?例如,如果我 运行 以下查询:
query GetPerson {
getPerson(id: "123") {
name
active
}
}
在我的 AppSync 解析器中,我有逻辑决定是否 return active
的值。如果我决定不 return active
,那么我会得到以下响应:
{
"data": {
"getPerson": null
},
"errors": [
{
"path": [
"getPerson",
"active"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'Boolean' within parent 'Person' (/getPerson/active)"
}
]
}
因为在我的架构中 active
字段是非空的。有没有什么可以抑制这个错误和 return 部分数据(即 name
的值)?我希望得到这样的回复:
{
"data": {
"getPerson": {
"name": "Jane Doe"
}
},
"errors": [
{
"path": [
"getPerson",
"active"
],
"locations": null,
"message": "Cannot return null for non-nullable type: 'Boolean' within parent 'Person' (/getPerson/active)"
}
]
}
没有。非空字段不应该 return null.
如果请求该字段并将其解析为 null,GraphQL 将 return 出错。因为该字段不能为 null,所以 GraphQL 将为父字段 return null。如果该字段也是非空的,那么 that 字段的父字段将为 return null... 依此类推,直到它命中可为空的父字段或根 (即 data
)。 spec:
If an error is thrown while resolving a field, it should be treated as though the field returned null, and an error must be added to the "errors" list in the response.
If the result of resolving a field is null (either because the function to resolve the field returned null or because an error occurred), and that field is of a Non-Null type, then a field error is thrown. The error must be added to the "errors" list in the response...
Since Non-Null type fields cannot be null, field errors are propagated to be handled by the parent field. If the parent field may be null then it resolves to null, otherwise if it is a Non-Null type, the field error is further propagated to it’s parent field...
If all fields from the root of the request to the source of the field error return Non-Null types, then the "data" entry in the response should be null.
如果 可能 active
为 null,则不应在您的架构中将其设置为不可为 null。