Skip to content
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

Add RequestCallbackFn #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
46 changes: 27 additions & 19 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ const (
)

type ResultCallbackFn func(ctx context.Context, params *graphql.Params, result *graphql.Result, responseBody []byte)
type RequestCallbackFn func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context

type Handler struct {
Schema *graphql.Schema
pretty bool
graphiql bool
playground bool
rootObjectFn RootObjectFn
resultCallbackFn ResultCallbackFn
Schema *graphql.Schema
pretty bool
graphiql bool
playground bool
requestCallbackFn RequestCallbackFn
rootObjectFn RootObjectFn
resultCallbackFn ResultCallbackFn
}
type RequestOptions struct {
Query string `json:"query" url:"query" schema:"query"`
Expand Down Expand Up @@ -178,19 +180,24 @@ func (h *Handler) ContextHandler(ctx context.Context, w http.ResponseWriter, r *

// ServeHTTP provides an entrypoint into executing graphQL queries.
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h.ContextHandler(r.Context(), w, r)
ctx := r.Context()
if h.requestCallbackFn != nil {
ctx = h.requestCallbackFn(ctx, w, r)
}
h.ContextHandler(ctx, 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
RootObjectFn RootObjectFn
ResultCallbackFn ResultCallbackFn
Schema *graphql.Schema
Pretty bool
GraphiQL bool
Playground bool
RequestCallbackFn RequestCallbackFn
RootObjectFn RootObjectFn
ResultCallbackFn ResultCallbackFn
}

func NewConfig() *Config {
Expand All @@ -211,11 +218,12 @@ func New(p *Config) *Handler {
}

return &Handler{
Schema: p.Schema,
pretty: p.Pretty,
graphiql: p.GraphiQL,
playground: p.Playground,
rootObjectFn: p.RootObjectFn,
resultCallbackFn: p.ResultCallbackFn,
Schema: p.Schema,
pretty: p.Pretty,
graphiql: p.GraphiQL,
playground: p.Playground,
requestCallbackFn: p.RequestCallbackFn,
rootObjectFn: p.RootObjectFn,
resultCallbackFn: p.ResultCallbackFn,
}
}
22 changes: 18 additions & 4 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,24 @@ func TestHandler_BasicQuery_Pretty(t *testing.T) {
queryString := `query=query HeroNameQuery { hero { name } }&operationName=HeroNameQuery`
req, _ := http.NewRequest("GET", fmt.Sprintf("/graphql?%v", queryString), nil)

callbackCalled := false
requestCallbackCalled := false
resultCallbackCalled := false
h := handler.New(&handler.Config{
Schema: &testutil.StarWarsSchema,
Pretty: true,
RequestCallbackFn: func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
requestCallbackCalled = true
w.Header().Add("X-Custom-Header", "header-value")
return context.WithValue(ctx, "custom", "value")
},
ResultCallbackFn: func(ctx context.Context, params *graphql.Params, result *graphql.Result, responseBody []byte) {
callbackCalled = true
resultCallbackCalled = true
if params.OperationName != "HeroNameQuery" {
t.Fatalf("OperationName passed to callback was not HeroNameQuery: %v", params.OperationName)
}

if ctx.Value("custom").(string) != "value" {
t.Fatalf("context was not feeled in RequestCallbackFn")
}
if result.HasErrors() {
t.Fatalf("unexpected graphql result errors")
}
Expand All @@ -115,10 +123,16 @@ func TestHandler_BasicQuery_Pretty(t *testing.T) {
if resp.Code != http.StatusOK {
t.Fatalf("unexpected server response %v", resp.Code)
}
if resp.Header()["X-Custom-Header"][0] != "header-value" {
t.Fatalf("HTTP headers was not feeled in RequestCallbackFn")
}
if !reflect.DeepEqual(result, expected) {
t.Fatalf("wrong result, graphql result diff: %v", testutil.Diff(expected, result))
}
if !callbackCalled {
if !requestCallbackCalled {
t.Fatalf("RequestCallbackFn was not called when it should have been")
}
if !resultCallbackCalled {
t.Fatalf("ResultCallbackFn was not called when it should have been")
}
}
Expand Down