Schema 扩充输出扩展
输出扩展
GraphQL 规范详细描述了响应的格式化方式,规定了返回映射中必须使用的顶级条目:查询到的数据添加在 data 条目下,错误添加在 errors 条目下。
但有时我们需要输出一些额外信息,例如日志、警告或建议。这些条目未被规范涵盖,我们无法将其作为独立的顶级条目添加。为此,GraphQL 规范提供了一个特殊位置,供我们自由填充、传递任意自定义数据,那就是顶级的 extensions 条目。
如 Response Format 章节 所述:
The response map may also contain an entry with key
extensions. This entry, if set, must have a map as its value. This entry is reserved for implementors to extend the protocol however they see fit, and hence there are no additional restrictions on its contents.
Gato GraphQL 的「Proactive Feedback」功能利用此能力扩展 GraphQL API 的响应,提供额外信息:
- Deprecations
- Warnings
我们可以向用户提供额外信息,指出 query 的潜在改进点:
{
"extensions": {
"warnings": [
{
"message": "Dynamic variable with name 'props' had already been set, had its value overridden",
"locations": [
{
"line": 4,
"column": 25
}
]
}
]
},
"data": {
"posts": {
"excerpt": "Hello world!",
"Content": "<p>Hello world!</p>"
}
}
}Prev
Next