@@ -14,6 +14,7 @@ import (
14
14
"github.com/graphql-go/graphql/testutil"
15
15
"github.com/graphql-go/handler"
16
16
"github.com/graphql-go/relay/examples/starwars" // TODO: remove this dependency
17
+ "golang.org/x/net/context"
17
18
)
18
19
19
20
func decodeResponse (t * testing.T , recorder * httptest.ResponseRecorder ) * graphql.Result {
@@ -39,6 +40,49 @@ func executeTest(t *testing.T, h *handler.Handler, req *http.Request) (*graphql.
39
40
return result , resp
40
41
}
41
42
43
+ func TestContextPropagated (t * testing.T ) {
44
+ myNameQuery := graphql .NewObject (graphql.ObjectConfig {
45
+ Name : "Query" ,
46
+ Fields : graphql.Fields {
47
+ "name" : & graphql.Field {
48
+ Name : "name" ,
49
+ Type : graphql .String ,
50
+ Resolve : func (p graphql.ResolveParams ) (interface {}, error ) {
51
+ return p .Context .Value ("name" ), nil
52
+ },
53
+ },
54
+ },
55
+ })
56
+ myNameSchema , err := graphql .NewSchema (graphql.SchemaConfig {myNameQuery , nil })
57
+ if err != nil {
58
+ t .Fatal (err )
59
+ }
60
+
61
+ expected := & graphql.Result {
62
+ Data : map [string ]interface {}{
63
+ "name" : "context-data" ,
64
+ },
65
+ }
66
+ queryString := `query={name}`
67
+ req , _ := http .NewRequest ("GET" , fmt .Sprintf ("/graphql?%v" , queryString ), nil )
68
+
69
+ h := handler .New (& handler.Config {
70
+ Schema : & myNameSchema ,
71
+ Pretty : true ,
72
+ })
73
+
74
+ ctx := context .WithValue (context .Background (), "name" , "context-data" )
75
+ resp := httptest .NewRecorder ()
76
+ h .ContextHandler (ctx , resp , req )
77
+ result := decodeResponse (t , resp )
78
+ if resp .Code != http .StatusOK {
79
+ t .Fatalf ("unexpected server response %v" , resp .Code )
80
+ }
81
+ if ! reflect .DeepEqual (result , expected ) {
82
+ t .Fatalf ("wrong result, graphql result diff: %v" , testutil .Diff (expected , result ))
83
+ }
84
+ }
85
+
42
86
func TestHandler_BasicQuery (t * testing.T ) {
43
87
44
88
expected := & graphql.Result {
0 commit comments