如何使用 GraphQLError 自定义消息?

How to use GraphQLError for customize messaging?

我正在尝试使用 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

提前致谢。

"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)

在 GraphQLError 内部,您应该可以访问 GraphQLErrorExtensions,它应该允许您在抛出错误时附加自定义消息。

此答案是在了解 apollo 服务器通过此扩展选项抛出自定义错误的情况下编写的。这可能是可以实现的,但更复杂。我建议检查 apollo 服务器错误:https://github.com/apollographql/apollo-server/blob/main/packages/apollo-server-errors/src/index.ts


看起来你可以做的只是通过扩展传递任何附加信息,但你只能在构造函数中设置它:new GraphQLError('your error message', null, null, null, null, null, {"message1": [ "custom message1" ], "message2": [ "customer message1", "custom message2" ]})