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

copy complexity to RequestContext #403

Merged
merged 3 commits into from
Nov 2, 2018
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
4 changes: 4 additions & 0 deletions graphql/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type RequestContext struct {
RawQuery string
Variables map[string]interface{}
Doc *ast.QueryDocument

ComplexityLimit int
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is ComplexityLimit here, its static?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's static value determined at compile time.

BTW, I want to modify it on run time...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I want to modify it on run time...

🤔

when? ideally a request over the limit doesn't run anything, especially not in the resolver stack.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, If we want to change ComplexityLimit between 1st party client and 3rd party client.

OperationComplexity int

// ErrorPresenter will be used to generate the error
// message from errors given to Error().
ErrorPresenter ErrorPresenterFunc
Expand Down
19 changes: 11 additions & 8 deletions handler/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Config struct {
complexityLimit int
}

func (c *Config) newRequestContext(doc *ast.QueryDocument, query string, variables map[string]interface{}) *graphql.RequestContext {
func (c *Config) newRequestContext(es graphql.ExecutableSchema, doc *ast.QueryDocument, op *ast.OperationDefinition, query string, variables map[string]interface{}) *graphql.RequestContext {
reqCtx := graphql.NewRequestContext(doc, query, variables)
if hook := c.recover; hook != nil {
reqCtx.Recover = hook
Expand All @@ -59,6 +59,12 @@ func (c *Config) newRequestContext(doc *ast.QueryDocument, query string, variabl
reqCtx.Tracer = &graphql.NopTracer{}
}

if c.complexityLimit > 0 {
reqCtx.ComplexityLimit = c.complexityLimit
operationComplexity := complexity.Calculate(es, op, variables)
reqCtx.OperationComplexity = operationComplexity
}

return reqCtx
}

Expand Down Expand Up @@ -342,7 +348,7 @@ func (gh *graphqlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
gh.cache.Add(reqParams.Query, doc)
}

reqCtx := gh.cfg.newRequestContext(doc, reqParams.Query, vars)
reqCtx := gh.cfg.newRequestContext(gh.exec, doc, op, reqParams.Query, vars)
ctx = graphql.WithRequestContext(ctx, reqCtx)

defer func() {
Expand All @@ -352,12 +358,9 @@ func (gh *graphqlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}()

if gh.cfg.complexityLimit > 0 {
queryComplexity := complexity.Calculate(gh.exec, op, vars)
if queryComplexity > gh.cfg.complexityLimit {
sendErrorf(w, http.StatusUnprocessableEntity, "query has complexity %d, which exceeds the limit of %d", queryComplexity, gh.cfg.complexityLimit)
return
}
if reqCtx.ComplexityLimit > 0 && reqCtx.OperationComplexity > reqCtx.ComplexityLimit {
sendErrorf(w, http.StatusUnprocessableEntity, "operation has complexity %d, which exceeds the limit of %d", reqCtx.OperationComplexity, reqCtx.ComplexityLimit)
return
}

switch op.Operation {
Expand Down
2 changes: 1 addition & 1 deletion handler/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (c *wsConnection) subscribe(message *operationMessage) bool {
c.sendError(message.ID, err)
return true
}
reqCtx := c.cfg.newRequestContext(doc, reqParams.Query, vars)
reqCtx := c.cfg.newRequestContext(c.exec, doc, op, reqParams.Query, vars)
ctx := graphql.WithRequestContext(c.ctx, reqCtx)

if c.initPayload != nil {
Expand Down