diff --git a/graphcoolPlayground_test.go b/graphcoolPlayground_test.go index 8d42fc4..7d64254 100644 --- a/graphcoolPlayground_test.go +++ b/graphcoolPlayground_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "github.com/graphql-go/graphql" "github.com/graphql-go/graphql/testutil" "github.com/graphql-go/handler" ) @@ -62,7 +63,9 @@ func TestRenderPlayground(t *testing.T) { req.Header.Set("Accept", tc.accept) h := handler.New(&handler.Config{ - Schema: &testutil.StarWarsSchema, + Params: graphql.Params{ + Schema: testutil.StarWarsSchema, + }, GraphiQL: false, Playground: tc.playgroundEnabled, }) diff --git a/graphiql_test.go b/graphiql_test.go index 25ad929..8fe2adf 100644 --- a/graphiql_test.go +++ b/graphiql_test.go @@ -6,6 +6,7 @@ import ( "strings" "testing" + "github.com/graphql-go/graphql" "github.com/graphql-go/graphql/testutil" "github.com/graphql-go/handler" ) @@ -62,7 +63,9 @@ func TestRenderGraphiQL(t *testing.T) { req.Header.Set("Accept", tc.accept) h := handler.New(&handler.Config{ - Schema: &testutil.StarWarsSchema, + Params: graphql.Params{ + Schema: testutil.StarWarsSchema, + }, GraphiQL: tc.graphiqlEnabled, }) diff --git a/handler.go b/handler.go index eebd0a3..94f7bf6 100644 --- a/handler.go +++ b/handler.go @@ -8,8 +8,6 @@ import ( "strings" "github.com/graphql-go/graphql" - - "context" ) const ( @@ -19,9 +17,9 @@ const ( ) type Handler struct { - Schema *graphql.Schema - pretty bool - graphiql bool + Params graphql.Params + pretty bool + graphiql bool playground bool } type RequestOptions struct { @@ -114,27 +112,18 @@ func NewRequestOptions(r *http.Request) *RequestOptions { } } -// ContextHandler provides an entrypoint into executing graphQL queries with a -// user-provided context. -func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { - // get query +// ServeHTTP provides an entrypoint into executing graphQL queries. +func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { opts := NewRequestOptions(r) - - // execute graphql query - params := graphql.Params{ - Schema: *h.Schema, - RequestString: opts.Query, - VariableValues: opts.Variables, - OperationName: opts.OperationName, - Context: ctx, - } - result := graphql.Do(params) - + h.Params.RequestString = opts.Query + h.Params.VariableValues = opts.Variables + h.Params.OperationName = opts.OperationName + result := graphql.Do(h.Params) if h.graphiql { acceptHeader := r.Header.Get("Accept") _, raw := r.URL.Query()["raw"] if !raw && !strings.Contains(acceptHeader, "application/json") && strings.Contains(acceptHeader, "text/html") { - renderGraphiQL(w, params) + renderGraphiQL(w, h.Params) return } } @@ -164,39 +153,18 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r * } } -// ServeHTTP provides an entrypoint into executing graphQL queries. -func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - h.ContextHandler(r.Context(), w, r) -} - type Config struct { - Schema *graphql.Schema - Pretty bool - GraphiQL bool + Params graphql.Params + Pretty bool + GraphiQL bool Playground bool } -func NewConfig() *Config { - return &Config{ - Schema: nil, - Pretty: true, - GraphiQL: true, - Playground: false, - } -} - -func New(p *Config) *Handler { - if p == nil { - p = NewConfig() - } - if p.Schema == nil { - panic("undefined GraphQL schema") - } - +func New(c *Config) *Handler { return &Handler{ - Schema: p.Schema, - pretty: p.Pretty, - graphiql: p.GraphiQL, - playground: p.Playground, + Params: c.Params, + pretty: c.Pretty, + graphiql: c.GraphiQL, + playground: c.Playground, } } diff --git a/handler_test.go b/handler_test.go index ea73d2a..a0a5f26 100644 --- a/handler_test.go +++ b/handler_test.go @@ -67,14 +67,19 @@ func TestContextPropagated(t *testing.T) { queryString := `query={name}` req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil) + ctx := context.WithValue(context.Background(), "name", "context-data") h := handler.New(&handler.Config{ - Schema: &myNameSchema, + Params: graphql.Params{ + Schema: myNameSchema, + Context: ctx, + }, Pretty: true, }) - ctx := context.WithValue(context.Background(), "name", "context-data") resp := httptest.NewRecorder() - h.ContextHandler(ctx, resp, req) + + h.ServeHTTP(resp, req) + result := decodeResponse(t, resp) if resp.Code != http.StatusOK { t.Fatalf("unexpected server response %v", resp.Code) @@ -96,7 +101,9 @@ func TestHandler_BasicQuery_Pretty(t *testing.T) { req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil) h := handler.New(&handler.Config{ - Schema: &testutil.StarWarsSchema, + Params: graphql.Params{ + Schema: testutil.StarWarsSchema, + }, Pretty: true, }) result, resp := executeTest(t, h, req) @@ -120,7 +127,9 @@ func TestHandler_BasicQuery_Ugly(t *testing.T) { req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil) h := handler.New(&handler.Config{ - Schema: &testutil.StarWarsSchema, + Params: graphql.Params{ + Schema: testutil.StarWarsSchema, + }, Pretty: false, }) result, resp := executeTest(t, h, req) @@ -132,21 +141,51 @@ func TestHandler_BasicQuery_Ugly(t *testing.T) { } } -func TestHandler_Params_NilParams(t *testing.T) { - defer func() { - if r := recover(); r != nil { - if str, ok := r.(string); ok { - if str != "undefined GraphQL schema" { - t.Fatalf("unexpected error, got %v", r) - } - // test passed - return - } - t.Fatalf("unexpected error, got %v", r) - - } - t.Fatalf("expected to panic, did not panic") - }() - _ = handler.New(nil) +func TestHandler_Params_RootObject(t *testing.T) { + rootObject := map[string]interface{}{ + "foo": "bar", + } + + myNameQuery := graphql.NewObject(graphql.ObjectConfig{ + Name: "Query", + Fields: graphql.Fields{ + "name": &graphql.Field{ + Name: "name", + Type: graphql.String, + Resolve: func(p graphql.ResolveParams) (interface{}, error) { + return p.Info.RootValue.(map[string]interface{})["foo"], nil + }, + }, + }, + }) + myNameSchema, err := graphql.NewSchema(graphql.SchemaConfig{ + Query: myNameQuery, + }) + if err != nil { + t.Fatal(err) + } + + expected := &graphql.Result{ + Data: map[string]interface{}{ + "name": rootObject["foo"], + }, + } + queryString := `query={name}` + req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil) + h := handler.New(&handler.Config{ + Params: graphql.Params{ + Schema: myNameSchema, + RootObject: rootObject, + }, + Pretty: true, + }) + + result, resp := executeTest(t, h, req) + if resp.Code != http.StatusOK { + t.Fatalf("unexpected server response %v", resp.Code) + } + if !reflect.DeepEqual(result, expected) { + t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result)) + } }