Skip to content

Allows a RootObject to be generated per request #42

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

Merged
Merged
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
33 changes: 21 additions & 12 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ const (
)

type Handler struct {
Schema *graphql.Schema
pretty bool
graphiql bool
playground bool
Schema *graphql.Schema
pretty bool
graphiql bool
playground bool
rootObjectFn RootObjectFn
}
type RequestOptions struct {
Query string `json:"query" url:"query" schema:"query"`
Expand Down Expand Up @@ -128,6 +129,9 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
OperationName: opts.OperationName,
Context: ctx,
}
if h.rootObjectFn != nil {
params.RootObject = h.rootObjectFn(ctx, r)
}
result := graphql.Do(params)

if h.graphiql {
Expand Down Expand Up @@ -169,11 +173,15 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.ContextHandler(r.Context(), w, r)
}

// RootObjectFn allows a user to generate a RootObject per request
type RootObjectFn func(ctx context.Context, r *http.Request) map[string]interface{}

type Config struct {
Schema *graphql.Schema
Pretty bool
GraphiQL bool
Playground bool
Schema *graphql.Schema
Pretty bool
GraphiQL bool
Playground bool
RootObjectFn RootObjectFn
}

func NewConfig() *Config {
Expand All @@ -194,9 +202,10 @@ func New(p *Config) *Handler {
}

return &Handler{
Schema: p.Schema,
pretty: p.Pretty,
graphiql: p.GraphiQL,
playground: p.Playground,
Schema: p.Schema,
pretty: p.Pretty,
graphiql: p.GraphiQL,
playground: p.Playground,
rootObjectFn: p.RootObjectFn,
}
}
46 changes: 46 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

"context"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/handler"
Expand Down Expand Up @@ -150,3 +151,48 @@ func TestHandler_Params_NilParams(t *testing.T) {
_ = handler.New(nil)

}

func TestHandler_BasicQuery_WithRootObjFn(t *testing.T) {
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) {
rv := p.Info.RootValue.(map[string]interface{})
return rv["rootValue"], nil
},
},
},
})
myNameSchema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: myNameQuery,
})
if err != nil {
t.Fatal(err)
}

expected := &graphql.Result{
Data: map[string]interface{}{
"name": "foo",
},
}
queryString := `query={name}`
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)

h := handler.New(&handler.Config{
Schema: &myNameSchema,
Pretty: true,
RootObjectFn: func(ctx context.Context, r *http.Request) map[string]interface{} {
return map[string]interface{}{"rootValue": "foo"}
},
})
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))
}
}