Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an array of errors with graphQL

How can I return multiple error messages like this ?

"errors": [
  {
    "message": "first error",
    "locations": [
      {
        "line": 2,
        "column": 3
      }
    ],
    "path": [
      "somePath"
    ]
  },
  {
    "message": "second error",
    "locations": [
      {
        "line": 8,
        "column": 9
      }
    ],
    "path": [
      "somePath"
    ]
  },
]

On my server, if I do throw('an error'), it returns.

"errors": [
  {
    "message": "an error",
    "locations": [
      {
      }
    ],
    "path": ["somePath"]
  }
]

I would like to return an array of all the errors in the query. How can I add multiple errors to the errors array ?

like image 927
Lev Avatar asked May 23 '17 09:05

Lev


2 Answers

Throw an error object with errors:[] in it. The errors array should have all the errors you wanted to throw together. Use the formatError function to format the error output. In the below example, I am using Apollo UserInputError. You can use GraphQLError as well. It doesn't matter.

const error = new UserInputError()
error.errors = errorslist.map((i) => {
  const _error = new UserInputError()
  _error.path = i.path
  _error.message = i.type
  return _error
})
throw error

new ApolloServer({
  typeDefs,
  resolvers,
  formatError: ({ message, path }) => ({
    message,
    path,
  }),
})

//sample output response
{
  "data": {
    "createUser": null
  },
  "errors": [
    {
      "message": "format",
      "path": "username"
    },
    {
      "message": "min",
      "path": "phone"
    }
  ]
}
like image 124
Ravi Avatar answered Nov 14 '22 22:11

Ravi


Using ApolloServer I've found multiple errors will be returned when querying an array of items and an optional field's resolver errors.

// Schema
gql`
  type Foo {
    id: ID!
    bar: String # Optional
  }

  type Query {
    foos: [Foo!]!
  }
`;

// Resolvers
const resolvers = {
  Query: {
    foos: () => [{ id: 1 }, { id: 2 }]
  }
  Foo: {
    bar: (foo) => {
       throw new Error(`Failed to get Foo.bar: ${foo.id}`);
    }
  }
}

// Query
gql`
  query Foos {
    foos {
      id
      bar
    }
  }
`;

// Response
{
  "data": {
    "foos": [{ id: 1, bar: null }, { id: 2, bar: null }]
  },
  "errors": [{
    "message": "Failed to get Foo.bar: 1"
  }, {
    "message": "Failed to get Foo.bar: 2"
  }]
}

If Foo.bar is not optional, it will return just the first error.

If you want to return many errors, at once, I would recommend MultiError from VError which allows you to represent many errors in one error instance.

like image 30
riscarrott Avatar answered Nov 14 '22 21:11

riscarrott