0.0.0-alpha.107 • Published 11 days ago

@graphql-debugger/trace-directive v0.0.0-alpha.107

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

@graphql-debugger/trace-directive

npm version License: MIT

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:

Traces

Getting Started

Running Jaeger UI

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

License

MIT - Rocket Connect - https://github.com/rocket-connect

0.0.0-alpha.107

11 days ago

0.0.0-alpha.106

12 days ago

0.0.0-alpha.105

12 days ago

0.0.0-alpha.104

13 days ago

0.0.0-alpha.103

14 days ago

0.0.0-alpha.102

16 days ago

0.0.0-alpha.101

16 days ago

0.0.0-alpha.100

25 days ago

0.0.0-alpha.99

3 months ago

0.0.0-alpha.98

3 months ago

0.0.0-alpha.97

3 months ago

0.0.0-alpha.93

3 months ago

0.0.0-alpha.92

3 months ago

0.0.0-alpha.91

3 months ago

0.0.0-alpha.90

3 months ago

0.0.0-alpha.96

3 months ago

0.0.0-alpha.95

3 months ago

0.0.0-alpha.94

3 months ago

0.0.0-alpha.89

3 months ago

0.0.0-alpha.88

3 months ago

0.0.0-alpha.86

4 months ago

0.0.0-alpha.84

4 months ago

0.0.0-alpha.82

4 months ago

0.0.0-alpha.81

4 months ago

0.0.0-alpha.80

4 months ago

0.0.0-alpha.83

4 months ago

0.0.0-alpha.79

4 months ago

0.0.0-alpha.78

4 months ago

0.0.0-alpha.77

4 months ago

0.0.0-alpha.75

5 months ago

0.0.0-alpha.74

5 months ago

0.0.0-alpha.73

5 months ago

0.0.0-alpha.76

5 months ago

0.0.0-alpha.72

5 months ago

0.0.0-alpha.71

5 months ago

0.0.0-alpha.70

6 months ago

0.0.0-alpha.69

6 months ago

0.0.0-alpha.68

6 months ago

0.0.0-alpha.67

6 months ago

0.0.0-alpha.66

6 months ago

0.0.0-alpha.65

6 months ago

0.0.0-alpha.64

6 months ago

0.0.0-alpha.63

6 months ago

0.0.0-alpha.62

7 months ago

0.0.0-alpha.61

7 months ago

0.0.0-alpha.60

7 months ago

0.0.0-alpha.59

7 months ago

0.0.0-alpha.58

7 months ago

0.0.0-alpha.57

7 months ago

0.0.0-alpha.56

7 months ago

0.0.0-alpha.55

7 months ago

0.0.0-alpha.54

7 months ago

0.0.0-alpha.53

7 months ago

0.0.0-alpha.52

7 months ago

0.0.0-alpha.51

7 months ago

0.0.0-alpha.50

7 months ago

0.0.0-alpha.49

7 months ago

0.0.0-alpha.48

8 months ago

0.0.0-alpha.47

8 months ago