我需要Apollo GraphQL来发回http响应

时间:2016-11-02 18:44:21

标签: graphql apollo apollo-server

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

我在这里看不到任何东西,除非我找错了地方:

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

1 个答案:

答案 0 :(得分: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"
            ]
          }
        }
      }
    ]
  }
}

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