Skip to content

Commit 59006c0

Browse files
authored
Merge pull request #42 from puppetlabs/handler_rootobject_initialization
Allows a RootObject to be generated per request
2 parents b7e0284 + 8588110 commit 59006c0

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

handler.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ const (
1919
)
2020

2121
type Handler struct {
22-
Schema *graphql.Schema
23-
pretty bool
24-
graphiql bool
25-
playground bool
22+
Schema *graphql.Schema
23+
pretty bool
24+
graphiql bool
25+
playground bool
26+
rootObjectFn RootObjectFn
2627
}
2728
type RequestOptions struct {
2829
Query string `json:"query" url:"query" schema:"query"`
@@ -128,6 +129,9 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
128129
OperationName: opts.OperationName,
129130
Context: ctx,
130131
}
132+
if h.rootObjectFn != nil {
133+
params.RootObject = h.rootObjectFn(ctx, r)
134+
}
131135
result := graphql.Do(params)
132136

133137
if h.graphiql {
@@ -169,11 +173,15 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
169173
h.ContextHandler(r.Context(), w, r)
170174
}
171175

176+
// RootObjectFn allows a user to generate a RootObject per request
177+
type RootObjectFn func(ctx context.Context, r *http.Request) map[string]interface{}
178+
172179
type Config struct {
173-
Schema *graphql.Schema
174-
Pretty bool
175-
GraphiQL bool
176-
Playground bool
180+
Schema *graphql.Schema
181+
Pretty bool
182+
GraphiQL bool
183+
Playground bool
184+
RootObjectFn RootObjectFn
177185
}
178186

179187
func NewConfig() *Config {
@@ -194,9 +202,10 @@ func New(p *Config) *Handler {
194202
}
195203

196204
return &Handler{
197-
Schema: p.Schema,
198-
pretty: p.Pretty,
199-
graphiql: p.GraphiQL,
200-
playground: p.Playground,
205+
Schema: p.Schema,
206+
pretty: p.Pretty,
207+
graphiql: p.GraphiQL,
208+
playground: p.Playground,
209+
rootObjectFn: p.RootObjectFn,
201210
}
202211
}

handler_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"context"
14+
1415
"github.com/graphql-go/graphql"
1516
"github.com/graphql-go/graphql/testutil"
1617
"github.com/graphql-go/handler"
@@ -150,3 +151,48 @@ func TestHandler_Params_NilParams(t *testing.T) {
150151
_ = handler.New(nil)
151152

152153
}
154+
155+
func TestHandler_BasicQuery_WithRootObjFn(t *testing.T) {
156+
myNameQuery := graphql.NewObject(graphql.ObjectConfig{
157+
Name: "Query",
158+
Fields: graphql.Fields{
159+
"name": &graphql.Field{
160+
Name: "name",
161+
Type: graphql.String,
162+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
163+
rv := p.Info.RootValue.(map[string]interface{})
164+
return rv["rootValue"], nil
165+
},
166+
},
167+
},
168+
})
169+
myNameSchema, err := graphql.NewSchema(graphql.SchemaConfig{
170+
Query: myNameQuery,
171+
})
172+
if err != nil {
173+
t.Fatal(err)
174+
}
175+
176+
expected := &graphql.Result{
177+
Data: map[string]interface{}{
178+
"name": "foo",
179+
},
180+
}
181+
queryString := `query={name}`
182+
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
183+
184+
h := handler.New(&handler.Config{
185+
Schema: &myNameSchema,
186+
Pretty: true,
187+
RootObjectFn: func(ctx context.Context, r *http.Request) map[string]interface{} {
188+
return map[string]interface{}{"rootValue": "foo"}
189+
},
190+
})
191+
result, resp := executeTest(t, h, req)
192+
if resp.Code != http.StatusOK {
193+
t.Fatalf("unexpected server response %v", resp.Code)
194+
}
195+
if !reflect.DeepEqual(result, expected) {
196+
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
197+
}
198+
}

0 commit comments

Comments
 (0)