2018-07-19 15:15:50 +03:00
|
|
|
package graphql
|
|
|
|
|
2018-07-20 01:25:30 +03:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2018-07-19 15:15:50 +03:00
|
|
|
|
2018-07-25 22:26:25 +03:00
|
|
|
"github.com/MichaelMure/git-bug/cache"
|
2018-07-20 01:25:30 +03:00
|
|
|
"github.com/MichaelMure/git-bug/repository"
|
2018-07-25 22:26:25 +03:00
|
|
|
graphqlHandler "github.com/graphql-go/handler"
|
2018-07-20 01:25:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
2018-07-25 22:26:25 +03:00
|
|
|
handler *graphqlHandler.Handler
|
2018-07-27 02:58:38 +03:00
|
|
|
cache cache.Cacher
|
2018-07-20 01:25:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2018-07-25 22:26:25 +03:00
|
|
|
ctx := context.WithValue(r.Context(), "cache", h.cache)
|
|
|
|
h.handler.ContextHandler(ctx, w, r)
|
2018-07-20 01:25:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandler(repo repository.Repo) (*Handler, error) {
|
|
|
|
schema, err := graphqlSchema()
|
2018-07-19 15:15:50 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-25 22:26:25 +03:00
|
|
|
h := graphqlHandler.New(&graphqlHandler.Config{
|
|
|
|
Schema: &schema,
|
|
|
|
Pretty: true,
|
|
|
|
GraphiQL: true,
|
|
|
|
})
|
|
|
|
|
2018-07-27 02:58:38 +03:00
|
|
|
c := cache.NewCache()
|
2018-07-25 22:26:25 +03:00
|
|
|
c.RegisterDefaultRepository(repo)
|
|
|
|
|
2018-07-20 01:25:30 +03:00
|
|
|
return &Handler{
|
2018-07-25 22:26:25 +03:00
|
|
|
handler: h,
|
|
|
|
cache: c,
|
2018-07-20 01:25:30 +03:00
|
|
|
}, nil
|
2018-07-19 15:15:50 +03:00
|
|
|
}
|