当前位置:  开发笔记 > 编程语言 > 正文

使用ApolloClient和Apollo-Server-Express进行GraphQL错误处理

如何解决《使用ApolloClient和Apollo-Server-Express进行GraphQL错误处理》经验,为你挑选了1个好方法。

onError函数from apollo-link-error有一个填充的graphQLErrors对象,但是当ApolloClient抛出一个错误对象时,该 graphQLErrors属性包含一个空数组.进一步检查显示graphql错误消息error.networkError.result.errors.

如何正确配置Apollo Client以返回填充的graphQLErrors对象?

Apollo客户端设置:

const {ApolloClient} = require('apollo-client')
const { ApolloLink } = require('apollo-link')

const {HttpLink} = require('apollo-link-http')
const {onError} = require('apollo-link-error')
const {InMemoryCache} = require('apollo-cache-inmemory')

const errorLink = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors) {
    graphQLErrors.map(({ message, locations, path }) =>
      console.log(
        `[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
      ),
    )
  }

  if (networkError) console.log(`[Network error]: ${networkError}`)
})

const middleware = (req, ignored_res, next) => {
  const client = new ApolloClient({
    link: ApolloLink.from([errorLink, new HttpLink({ uri:'http://somegraphqlserver.com', fetch: require('node-fetch') })]),
    cache: new InMemoryCache(),
  })

  req.GQLClient = client

  return next()
}

module.exports = middleware

调用apollo-server-express:

req.GQLClient
    .query({
      query: SOME_MALFORMED_QUERY,
    })
    .then((data) => {...})
    .catch((error) => {
      console.log('rawError', error)
      console.log('error.networkError.result.errors', error.networkError.result.errors)
      return next(error)
    })

控制台结果:

[GraphQL error]: Message: Cannot query field "blah" on type "CustomerList"., Location: [object Object], Path: undefined
[Network error]: Error: Response not successful: Received status code 400


 rawError { Error: Network error: Response not successful: Received status code 400
   at new ApolloError (/.../node_modules/apollo-client/bundle.umd.js:121:28)
   at /.../node_modules/apollo-client/bundle.umd.js:1187:41
   at /.../node_modules/apollo-client/bundle.umd.js:1620:17
   at Array.forEach ()
   at /.../node_modules/apollo-client/bundle.umd.js:1619:18
   at Map.forEach ()
   at QueryManager.broadcastQueries (/.../node_modules/apollo-client/bundle.umd.js:1614:22)
   at /.../node_modules/apollo-client/bundle.umd.js:1114:31
   at process._tickCallback (internal/process/next_tick.js:178:7)
 graphQLErrors: [],
 networkError: 
  { Error: Response not successful: Received status code 400
   at throwServerError (/.../node_modules/apollo-link-http-common/lib/bundle.umd.js:33:21)
   at /.../node_modules/apollo-link-http-common/lib/bundle.umd.js:58:17
   at process._tickCallback (internal/process/next_tick.js:178:7)
    response: 
     Response {
       size: 0,
       timeout: 0,
       [Symbol(Body internals)]: [Object],
       [Symbol(Response internals)]: [Object] },
    statusCode: 400,
    result: { errors: [Array] } },
 message: 'Network error: Response not successful: Received status code 400',
 extraInfo: undefined }


error.networkError.result.errors [ { message: 'Cannot query field "blah" on type "CustomerList".',
   locations: [ [Object] ] } ]

库版本:

服务器:

"apollo-server-express": "^1.3.2"

客户:

"apollo-cache-inmemory": "^1.1.12"
"apollo-client": "^2.2.8"
"apollo-link-error": "^1.0.9"
"apollo-link-http": "^1.5.4"

Daniel Reard.. 5

这是设计的.来自文档:

graphQLErrors:GraphQL端点的错误数组

networkError:链接执行或服务器响应期间的任何错误,未在GraphQL结果中作为错误字段的一部分提供

换句话说,如果您的查询格式错误,您请求的字段无效(如您的示例中所示),或者遇到导致状态不是200的任何其他问题,则该错误将显示为该networkError属性的一部分.另一方面,如果请求返回200,但是响应中的errors数组被填充,那些相同的错误将作为其中一部分返回graphQLErrors.

如果要查看graphQLErrors正在填充的示例,请正确格式化查询,但让其中一个解析器在调用后立即抛出错误.只要查询没有遇到任何其他问题,您应该会看到内部弹出相同的错误graphQLErrors.



1> Daniel Reard..:

这是设计的.来自文档:

graphQLErrors:GraphQL端点的错误数组

networkError:链接执行或服务器响应期间的任何错误,未在GraphQL结果中作为错误字段的一部分提供

换句话说,如果您的查询格式错误,您请求的字段无效(如您的示例中所示),或者遇到导致状态不是200的任何其他问题,则该错误将显示为该networkError属性的一部分.另一方面,如果请求返回200,但是响应中的errors数组被填充,那些相同的错误将作为其中一部分返回graphQLErrors.

如果要查看graphQLErrors正在填充的示例,请正确格式化查询,但让其中一个解析器在调用后立即抛出错误.只要查询没有遇到任何其他问题,您应该会看到内部弹出相同的错误graphQLErrors.

推荐阅读
oDavid_仔o_880
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有