Skip to content

handler: replaces Handler.Schema with Handler.Params (graphql.Params) #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion graphcoolPlayground_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/handler"
)
Expand Down Expand Up @@ -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,
})
Expand Down
5 changes: 4 additions & 1 deletion graphiql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"testing"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/handler"
)
Expand Down Expand Up @@ -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,
})

Expand Down
68 changes: 18 additions & 50 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"strings"

"github.com/graphql-go/graphql"

"context"
)

const (
Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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,
}
}
81 changes: 60 additions & 21 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))
}
}