`Extensions` field not shown in apollo graphql response data

我们两清 提交于 2021-02-10 13:20:30

问题


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

You can run queries like:

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

Problem:

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":{....}

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

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


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



来源:https://stackoverflow.com/questions/53367351/extensions-field-not-shown-in-apollo-graphql-response-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!