阿波罗graphql响应数据中未显示``扩展''字段 [英] `Extensions` field not shown in apollo graphql response data

查看:70
本文介绍了阿波罗graphql响应数据中未显示``扩展''字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个可复制的示例.运行 app.js 并在 http://localhost:4000/graphql

您可以运行以下查询:

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

问题:

我需要响应数据中 extensions 字段中的调试信息.我说的是这个扩展名字段:

I need debugging information from the extensions field in the response data. I'm talking about this extensions field:

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

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

But in reality, I'm only getting the data field:

  "data":{....}

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

I have already enabled tracing and cacheControl in the apollo server config but the extensions field is still excluded in the response data. How can I get the extensions data back?

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

Here's how the apollo engine starts:

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

推荐答案

您可以使用 formatResponse 格式化来自阿波罗的响应.

You could format the response from apollo with 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;
  }
});

文档

这篇关于阿波罗graphql响应数据中未显示``扩展''字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆