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

Piy*_*sal 6 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;
      }, {});
  }
}
Run Code Online (Sandbox Code Playgroud)

这是我的应用程序索引文件 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
    };
  }
})));
Run Code Online (Sandbox Code Playgroud)

我的问题是如何使用这个函数来抓取 graphQLError

格式错误

提前致谢。

sli*_*wp2 5

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

"graphql": "^0.13.2"

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

这是我的工作:

appError.js

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

exports.AppError = AppError;
Run Code Online (Sandbox Code Playgroud)

抛出自定义错误resolver

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

formatError以下位置捕获此错误:

  formatError: error => {
    const { code, message } = error.originalError;
    return { code, message };
  },
Run Code Online (Sandbox Code Playgroud)

其他样品:

抛出你的错误resolver

const resolvers = {
  Query: {
    books: () => {
      throw new GraphQLError('something bad happened');
    }
  }
};
Run Code Online (Sandbox Code Playgroud)

捕获错误formatError

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

这是输出:

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)
Run Code Online (Sandbox Code Playgroud)