From 794217b96849077338a4dde3de03048d1dafe9ca Mon Sep 17 00:00:00 2001 From: Stephen Hitchner Date: Thu, 17 May 2018 13:29:16 -0700 Subject: [PATCH 1/2] Using writer directly for marshalling --- handler.go | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/handler.go b/handler.go index cfcb1ca..d374b67 100644 --- a/handler.go +++ b/handler.go @@ -140,18 +140,14 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r * // use proper JSON Header w.Header().Add("Content-Type", "application/json; charset=utf-8") - + w.WriteHeader(http.StatusOK) + + enc := json.NewEncoder(w) if h.pretty { - w.WriteHeader(http.StatusOK) - buff, _ := json.MarshalIndent(result, "", "\t") - - w.Write(buff) - } else { - w.WriteHeader(http.StatusOK) - buff, _ := json.Marshal(result) - - w.Write(buff) + enc.SetIndent("", "\t") } + // Not much you can do with this error. + _ = enc.Encode(result) } // ServeHTTP provides an entrypoint into executing graphQL queries. From e70c16a3a49ed33bca21dffdae6c4dcd93e8d41f Mon Sep 17 00:00:00 2001 From: sjhitchner Date: Thu, 17 May 2018 13:48:07 -0700 Subject: [PATCH 2/2] fixing weirdness with tests only testing root repo not forked repos --- graphiql_test.go | 5 ++--- handler_test.go | 14 +++++++------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/graphiql_test.go b/graphiql_test.go index 25ad929..10a6ce0 100644 --- a/graphiql_test.go +++ b/graphiql_test.go @@ -1,4 +1,4 @@ -package handler_test +package handler import ( "net/http" @@ -7,7 +7,6 @@ import ( "testing" "github.com/graphql-go/graphql/testutil" - "github.com/graphql-go/handler" ) func TestRenderGraphiQL(t *testing.T) { @@ -61,7 +60,7 @@ func TestRenderGraphiQL(t *testing.T) { req.Header.Set("Accept", tc.accept) - h := handler.New(&handler.Config{ + h := New(&Config{ Schema: &testutil.StarWarsSchema, GraphiQL: tc.graphiqlEnabled, }) diff --git a/handler_test.go b/handler_test.go index ea73d2a..d57678a 100644 --- a/handler_test.go +++ b/handler_test.go @@ -1,4 +1,4 @@ -package handler_test +package handler import ( "encoding/json" @@ -13,7 +13,6 @@ import ( "context" "github.com/graphql-go/graphql" "github.com/graphql-go/graphql/testutil" - "github.com/graphql-go/handler" ) func decodeResponse(t *testing.T, recorder *httptest.ResponseRecorder) *graphql.Result { @@ -32,7 +31,8 @@ func decodeResponse(t *testing.T, recorder *httptest.ResponseRecorder) *graphql. } return &target } -func executeTest(t *testing.T, h *handler.Handler, req *http.Request) (*graphql.Result, *httptest.ResponseRecorder) { + +func executeTest(t *testing.T, h *Handler, req *http.Request) (*graphql.Result, *httptest.ResponseRecorder) { resp := httptest.NewRecorder() h.ServeHTTP(resp, req) result := decodeResponse(t, resp) @@ -67,7 +67,7 @@ func TestContextPropagated(t *testing.T) { queryString := `query={name}` req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil) - h := handler.New(&handler.Config{ + h := New(&Config{ Schema: &myNameSchema, Pretty: true, }) @@ -95,7 +95,7 @@ func TestHandler_BasicQuery_Pretty(t *testing.T) { queryString := `query=query HeroNameQuery { hero { name } }` req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil) - h := handler.New(&handler.Config{ + h := New(&Config{ Schema: &testutil.StarWarsSchema, Pretty: true, }) @@ -119,7 +119,7 @@ func TestHandler_BasicQuery_Ugly(t *testing.T) { queryString := `query=query HeroNameQuery { hero { name } }` req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil) - h := handler.New(&handler.Config{ + h := New(&Config{ Schema: &testutil.StarWarsSchema, Pretty: false, }) @@ -147,6 +147,6 @@ func TestHandler_Params_NilParams(t *testing.T) { } t.Fatalf("expected to panic, did not panic") }() - _ = handler.New(nil) + _ = New(nil) }