handler.go

 1package graphql
 2
 3import (
 4	"context"
 5	"net/http"
 6
 7	"github.com/MichaelMure/git-bug/repository"
 8	"github.com/graphql-go/handler"
 9)
10
11type Handler struct {
12	Handler *handler.Handler
13	Repo    repository.Repo
14}
15
16func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
17	ctx := context.WithValue(r.Context(), "repo", h.Repo)
18	h.Handler.ContextHandler(ctx, w, r)
19}
20
21func NewHandler(repo repository.Repo) (*Handler, error) {
22	schema, err := graphqlSchema()
23
24	if err != nil {
25		return nil, err
26	}
27
28	return &Handler{
29		Handler: handler.New(&handler.Config{
30			Schema:   &schema,
31			Pretty:   true,
32			GraphiQL: true,
33		}),
34		Repo: repo,
35	}, nil
36}