handler.go

 1//go:generate go tool gqlgen generate
 2
 3// Package graphql contains the root GraphQL http handler
 4package graphql
 5
 6import (
 7	"io"
 8	"net/http"
 9
10	"github.com/99designs/gqlgen/graphql/handler"
11
12	"github.com/git-bug/git-bug/api/graphql/graph"
13	"github.com/git-bug/git-bug/api/graphql/resolvers"
14	"github.com/git-bug/git-bug/cache"
15)
16
17// Handler is the root GraphQL http handler
18type Handler struct {
19	http.Handler
20	io.Closer
21}
22
23func NewHandler(mrc *cache.MultiRepoCache, errorOut io.Writer) Handler {
24	rootResolver := resolvers.NewRootResolver(mrc)
25	config := graph.Config{Resolvers: rootResolver}
26
27	// TODO? https://gqlgen.com/recipes/subscriptions/ says to configure a WS upgrader, but
28	// handler.NewDefaultServer doesn't do it.
29	h := handler.NewDefaultServer(graph.NewExecutableSchema(config))
30
31	if errorOut != nil {
32		h.Use(&Tracer{Out: errorOut})
33	}
34
35	return Handler{
36		Handler: h,
37		Closer:  rootResolver,
38	}
39}