首页 > 解决方案 > 在解析器中返回数据和错误

问题描述

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 对此进行测试

标签: graphql

解决方案


推荐阅读