From 074156634b47bfb6141f20a26ec38a386f9ba1b1 Mon Sep 17 00:00:00 2001 From: Michael Muré Date: Wed, 25 Jul 2018 21:26:25 +0200 Subject: add a cache to support the graphql API and the future interactive CLI UI --- graphql/handler.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) (limited to 'graphql/handler.go') diff --git a/graphql/handler.go b/graphql/handler.go index b49ca56b..89ef3801 100644 --- a/graphql/handler.go +++ b/graphql/handler.go @@ -4,18 +4,19 @@ import ( "context" "net/http" + "github.com/MichaelMure/git-bug/cache" "github.com/MichaelMure/git-bug/repository" - "github.com/graphql-go/handler" + graphqlHandler "github.com/graphql-go/handler" ) type Handler struct { - Handler *handler.Handler - Repo repository.Repo + handler *graphqlHandler.Handler + cache cache.Cache } func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - ctx := context.WithValue(r.Context(), "repo", h.Repo) - h.Handler.ContextHandler(ctx, w, r) + ctx := context.WithValue(r.Context(), "cache", h.cache) + h.handler.ContextHandler(ctx, w, r) } func NewHandler(repo repository.Repo) (*Handler, error) { @@ -25,12 +26,17 @@ func NewHandler(repo repository.Repo) (*Handler, error) { return nil, err } + h := graphqlHandler.New(&graphqlHandler.Config{ + Schema: &schema, + Pretty: true, + GraphiQL: true, + }) + + c := cache.NewDefaultCache() + c.RegisterDefaultRepository(repo) + return &Handler{ - Handler: handler.New(&handler.Config{ - Schema: &schema, - Pretty: true, - GraphiQL: true, - }), - Repo: repo, + handler: h, + cache: c, }, nil } -- cgit