vuejs / apollo

🚀 Apollo/GraphQL integration for VueJS
http://apollo.vuejs.org
MIT License
6.03k stars 522 forks source link

Surface errors from GraphQL server response #846

Open chriscalo opened 5 years ago

chriscalo commented 5 years ago

Is your feature request related to a problem? Please describe.

As I'm authoring new Apollo client queries in my Nuxt SSR app, I make mistakes, of course, and I see the following an error in my server output (source):

POST /graphql 400 1227 - 3.604 ms
%cError background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold; Network error: Response not successful: Received status code 400
%cError background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold; Network error: Response not successful: Received status code 400
GET /vars/ 200 2017 - 27.974 ms

Problem 1: There's no useful error message here. It just says Network error: Response not successful: Received status code 400. I know I made a mistake somewhere, but the error message neither helps me locate it nor tells me what I've done wrong. So I have to go looking around at different files to see if I can spot where I've gone wrong.

Problem 2: I see the error message in the browser, but it also doesn't contain any useful information. In fact, it distracts me from noticing that I really should be looking at the failed POST request to /graphql 2 lines above.

Screen Shot 2019-10-28 at 10 45 09 AM

But to get to the error message requires 2 additional steps (that seem unnecessary): first, you have to click the little URL string (http://localhost:8000/graphql), which takes you to the Network tab in Dev Tools. But I still don't have the info I need:

Screen Shot 2019-10-28 at 11 01 32 AM

I need to know to click on the graphql row, which finally shows me the real problem: I misspelled the field name!

Screen Shot 2019-10-28 at 11 04 36 AM 1

And it's not even formatted for easy reading. Here's the full JSON error after taking it into a text editor and running it through Prettier:

{
  "errors": [
    {
      "message": "Cannot query field \"valueseff\" on type \"Var\". Did you mean \"value\"?",
      "locations": [{ "line": 4, "column": 5 }],
      "extensions": {
        "code": "GRAPHQL_VALIDATION_FAILED",
        "exception": {
          "stacktrace": [
            "GraphQLError: Cannot query field \"valueseff\" on type \"Var\". Did you mean \"value\"?",
            "    at Object.Field (/Users/ccalo/Projects/viaticus/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)",
            "    at Object.enter (/Users/ccalo/Projects/viaticus/node_modules/graphql/language/visitor.js:324:29)",
            "    at Object.enter (/Users/ccalo/Projects/viaticus/node_modules/graphql/language/visitor.js:375:25)",
            "    at visit (/Users/ccalo/Projects/viaticus/node_modules/graphql/language/visitor.js:242:26)",
            "    at Object.validate (/Users/ccalo/Projects/viaticus/node_modules/graphql/validation/validate.js:73:24)",
            "    at validate (/Users/ccalo/Projects/viaticus/node_modules/apollo-server-core/dist/requestPipeline.js:212:32)",
            "    at Object.<anonymous> (/Users/ccalo/Projects/viaticus/node_modules/apollo-server-core/dist/requestPipeline.js:125:42)",
            "    at Generator.next (<anonymous>)",
            "    at fulfilled (/Users/ccalo/Projects/viaticus/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)"
          ]
        }
      }
    }
  ]
}

Problem 3: The intended formatting works in the browser, but, as shown above, it doesn't at all work on my Node.js server output.

Screen Shot 2019-10-28 at 10 55 03 AM

Describe the solution you'd like

Solution 1: In both Node.js and in the browser, show the error message from the GraphQL server response. For example:

POST /graphql 400 1227 - 3.604 ms

GraphQLError: Cannot query field "valueseff" on type "Var". Did you mean "value"?",
  at Object.Field (/Users/ccalo/Projects/viaticus/node_modules/graphql/validation/rules/FieldsOnCorrectType.js:53:31)",
  at Object.enter (/Users/ccalo/Projects/viaticus/node_modules/graphql/language/visitor.js:324:29)",
  at Object.enter (/Users/ccalo/Projects/viaticus/node_modules/graphql/language/visitor.js:375:25)",
  at visit (/Users/ccalo/Projects/viaticus/node_modules/graphql/language/visitor.js:242:26)",
  at Object.validate (/Users/ccalo/Projects/viaticus/node_modules/graphql/validation/validate.js:73:24)",
  at validate (/Users/ccalo/Projects/viaticus/node_modules/apollo-server-core/dist/requestPipeline.js:212:32)",
  at Object.<anonymous> (/Users/ccalo/Projects/viaticus/node_modules/apollo-server-core/dist/requestPipeline.js:125:42)",
  at Generator.next (<anonymous>)",
  at fulfilled (/Users/ccalo/Projects/viaticus/node_modules/apollo-server-core/dist/requestPipeline.js:5:58)"

GET /vars/ 200 2017 - 27.974 ms
  1. When running in SSR mode in Node.js, remove the formatting that only works in browser Dev Tools.

Describe alternatives you've considered

I've since learned that I need to do the following:

  1. Ignore Network error: Response not successful: Received status code 400 error messages in server output.
  2. Go to the browser and open the console.
  3. Ignore Network error: Response not successful: Received status code 400 error messages in the console.
  4. Find the failed POST request, don't expand it, look for the URL and click it.
  5. Find the graphql row, click it.
  6. Look at the error message.

I now know to do that, but that feels like far too much work for something that I'm assuming can be surfaced in my server output and in the browser console.

Additional context

I'm using Nuxt.js for SSR along with @nuxtjs/apollo.

rospirski commented 4 years ago

I have the same problem. Did anyone find any solution?

image