在解析器中同时返回数据和错误

时间:2018-07-18 06:04:12

标签: graphql

issue 591中,leebyron指出,您可以通过返回一个数组(例如,规范)来返回解析器中的数据和错误。

resolve() {
    return [ "value", new Error("2nd value is missing"), "value" ]
}

但是这不起作用。我本以为以下应该可行(从getting started修改):

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版本进行测试

0 个答案:

没有答案
相关问题