This course has been updated! We now recommend you take the Server-Side GraphQL in Node.js course.

Check out a free preview of the full Introduction to GraphQL course:
The "Query Errors" Lesson is part of the full, Introduction to GraphQL course featured in this preview video. Here's what you'd learn in this lesson:

Scott demonstrates what an error looks like as a response from the API.

Get Unlimited Access Now

Transcript from the "Query Errors" Lesson

[00:00:00]
>> Scott Moss: The other thing I wanna show is I talked about throwing error, so let's say I throw an error in here.
>> Scott Moss: Throw new Error, oop. So if I say that,
>> Scott Moss: Let this refresh, run this. You can see that instead of getting back a data property, I get back an error property.

[00:00:25] The data property is null. So it gets nulled out completely. Then you get back an errors property with an array of the errors that were thrown. So you can see I got the error message I had, it puts the location where it was, what path it was on.

[00:00:39] So in this case, I tried to execute the cat query so that's where it happened. And then this is Apollo doing some of its stuff, where it tries to give you a nice error code so you can figure out what's happening. And then maybe some not so public stuff you wanna show, like the stack trace.

[00:00:53] So you could wrap all of this and get rid of it if you like by formatting the error, but this is what happens. But if you were to look at the status code of this in like a browser or whatever, it's still a 200.
>> Scott Moss: You always gonna get back in 200.

[00:01:06] Unless your server, if your server craps out then that's not Express, I'm sorry, that's not Apollo, that's not GraphQL, that's just your server, that's outside of GraphQL. But inside of GraphQL, it's always gonna be a 200, it's always gonna select these errors. Sometimes you might have a state when you get back data and you get back errors at the same time.

[00:01:25] That can possibly happen when you start doing this nested data. And somewhere down here there might be an error but everything else resolved up here. So there's special cases where that would happen, where you might have some data that came back in some of that error. But that's only because you might have designed your scheme in a way where it's non-restrictive.

[00:01:46] To allow some data to come and some data not to come, and because of that you ran into that problem. So that's why it's important to figure out if you need to put non nolar or not non nolar on your data.