Red*_*ant 5 node.js express apollo graphql apollo-server
这是一个可重现的示例。在http://localhost:4000/graphql上运行app.js并导航游乐场
您可以运行如下查询:
query RecipeQuery{
recipe(title:"Recipe 2"){
description
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
我需要extensions响应数据中现场的调试信息。我说的是这个extensions领域:
"data":{....},
"extensions": {
"tracing": {}
"cacheControl":{}
}
Run Code Online (Sandbox Code Playgroud)
但实际上,我只获取数据字段:
"data":{....}
Run Code Online (Sandbox Code Playgroud)
我已经在 apollo 服务器配置中启用了tracing和,但该字段仍然被排除在响应数据中。我怎样才能取回数据?cacheControlextensionsextensions
以下是阿波罗引擎的启动方式:
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}`),
);
Run Code Online (Sandbox Code Playgroud)
依赖关系
"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"
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 apollo 格式化响应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;
}
});
Run Code Online (Sandbox Code Playgroud)