我有一个GraphQL查询,该查询调用REST服务以获取返回对象。该查询包含一个Id参数,然后将其传递给服务。但是,如果不存在具有该ID的对象,则REST服务可以使用http状态404找不到响应。这似乎是正确的答案。
如何在GraphQL中建模“未找到”响应? 有没有办法通知GQL呼叫者某些东西不存在?
更新
我正在考虑的一些选择:
但是目前尚不清楚GQL API设计中是否有建议的做法。
答案 0 :(得分:2)
您可能会将其视为错误并进行相应处理。
我建议您检查the GraphQL spec, the paragraph about error handling.
我希望它完全包含您要查找的内容。
基本上,您应该尽可能返回,并在“错误”字段中告知客户潜在的问题。
文档中的示例:
请求:
{
hero(episode: $episode) {
name
heroFriends: friends {
id
name
}
}
}
响应:
{
"errors": [
{
"message": "Name for character with ID 1002 could not be fetched.",
"locations": [ { "line": 6, "column": 7 } ],
"path": [ "hero", "heroFriends", 1, "name" ]
}
],
"data": {
"hero": {
"name": "R2-D2",
"heroFriends": [
{
"id": "1000",
"name": "Luke Skywalker"
},
{
"id": "1002",
"name": null
},
{
"id": "1003",
"name": "Leia Organa"
}
]
}
}
}