aboutsummaryrefslogblamecommitdiffstats
path: root/graphql/handler.go
blob: 89ef3801808813a8c2f61692892bb13121dfc046 (plain) (tree)
1
2
3
4
5
6
7
8
9

               


                  
 
                                              
                                                   
                                                      


                     

                                       


                                                                     

                                                               



                                                         




                               








                                                       
                        

                           
              
 
package graphql

import (
	"context"
	"net/http"

	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/repository"
	graphqlHandler "github.com/graphql-go/handler"
)

type Handler struct {
	handler *graphqlHandler.Handler
	cache   cache.Cache
}

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := context.WithValue(r.Context(), "cache", h.cache)
	h.handler.ContextHandler(ctx, w, r)
}

func NewHandler(repo repository.Repo) (*Handler, error) {
	schema, err := graphqlSchema()

	if err != nil {
		return nil, err
	}

	h := graphqlHandler.New(&graphqlHandler.Config{
		Schema:   &schema,
		Pretty:   true,
		GraphiQL: true,
	})

	c := cache.NewDefaultCache()
	c.RegisterDefaultRepository(repo)

	return &Handler{
		handler: h,
		cache:   c,
	}, nil
}