enisdenjo / go-gqlhive

Usage reporting to GraphQL Hive for gqlgen

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gqlhive Go Report Card Go Reference

Usage reporting to GraphQL Hive for gqlgen.

Getting started

Install

go get github.com/enisdenjo/go-gqlhive@latest

Use

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))
}

Configure

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))
}

About

Usage reporting to GraphQL Hive for gqlgen

License:MIT License


Languages

Language:Go 100.0%