Enable Tracing in Apollo Server
This code sample demonstrates how to enable tracing in Apollo Server. By setting the `tracing` option to `true` and using `addTracingToResolvers`, you can collect detailed performance data for each GraphQL request.
const { ApolloServer, gql } = require('apollo-server');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const { addTracingToResolvers } = require('apollo-tracing');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const schema = makeExecutableSchema({ typeDefs, resolvers });
addTracingToResolvers(schema);
const server = new ApolloServer({
schema,
tracing: true,
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Accessing Tracing Data
This code sample shows how to access tracing data in the response of a GraphQL query. When tracing is enabled, the response will include an `extensions` field containing detailed timing information for each resolver.
const { ApolloServer, gql } = require('apollo-server');
const typeDefs = gql`
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
tracing: true,
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
// Example query to access tracing data
// query {
// hello
// }
// The response will include a `extensions` field with tracing data
// {
// "data": {
// "hello": "Hello world!"
// },
// "extensions": {
// "tracing": {
// "version": 1,
// "startTime": "2023-10-01T00:00:00.000Z",
// "endTime": "2023-10-01T00:00:00.100Z",
// "duration": 100000000,
// "execution": {
// "resolvers": [
// {
// "path": ["hello"],
// "parentType": "Query",
// "fieldName": "hello",
// "returnType": "String",
// "startOffset": 0,
// "duration": 100000000
// }
// ]
// }
// }
// }
// }