Skip to content

Commit da2ce2c

Browse files
Updated to use Go 1.7 request based context
1 parent 52e6de4 commit da2ce2c

File tree

3 files changed

+74
-29
lines changed

3 files changed

+74
-29
lines changed

handler.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"strings"
99

1010
"github.com/graphql-go/graphql"
11-
12-
"golang.org/x/net/context"
1311
)
1412

1513
const (
@@ -20,7 +18,7 @@ const (
2018

2119
type Handler struct {
2220
Schema *graphql.Schema
23-
21+
2422
pretty bool
2523
}
2624
type RequestOptions struct {
@@ -115,7 +113,7 @@ func NewRequestOptions(r *http.Request) *RequestOptions {
115113

116114
// ContextHandler provides an entrypoint into executing graphQL queries with a
117115
// user-provided context.
118-
func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
116+
func (h *Handler) ContextHandler(w http.ResponseWriter, r *http.Request) {
119117
// get query
120118
opts := NewRequestOptions(r)
121119

@@ -125,11 +123,10 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
125123
RequestString: opts.Query,
126124
VariableValues: opts.Variables,
127125
OperationName: opts.OperationName,
128-
Context: ctx,
126+
Context: r.Context(),
129127
}
130128
result := graphql.Do(params)
131129

132-
133130
if h.pretty {
134131
w.WriteHeader(http.StatusOK)
135132
buff, _ := json.MarshalIndent(result, "", "\t")
@@ -138,14 +135,14 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *
138135
} else {
139136
w.WriteHeader(http.StatusOK)
140137
buff, _ := json.Marshal(result)
141-
138+
142139
w.Write(buff)
143140
}
144141
}
145142

146143
// ServeHTTP provides an entrypoint into executing graphQL queries.
147144
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
148-
h.ContextHandler(context.Background(), w, r)
145+
h.ContextHandler(w, r)
149146
}
150147

151148
type Config struct {

handler_test.go

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
package handler_test
1+
package handler
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io/ioutil"
@@ -12,9 +13,6 @@ import (
1213

1314
"github.com/graphql-go/graphql"
1415
"github.com/graphql-go/graphql/testutil"
15-
"github.com/graphql-go/handler"
16-
"github.com/graphql-go/relay/examples/starwars" // TODO: remove this dependency
17-
"golang.org/x/net/context"
1816
)
1917

2018
func decodeResponse(t *testing.T, recorder *httptest.ResponseRecorder) *graphql.Result {
@@ -33,7 +31,7 @@ func decodeResponse(t *testing.T, recorder *httptest.ResponseRecorder) *graphql.
3331
}
3432
return &target
3533
}
36-
func executeTest(t *testing.T, h *handler.Handler, req *http.Request) (*graphql.Result, *httptest.ResponseRecorder) {
34+
func executeTest(t *testing.T, h *Handler, req *http.Request) (*graphql.Result, *httptest.ResponseRecorder) {
3735
resp := httptest.NewRecorder()
3836
h.ServeHTTP(resp, req)
3937
result := decodeResponse(t, resp)
@@ -53,7 +51,8 @@ func TestContextPropagated(t *testing.T) {
5351
},
5452
},
5553
})
56-
myNameSchema, err := graphql.NewSchema(graphql.SchemaConfig{myNameQuery, nil})
54+
55+
myNameSchema, err := graphql.NewSchema(graphql.SchemaConfig{Query: myNameQuery})
5756
if err != nil {
5857
t.Fatal(err)
5958
}
@@ -66,18 +65,19 @@ func TestContextPropagated(t *testing.T) {
6665
queryString := `query={name}`
6766
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
6867

69-
h := handler.New(&handler.Config{
70-
Schema: &myNameSchema,
71-
Pretty: true,
72-
})
68+
h := New(&Config{Schema: &myNameSchema, Pretty: true})
7369

74-
ctx := context.WithValue(context.Background(), "name", "context-data")
70+
*req = *req.WithContext(context.WithValue(context.Background(), "name", "context-data"))
7571
resp := httptest.NewRecorder()
76-
h.ContextHandler(ctx, resp, req)
72+
73+
h.ContextHandler(resp, req)
74+
7775
result := decodeResponse(t, resp)
76+
7877
if resp.Code != http.StatusOK {
7978
t.Fatalf("unexpected server response %v", resp.Code)
8079
}
80+
8181
if !reflect.DeepEqual(result, expected) {
8282
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
8383
}
@@ -92,17 +92,18 @@ func TestHandler_BasicQuery_Pretty(t *testing.T) {
9292
},
9393
},
9494
}
95+
9596
queryString := `query=query RebelsShipsQuery { rebels { id, name } }`
9697
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
9798

98-
h := handler.New(&handler.Config{
99-
Schema: &starwars.Schema,
100-
Pretty: true,
101-
})
99+
h := New(&Config{Schema: &schema, Pretty: true})
100+
102101
result, resp := executeTest(t, h, req)
102+
103103
if resp.Code != http.StatusOK {
104104
t.Fatalf("unexpected server response %v", resp.Code)
105105
}
106+
106107
if !reflect.DeepEqual(result, expected) {
107108
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
108109
}
@@ -117,17 +118,18 @@ func TestHandler_BasicQuery_Ugly(t *testing.T) {
117118
},
118119
},
119120
}
121+
120122
queryString := `query=query RebelsShipsQuery { rebels { id, name } }`
121123
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)
122124

123-
h := handler.New(&handler.Config{
124-
Schema: &starwars.Schema,
125-
Pretty: false,
126-
})
125+
h := New(&Config{Schema: &schema, Pretty: false})
126+
127127
result, resp := executeTest(t, h, req)
128+
128129
if resp.Code != http.StatusOK {
129130
t.Fatalf("unexpected server response %v", resp.Code)
130131
}
132+
131133
if !reflect.DeepEqual(result, expected) {
132134
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
133135
}
@@ -148,6 +150,6 @@ func TestHandler_Params_NilParams(t *testing.T) {
148150
}
149151
t.Fatalf("expected to panic, did not panic")
150152
}()
151-
_ = handler.New(nil)
152153

154+
_ = New(nil)
153155
}

schema_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package handler
2+
3+
import (
4+
"github.com/graphql-go/graphql"
5+
)
6+
7+
type faction struct {
8+
ID string `json:"id"`
9+
Name string `json:"name"`
10+
}
11+
12+
var rebels = &faction{
13+
"RmFjdGlvbjox",
14+
"Alliance to Restore the Republic",
15+
}
16+
17+
var factionType = graphql.NewObject(graphql.ObjectConfig{
18+
Name: "Faction",
19+
Description: "A faction in the Star Wars saga",
20+
Fields: graphql.Fields{
21+
"id": &graphql.Field{
22+
Type: graphql.String,
23+
Description: "Id of the faction",
24+
},
25+
"name": &graphql.Field{
26+
Type: graphql.String,
27+
Description: "The name of the faction.",
28+
},
29+
},
30+
})
31+
32+
var queryType = graphql.NewObject(graphql.ObjectConfig{
33+
Name: "Query",
34+
Fields: graphql.Fields{
35+
"rebels": &graphql.Field{
36+
Type: factionType,
37+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
38+
return rebels, nil
39+
},
40+
},
41+
},
42+
})
43+
44+
var schema, _ = graphql.NewSchema(graphql.SchemaConfig{
45+
Query: queryType,
46+
})

0 commit comments

Comments
 (0)