Search code examples
graphqlexpress-graphql

How to use GraphQLError for customize messaging?


I am trying to do customize messaging with GraphQLError.

There are few use cases that I want to handle with GraphQL Error:

  • when username and password did not match, I want to return customize the message that username and password did not match.

  • When the user entered an invalid email, I want to return customize the message that entered email is not valid.

  • And few other use cases.

I created a ValidateError.js File to use GraphQLError handling function:

const { GraphQLError } = require('graphql');

module.exports  = class ValidationError extends GraphQLError {
  constructor(errors) {

    super('The request is invalid');

    var err = errors.reduce((result, error) => {

        if (Object.prototype.hasOwnProperty.call(result, error.key)) {
          result[error.key].push(error.message);
        } else {
          result[error.key] = [error.message];
        }

        return result;
      }, {});
  }
}

Here is the code of my application index file app.js:

app.use('/graphql', graphqlExpress(req => ({
  schema,
  context: {
    user: req.user
  },
  formatError(err) {
    return {
      message: err.message,
      code: err.originalError && err.originalError.code,   
      locations: err.locations,
      path: err.path
    };
  }
})));

My question is how can I use this function for grabbing graphQLError

formatError

Thanks in advance.


Solution

  • "apollo-server-express": "^1.3.5"

    "graphql": "^0.13.2"

    Just throw your error in resolver, formatError function will catch each error thrown in resolver.

    Here is my work:

    appError.js

    class AppError extends Error {
      constructor(opts) {
        super(opts.msg);
        this.code = opts.code;
      }
    }
    
    exports.AppError = AppError;
    

    throw an custom error in resolver:

    throw new AppError({ msg: 'authorization failed', code: 1001 });

    catch this error in formatError:

      formatError: error => {
        const { code, message } = error.originalError;
        return { code, message };
      },
    

    Other sample:

    throw your error in resolver:

    const resolvers = {
      Query: {
        books: () => {
          throw new GraphQLError('something bad happened');
        }
      }
    };
    

    catch error in formatError:

    graphqlExpress(req => {
        return {
          schema,
          formatError: err => {
            console.log('format error');
            return err;
          }
        };
      })
    

    Here is the output:

    format error
    GraphQLError: something bad happened
        at books (/Users/ldu020/workspace/apollo-server-express-starter/src/graphql-error/index.js:23:13)
        at /Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql-tools/dist/schemaGenerator.js:518:26
        at resolveFieldValueOrError (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:531:18)
        at resolveField (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:495:16)
        at /Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:364:18
        at Array.reduce (<anonymous>)
        at executeFields (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:361:42)
        at executeOperation (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:289:122)
        at executeImpl (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:154:14)
        at Object.execute (/Users/ldu020/workspace/apollo-server-express-starter/node_modules/graphql/execution/execute.js:131:229)