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

问题描述

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

Here is a reproducible example. Run app.js and navigate the playground at http://localhost:4000/graphql

您可以运行以下查询:

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

问题:

我需要来自响应数据中 extensions 字段的调试信息.我在谈论这个 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 服务器配置中启用了 tracingcacheControl,但是 extensions 字段仍然被排除在响应数据中.如何取回 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;
  }
});

doc

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

08-18 11:00