0

我希望能够让我的 express graphQL 服务器将 HTTP 状态代码连同响应一起发送回客户端。目前,我所看到的只是data,基于状态代码的附加功能会有所帮助。

我在这里什么都看不到,除非我找错地方了:

http://dev.apollodata.com/tools/graphql-server/setup.html

4

1 回答 1

0

对于 Apollo Server 2.2.x 及更高版本,我们可以使用插件来自定义 HTTP 状态码。在willsendresponse事件处理程序中自定义 HTTP 状态代码。您可以检查errors数组并为不同类型的错误设置相应的 HTTP 状态代码。

例如

server.ts

import { ApolloServer, gql } from 'apollo-server';
import { ApolloServerPlugin } from 'apollo-server-plugin-base';

function customHTTPStatusPlugin(): ApolloServerPlugin {
  return {
    requestDidStart(requestContext) {
      return {
        willSendResponse({ errors, response }) {
          if (response && response.http) {
            if (errors) {
              response.data = undefined;
              response.http.status = 500;
            }
          }
        },
      };
    },
  };
}

const typeDefs = gql`
  type Query {
    hello: String
  }
`;
const resolvers = {
  Query: {
    hello() {
      throw new Error('something happened');
    },
  },
};

const server = new ApolloServer({ typeDefs, resolvers, plugins: [customHTTPStatusPlugin()] });
const port = 3000;
server.listen(port).then(({ url }) => console.log(`Server is ready at ${url}`));

响应:

{
  "error": {
    "errors": [
      {
        "message": "something happened",
        "locations": [
          {
            "line": 2,
            "column": 3
          }
        ],
        "path": [
          "hello"
        ],
        "extensions": {
          "code": "INTERNAL_SERVER_ERROR",
          "exception": {
            "stacktrace": [
              "Error: something happened",
              "    at hello (/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/src/stackoverflow/40387508/server.ts:30:13)",
              "    at field.resolve (/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/node_modules/apollo-server-express/node_modules/graphql-extensions/src/index.ts:274:18)",
              "    at field.resolve (/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/node_modules/apollo-server-express/node_modules/apollo-server-core/src/utils/schemaInstrumentation.ts:103:18)",
              "    at resolveFieldValueOrError (/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/node_modules/graphql/execution/execute.js:467:18)",
              "    at resolveField (/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/node_modules/graphql/execution/execute.js:434:16)",
              "    at executeFields (/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/node_modules/graphql/execution/execute.js:275:18)",
              "    at executeOperation (/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/node_modules/graphql/execution/execute.js:219:122)",
              "    at executeImpl (/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/node_modules/graphql/execution/execute.js:104:14)",
              "    at Object.execute (/Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/node_modules/graphql/execution/execute.js:64:35)",
              "    at /Users/ldu020/workspace/github.com/mrdulin/apollo-graphql-tutorial/node_modules/apollo-server-express/node_modules/apollo-server-core/src/requestPipeline.ts:548:22"
            ]
          }
        }
      }
    ]
  }
}

您还将500 Internal Server Error在客户端获得 HTTP 状态代码。

于 2020-10-08T09:02:46.737 回答