Graphql-Shield 劫持错误

Mar*_*cus 1 apollo graphql apollo-server graphql-shield

在解析器中,throw new createError.BadRequest("bad input")错误被劫持Graphql-shield并显示为

{
    "errors": [
        {
            "message": "Not Authorised!",
            "locations": [
                {
                    "line": 2,
                    "column": 3
                }
            ],
            "path": [
                "myMutation"
            ],
            "extensions": {
                "code": "INTERNAL_SERVER_ERROR",
                "exception": {
                    "stacktrace": [
                        "Error: Not Authorised!",
Run Code Online (Sandbox Code Playgroud)

这是 Apollo 服务器设置

    const schema = buildSubgraphSchema([
      { typeDefs: await typeDefs(), resolvers },
    ]);
    const apolloServer = new ApolloServer({
      schema: applyMiddleware(schema, permissions),
      context: async ({ req, res }) => new AuthenticatedContext(req, res)
    });
Run Code Online (Sandbox Code Playgroud)

如何返回实际发生的错误?

小智 6

我在屏蔽文档中找到了解决方案:

const permissions = shield({
    Query: {
        ...
    },
    Mutation: {
        ...
    },
}, {allowExternalErrors: true});
Run Code Online (Sandbox Code Playgroud)

根据文档,allowExternalErrors选项默认为 false。