Skip to content

Add ContextHandler API function to externally provided net/context.Context #6

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 1 commit into from
Jan 28, 2016
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
13 changes: 10 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gorilla/schema"
"github.com/graphql-go/graphql"
"github.com/unrolled/render"
"golang.org/x/net/context"
)

const (
Expand Down Expand Up @@ -104,9 +105,9 @@ func getRequestOptions(r *http.Request) *requestOptions {
}
}

// ServeHTTP provides an entry point into executing graphQL queries
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {

// ContextHandler provides an entrypoint into executing graphQL queries with a
// user-provided context.
func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
// get query
opts := getRequestOptions(r)

Expand All @@ -116,13 +117,19 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
RequestString: opts.Query,
VariableValues: opts.Variables,
OperationName: opts.OperationName,
Context: ctx,
}
result := graphql.Do(params)

// render result
h.render.JSON(w, http.StatusOK, result)
}

// ServeHTTP provides an entrypoint into executing graphQL queries.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.ContextHandler(context.Background(), w, r)
}

type Config struct {
Schema *graphql.Schema
Pretty bool
Expand Down
44 changes: 44 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/graphql-go/graphql/testutil"
"github.com/graphql-go/handler"
"github.com/graphql-go/relay/examples/starwars" // TODO: remove this dependency
"golang.org/x/net/context"
)

func decodeResponse(t *testing.T, recorder *httptest.ResponseRecorder) *graphql.Result {
Expand All @@ -39,6 +40,49 @@ func executeTest(t *testing.T, h *handler.Handler, req *http.Request) (*graphql.
return result, resp
}

func TestContextPropagated(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) {
return p.Context.Value("name"), nil
},
},
},
})
myNameSchema, err := graphql.NewSchema(graphql.SchemaConfig{myNameQuery, nil})
if err != nil {
t.Fatal(err)
}

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

h := handler.New(&handler.Config{
Schema: &myNameSchema,
Pretty: true,
})

ctx := context.WithValue(context.Background(), "name", "context-data")
resp := httptest.NewRecorder()
h.ContextHandler(ctx, resp, req)
result := decodeResponse(t, resp)
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))
}
}

func TestHandler_BasicQuery(t *testing.T) {

expected := &graphql.Result{
Expand Down