Skip to content

Commit

Permalink
Merge pull request #42 from puppetlabs/handler_rootobject_initialization
Browse files Browse the repository at this point in the history
Allows a RootObject to be generated per request
  • Loading branch information
chris-ramon committed Jul 12, 2018
2 parents b7e0284 + 8588110 commit 59006c0
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 12 deletions.
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))
}
}

0 comments on commit 59006c0

Please sign in to comment.