阿波罗graphql响应数据中未显示``扩展''字段

时间:2018-11-19 01:58:13

标签: node.js express graphql apollo apollo-server

这里是reproducible example。运行app.js,然后在http://localhost:4000/graphql

上浏览游乐场

您可以运行以下查询:

query RecipeQuery{
  recipe(title:"Recipe 2"){
    description
  }
}

问题:

我需要响应数据中extensions字段中的调试信息。我在谈论这个extensions领域:

  "data":{....},
  "extensions": {
    "tracing": {}
    "cacheControl":{}
  }

但是实际上,我只得到数据字段:

  "data":{....}

我已经在apollo服务器配置中启用了tracingcacheControl,但是响应数据中仍然排除了extensions字段。如何获取extensions数据?

这是阿波罗引擎启动的方式:

const expressApp = express();

const server = new ApolloServer({
    schema,
    tracing: true,
    cacheControl: true,
    engine: false, // we will provide our own ApolloEngine
});

server.applyMiddleware({ app: expressApp });

const engine = new ApolloEngine({
    apiKey: "YOUR_ID",

});

engine.listen(
    {
        port,
        expressApp,
        graphqlPaths: [graphqlEndpointPath],
    },
    () => console.log(`Server with Apollo Engine is running on http://localhost:${port}`),
);

依赖项

  "dependencies": {
    "apollo-cache-control": "^0.1.1",
    "apollo-engine": "^1.1.2",
    "apollo-server-express": "^2.2.2",
    "graphql-depth-limit": "^1.1.0",
    "graphql-yoga": "^1.16.7",
    "type-graphql": "^0.15.0"
  }

1 个答案:

答案 0 :(得分:1)

您可以使用formatResponse格式化来自阿波罗的回复。

const server = new ApolloServer({
  typeDefs,
  resolvers,
  formatResponse: response => {
    console.log(response); 
    /* 
    ** { data } with informations such as queryType,
    ** directives ...
    ** I guess there is also the extensions key 
    */
    return response;
  }
});

doc