graphql - 在解析器中返回数据和错误
问题描述
在issue 591 leebyron 中,您可以通过返回一个数组来返回数据和解析器中的错误(根据规范),例如
resolve() {
return [ "value", new Error("2nd value is missing"), "value" ]
}
但这不起作用。我会认为以下应该可以工作(从入门修改):
import { graphql, buildSchema } from 'graphql'
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Query {
hello: String
}
`)
// The root provides a resolver function for each API endpoint
var root = {
hello: () => {
return [ 'Hello world!', new Error('apocalypse') ]
}
}
// Run the GraphQL query '{ hello }' and print out the response
graphql(schema, '{ hello }', root).then((response) => {
console.log(response)
})
并且应该返回
{
data: { hello: 'Hello world!' },
errors: [{ message: 'apocalypse' }]
}
那么我应该如何在解析器中返回数据和错误呢?我目前正在使用 graphql (js) 版本 0.13.2 对此进行测试
解决方案
推荐阅读
- php - 有没有办法从包含 PHP 中的 json 数组的 URL 中提取数据
- netlogo - 如何提取世界上每只海龟所走的路径并将其保存在 .csv 文件中?
- javascript - 如何在成功的 xhr POST 后将用户发送到链接?
- google-kubernetes-engine - 如何在自动驾驶模式下停止(而不是删除)K8S 集群
- numpy - .npz 文件包含稀疏矩阵。但是无法读取这些矩阵。如何解决这个问题?
- android - REACT NATIVE:我在底部添加了一张图片。现在我的键盘打不开
- django - Django CKEditor - youtube 嵌入视频在管理面板保存后消失
- google-sheets - searchRecord 未呈现正确的信息或需要多次显示
- flutter - `LinearProgressIndicator` - “BoxConstraints 强制使用无限宽度。” 错误
- php - PHP 对象数组(get() 方法不起作用)