我有一个问题,即 GraphQL 查询/变异的响应在以下每种情况下应该是什么样子:
我不确定后者是否可能,但我似乎记得在某处读到它可能发生。例如。在多个突变的情况下,假设有两个,其中每个突变都按顺序处理。我认为如果第一个突变没问题,上面的情况 #3 可能会发生,但是在第二个突变的执行过程中会发生错误,但我不确定。
无论如何,响应应该如何?喜欢下面的吗? (JSON 中的示例,其中每个都对应于之前的案例。)或者还有其他更惯用的方法吗?也许 Relay 提供了一些关于它应该是什么样子的指导方针?我找不到任何好的资源。
1:
{
"data": {
...
}
}
2:
{
"errors": [
{
...
},
...
]
}
3:
{
"data": {
...
},
"errors": [
{
...
},
...
]
}
谢谢。
最佳答案
是的,您的样本回复在我看来很正确。这是“案例 3”的更详细示例。
在其中一个字段中出现错误的示例查询
query MyQuery {
viewer {
articles(first: 1) {
edges {
node {
title
tags # we'll introduce an error in the schema here
}
}
}
}
}
样本响应
{
"data": {
"viewer": {
"articles": {
"edges": [
{
"node": {
"title": "Sample article title",
"tags": null
}
}
]
}
}
},
"errors": [
{
"message": "Cannot read property 'bar' of undefined",
"locations": [
{
"line": 7,
"column": 11
}
]
}
]
}
关于graphql - GraphQL 查询/变异的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33157168/