Skip to content

Commit

Permalink
Merge branch 'master' into feat-opencensus
Browse files Browse the repository at this point in the history
  • Loading branch information
vvakame committed Nov 2, 2018
2 parents 59ef91a + 926ad17 commit 4e7e6a1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
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
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

0 comments on commit 4e7e6a1

Please sign in to comment.