@graphql-debugger/trace-directive
v0.0.0-alpha.109
Published
- [graphql-debugger.com](http://www.graphql-debugger.com)
Downloads
50
Readme
@graphql-debugger/trace-directive
About
Place the @trace
directive on any fields you wish to trace.
type User {
name: String
age: Int
balance: Int @trace
posts: [Post] @trace
}
type Post {
title: String
comments: [Comment] @trace
}
type Comment {
content: String
}
Given the query:
query {
users {
name
balance
posts {
title
comments {
content
}
}
}
}
Outputting the following traces:
Getting Started
Running Jaeger UI
- https://www.jaegertracing.io/
This is an open-source collector, and it comes with a graphical interface. You collect the traces and spans from your GraphQL server and send export them to here. Then, once they are sent, you can visualize them like the image above.
To start this interface, I suggest you use Docker. Here is an all-in-one script to start jager.
docker run --rm --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-e COLLECTOR_OTLP_ENABLED=true \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 4317:4317 \
-p 4318:4318 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one:1.35
Then you can go to http://localhost:16686/ to open the UI.
Boilerplate
Quickstart boilerplate.
index.ts:
import {
GraphQLDebuggerContext,
setupOtel,
traceDirective,
} from "@graphql-debugger/trace-directive";
import { makeExecutableSchema } from "@graphql-tools/schema";
import { createServer } from "@graphql-yoga/node";
import util from "util";
setupOtel();
const sleep = util.promisify(setTimeout);
const typeDefs = `
type User {
name: String
age: Int
balance: Int @trace
posts: [Post] @trace
}
type Post {
title: String
comments: [Comment] @trace
}
type Comment {
content: String
}
type Query {
users: [User] @trace
}
`;
const resolvers = {
Query: {
users: async () => {
await sleep(200);
return [{ name: "Dan", age: 23 }];
},
},
User: {
balance: async () => {
await sleep(100);
return 100;
},
posts: async () => {
await sleep(500);
return [{ title: "Beer Is Cool" }];
},
},
Post: {
comments: async () => {
await sleep(300);
return [
{
content: "I also think beer is cool",
},
];
},
},
};
const trace = traceDirective();
let schema = makeExecutableSchema({
typeDefs: [typeDefs, trace.typeDefs],
resolvers,
});
schema = trace.transformer(schema);
const server = createServer({
schema,
port: 5000,
context: {
GraphQLDebuggerContext: new GraphQLDebuggerContext({
schema,
}),
},
});
server
.start()
.then(() => console.log("server online"))
.catch(console.error);
Context Value
Inject the GraphQLDebuggerContext
instance inside your GraphQL request context.
import { GraphQLDebuggerContext } from "@graphql-debugger/trace-directive";
const myServer = new GraphQLServerFooBar({
schema,
context: {
GraphQLDebuggerContext: new GraphQLDebuggerContext({
schema,
}),
},
});
Exporting traces
This package does not export traces to your collector, you must set this up yourself. Checkout the quickstart boilerplate setup-otel
file in this document.
Resources
- https://www.jaegertracing.io/
- https://opentelemetry.io/
- https://www.the-guild.dev/graphql/tools/docs/schema-directives
License
MIT - Rocket Connect - https://github.com/rocket-connect