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	"time"
10
11	"github.com/99designs/gqlgen/graphql/handler"
12	"github.com/99designs/gqlgen/graphql/handler/extension"
13	"github.com/99designs/gqlgen/graphql/handler/lru"
14	"github.com/99designs/gqlgen/graphql/handler/transport"
15	"github.com/gorilla/websocket"
16	"github.com/vektah/gqlparser/v2/ast"
17
18	"github.com/git-bug/git-bug/api/graphql/graph"
19	"github.com/git-bug/git-bug/api/graphql/resolvers"
20	"github.com/git-bug/git-bug/cache"
21)
22
23// Handler is the root GraphQL http handler
24type Handler struct {
25	http.Handler
26	io.Closer
27}
28
29// ServerConfig carries server-level configuration that is passed down to
30// GraphQL resolvers. It is constructed once at startup and does not change.
31type ServerConfig struct {
32	// AuthMode is one of "local", "oauth", or "readonly".
33	AuthMode string
34	// OAuthProviders lists the names of enabled OAuth providers, e.g. ["github"].
35	OAuthProviders []string
36}
37
38func NewHandler(mrc *cache.MultiRepoCache, cfg ServerConfig, errorOut io.Writer) Handler {
39	rootResolver := resolvers.NewRootResolver(mrc, cfg.AuthMode, cfg.OAuthProviders)
40	config := graph.Config{Resolvers: rootResolver}
41
42	h := handler.New(graph.NewExecutableSchema(config))
43
44	h.AddTransport(transport.Websocket{
45		KeepAlivePingInterval: 10 * time.Second,
46		Upgrader: websocket.Upgrader{
47			CheckOrigin: func(r *http.Request) bool { return true },
48		},
49	})
50	h.AddTransport(transport.Options{})
51	h.AddTransport(transport.GET{})
52	h.AddTransport(transport.POST{})
53	h.AddTransport(transport.MultipartForm{})
54
55	h.SetQueryCache(lru.New[*ast.QueryDocument](1000))
56
57	h.Use(extension.Introspection{})
58	h.Use(extension.AutomaticPersistedQuery{
59		Cache: lru.New[string](100),
60	})
61
62	if errorOut != nil {
63		h.Use(&Tracer{Out: errorOut})
64	}
65
66	return Handler{
67		Handler: h,
68		Closer:  rootResolver,
69	}
70}