Usage reporting to GraphQL Hive for gqlgen.
go get github.com/enisdenjo/go-gqlhive@latest
After getting started with gqlgen add the tracer to the server.
package main
import (
"log"
"net/http"
"os"
"github.com/enisdenjo/go-gqlhive/graphql/handler"
"github.com/enisdenjo/go-gqlhive/graphql/playground"
"github.com/enisdenjo/go-gqlhive"
"github.com/enisdenjo/go-gqlhive/internal/fixtures/todos/graph"
)
const defaultPort = "8080"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
// 👇 use the gqlhive tracer with your token
srv.Use(gqlhive.NewTracer("<your-graphql-hive-token>"))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
See traceroptions.go for configuring the tracer.
For example:
package main
import (
"context"
"log"
"net/http"
"os"
"time"
"github.com/enisdenjo/go-gqlhive/graphql/handler"
"github.com/enisdenjo/go-gqlhive/graphql/playground"
"github.com/domonda/go-types/nullable"
"github.com/enisdenjo/go-gqlhive"
"github.com/enisdenjo/go-gqlhive/internal/fixtures/todos/graph"
)
const defaultPort = "8080"
func main() {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}}))
// 👇 use the gqlhive tracer with your token and custom options
srv.Use(gqlhive.NewTracer("<your-graphql-hive-token>",
gqlhive.WithEndpoint("http://localhost"),
gqlhive.WithGenerateID(func(operation string, operationName nullable.TrimmedString) string {
// custom unique ID generation for operations
}),
gqlhive.WithSendReportTimeout(5*time.Second),
gqlhive.WithSendReport(func(ctx context.Context, endpoint, token string, report *gqlhive.Report) error {
// custom report sender for queued reports
}),
))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}