首页 文章

Apollo客户端:如何简单地调试400代码错误?

提问于
浏览
5

我正在使用Apollo-client而且我不明白为什么这么难调试我的错误:我正在尝试对我的石墨烯python实现GraphQL执行mutate调用 . 最终得到400错误代码,我无法检索信息 .

例如:当我在/ graphQL接口上尝试我的石墨烯服务时,它会将有用的错误作为错误输入或错误的方法名称返回给我 . 在这里,在apollo客户端,它只给我一个400代码错误 . 这对我没有任何帮助 . 那么有可能从我的apolo客户端获取石墨烯的错误信息,而不是无用的400代码错误吗?

这是一个例子,来自我的石墨烯界面(/ graphQL):

mutation myMutation {
  uploadFile(input:"") {
    success
  }
}

将输出:

{
  "errors": [
    {
      "message": "Argument \"input\" has invalid value \"\".\nExpected \"UploadFileMutationInput\", found not an object.",
      "locations": [
        {
          "column": 20,
          "line": 2
        }
      ]
    }
  ]
}

当我在apollo客户端(js)上尝试相同时:

client.mutate({
  mutation: gql`
    mutation($file: Upload!) {
      uploadFile(input: $file) {
        success
      }
    }
  `,
  variables: { file } })
  .then(console.log)
  .catch((e) => { console.log("catch", e) })

我得到 Error: Network error: Response not successful: Received status code 400

我的捕获从未被调用过,文档根本没有帮助我 .

我想要的是得到我的调用的突变细节错误:当我使用不正确的方法调用或错误的输入类型时,我不应该没有任何关于什么使我的请求变坏的信息 .

3 回答

  • 2

    您应该在实例化 ApolloClient 时使用此链接:https://www.npmjs.com/package/apollo-link-error . 它应该在其他链接之前,如下:

    import { ApolloClient, ApolloLink } from 'apollo-boost'
    import { onError } from 'apollo-link-error'
    
    const errorLink = onError(({ graphQLErrors }) => {
      if (graphQLErrors) graphQLErrors.map(({ message }) => console.log(message))
    })
    
    new ApolloClient({
      ...
      link: ApolloLink.from([errorLink, authLink, restLink, httpLink]),
    })
    
  • 6

    这个解决方案为我解决了它:

    const client = new ApolloClient({
      uri: 'http://localhost:4444/graphql',
      onError: ({ networkError, graphQLErrors }) => {
        console.log('graphQLErrors', graphQLErrors)
        console.log('networkError', networkError)
      }
    })
    
  • -1

    我设法通过直接在客户端配置中放置一个 onError 函数来实现它(在变异中执行它时, graphQLErrors 总是空的):

    const client = new ApolloClient({
      uri: 'http://localhost:3000/graphql',
      onError: (e) => { console.log(e) },
    });
    

相关问题