3

I am using apollographql/graphql-server. The server responses look like:

{"data":{...},"errors":[{"message":"...","locations":...}]}

I have two questions:

  1. I find that I can throw or return an Error object and it will be pushed to the response's errors array, but the response is sent as soon as I do so. How can I return more than one error?

  2. Is the errors array supposed for application or server errors like bugs only? Should data checking and validation errors be placed inside data object?

Thanks in advance!

2 Answers 2

1
  1. If you need to return more than one error for a response, you'll most likely need to build an array of errors yourself, and then when you're ready, return the graphql errors. I do something similar in my code. Though most of the time, if I get an error, I'll want to stop it there anyways, and return the issue to the client.

  2. Anything outside of the typical 'happy path' for you're graphql errors you'll want to return an error. Your client side code is going to be relying on that data, so you'll want your client to be able to respond to unexpected issues that happen on the server. Resolved or empty result sets are the only thing that passes back as normal and doesn't error out. Anything else, missing parameters, database errors, etc, you'll want to return an error.

Hope that helps! :)

2
  • Thanks for your answer. 1) I tried to throw or return an array of errors or error with array of strings before, but no luck. Do you know how to do that? 2) Under normal routine, I place the errors caught in data object instead, as the error array from server response seems not suitable for errors like form validation
    – James
    Commented Feb 21, 2017 at 5:09
  • Hi James, i'm seeing the same thing and trying to get to the bottom of it, i cant figure out how to throw more than one error, so my errors coming back as an array seems a little pointless.... did you ever find a answer???
    – Samuel
    Commented Feb 28, 2019 at 14:44
0

Check this out: https://github.com/thebigredgeek/apollo-errors

From their ReadMe:

Create some errors:

import { createError } from 'apollo-errors';

export const FooError = createError('FooError', {
  message: 'A foo error has occurred'
});

Hook up formatting:

import express from 'express';
import bodyParser from 'body-parser';
import { formatError } from 'apollo-errors';
import { graphqlExpress } from 'apollo-server-express';
import schema from './schema';

const app = express();

app.use('/graphql',
  bodyParser.json(),
  graphqlExpress({
    formatError,
    schema
  })
);

app.listen(8080)

Throw some errors:

import { FooError } from './errors';

const resolverThatThrowsError = (root, params, context) => {
  throw new FooError({
    data: {
      something: 'important'
    }
  });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.