From d2b96f8d7aa2e13a1a85342acd20dc5eaae0a77b Mon Sep 17 00:00:00 2001 From: Dustin Oprea Date: Mon, 29 Aug 2016 08:49:18 -0400 Subject: [PATCH 1/3] Added support for a context-generator. --- handler.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/handler.go b/handler.go index 96483a8..8bf255f 100644 --- a/handler.go +++ b/handler.go @@ -21,8 +21,10 @@ const ( type Handler struct { Schema *graphql.Schema + cg *ContextGenerator pretty bool } + type RequestOptions struct { Query string `json:"query" url:"query" schema:"query"` Variables map[string]interface{} `json:"variables" url:"variables" schema:"variables"` @@ -143,6 +145,22 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r * } } +type ContextGenerator interface { + MakeContext(w http.ResponseWriter, r *http.Request) (context.Context, error) +} + +func (h *Handler) SetContextGenerator(cg *ContextGenerator) { + h.cg = cg +} + +func (h *Handler) GetContext(w http.ResponseWriter, r *http.Request) (ctx context.Context, err error) { + if h.cg != nil { + return h.cg.MakeContext(w, r) + } else { + return context.Background(), nil + } +} + // ServeHTTP provides an entrypoint into executing graphQL queries. func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.ContextHandler(context.Background(), w, r) From 256f38f2f1d96f833c19799a9f81fa5e19ebb27c Mon Sep 17 00:00:00 2001 From: Dustin Oprea Date: Mon, 29 Aug 2016 08:59:11 -0400 Subject: [PATCH 2/3] Can now equip handler with a context generator. --- handler.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/handler.go b/handler.go index 8bf255f..d4003f1 100644 --- a/handler.go +++ b/handler.go @@ -21,7 +21,7 @@ const ( type Handler struct { Schema *graphql.Schema - cg *ContextGenerator + rcg RequestContextGenerator pretty bool } @@ -145,17 +145,17 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r * } } -type ContextGenerator interface { +type RequestContextGenerator interface { MakeContext(w http.ResponseWriter, r *http.Request) (context.Context, error) } -func (h *Handler) SetContextGenerator(cg *ContextGenerator) { - h.cg = cg +func (h *Handler) SetRequestContextGenerator(rcg RequestContextGenerator) { + h.rcg = rcg } func (h *Handler) GetContext(w http.ResponseWriter, r *http.Request) (ctx context.Context, err error) { - if h.cg != nil { - return h.cg.MakeContext(w, r) + if h.rcg != nil { + return h.rcg.MakeContext(w, r) } else { return context.Background(), nil } @@ -163,7 +163,11 @@ func (h *Handler) GetContext(w http.ResponseWriter, r *http.Request) (ctx contex // ServeHTTP provides an entrypoint into executing graphQL queries. func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - h.ContextHandler(context.Background(), w, r) + if ctx, err := h.GetContext(w, r); err != nil { + panic(err) + } else { + h.ContextHandler(ctx, w, r) + } } type Config struct { From b05c256270fb773dfd41f95d7f5f3ff675c9a7e2 Mon Sep 17 00:00:00 2001 From: Dustin Oprea Date: Mon, 29 Aug 2016 09:04:05 -0400 Subject: [PATCH 3/3] Added RequestContextGenerator to documentation. --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/README.md b/README.md index 96cc881..d34ee08 100644 --- a/README.md +++ b/README.md @@ -32,6 +32,48 @@ func main() { } ``` +By default, `context.Background()` is used to generate contexts. If you need to provide an alternative method to support environments, like AppEngine, you can provide a RequestContextGenerator. + +```go +package main + +import ( + "net/http" + + "golang.org/x/net/context" + "github.com/graphql-go/handler" + "google.golang.org/appengine" +) + +type contextGenerator struct { + +} + +func (cg *contextGenerator) MakeContext(w http.ResponseWriter, r *http.Request) (context.Context, error) { + ctx := appengine.NewContext(r) + + return ctx, nil +} + +func main() { + + // define GraphQL schema using relay library helpers + schema := graphql.NewSchema(...) + + h := handler.New(&handler.Config{ + Schema: schema, + Pretty: true, + }) + + cg := &contextGenerator{} + h.SetRequestContextGenerator(cg) + + // serve HTTP + http.Handle("/graphql", h) + http.ListenAndServe(":8080", nil) +} +``` + ### Details The handler will accept requests with