如何使用GraphQLError自定义消息传递?

时间:2018-08-07 08:26:41

标签: graphql express-graphql

我正在尝试使用GraphQLError自定义消息传递。

我想用GraphQL错误处理几个用例:

  • 当用户名和密码不匹配时,我要返回自定义消息,即用户名和密码不匹配。

  • 当用户输入无效的电子邮件时,我要返回自定义的消息,指出输入的电子邮件无效。

  • 以及其他一些用例。

我创建了一个ValidateError.js文件以使用GraphQLError处理功能:

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;
      }, {});
  }
}

这是我的应用程序索引文件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
    };
  }
})));

我的问题是如何使用此函数来获取graphQLError

  

formatError

谢谢。

1 个答案:

答案 0 :(得分:1)

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

"graphql": "^0.13.2"

只需将您的错误抛出到resolver中,formatError函数将捕获解析器中抛出的每个错误。

这是我的工作:

appError.js

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

exports.AppError = AppError;

resolver中引发自定义错误:

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

formatError中捕获此错误:

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

其他示例:

resolver中输入错误:

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

formatError中捕获错误:

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

以下是输出:

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)