git-bug/graphql/handler.go

43 lines
789 B
Go
Raw Normal View History

2018-07-19 15:15:50 +03:00
package graphql
import (
"context"
"net/http"
2018-07-19 15:15:50 +03:00
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/repository"
graphqlHandler "github.com/graphql-go/handler"
)
type Handler struct {
handler *graphqlHandler.Handler
2018-07-27 02:58:38 +03:00
cache cache.Cacher
}
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()
2018-07-19 15:15:50 +03:00
if err != nil {
return nil, err
}
h := graphqlHandler.New(&graphqlHandler.Config{
Schema: &schema,
Pretty: true,
GraphiQL: true,
})
2018-07-27 02:58:38 +03:00
c := cache.NewCache()
c.RegisterDefaultRepository(repo)
return &Handler{
handler: h,
cache: c,
}, nil
2018-07-19 15:15:50 +03:00
}