diff options
author | Amine Hilaly <hilalyamine@gmail.com> | 2019-04-09 21:45:24 +0200 |
---|---|---|
committer | Amine Hilaly <hilalyamine@gmail.com> | 2019-04-09 21:45:24 +0200 |
commit | 26b5343e2160de172969e6834074cf8482ceb845 (patch) | |
tree | 04f27aa660a903d65f7b3d951bd1c6f92c59c0c3 /vendor/github.com/99designs/gqlgen/handler/graphql.go | |
parent | 6e8496f4c1767ca8a8b95716a04f1b492bef7397 (diff) | |
download | git-bug-26b5343e2160de172969e6834074cf8482ceb845.tar.gz |
Update Gopkg.*
Diffstat (limited to 'vendor/github.com/99designs/gqlgen/handler/graphql.go')
-rw-r--r-- | vendor/github.com/99designs/gqlgen/handler/graphql.go | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/vendor/github.com/99designs/gqlgen/handler/graphql.go b/vendor/github.com/99designs/gqlgen/handler/graphql.go index 7c5f70cf..92a0471c 100644 --- a/vendor/github.com/99designs/gqlgen/handler/graphql.go +++ b/vendor/github.com/99designs/gqlgen/handler/graphql.go @@ -34,6 +34,7 @@ type Config struct { requestHook graphql.RequestMiddleware tracer graphql.Tracer complexityLimit int + complexityLimitFunc graphql.ComplexityLimitFunc disableIntrospection bool connectionKeepAlivePingInterval time.Duration } @@ -60,11 +61,9 @@ func (c *Config) newRequestContext(es graphql.ExecutableSchema, doc *ast.QueryDo if hook := c.tracer; hook != nil { reqCtx.Tracer = hook - } else { - reqCtx.Tracer = &graphql.NopTracer{} } - if c.complexityLimit > 0 { + if c.complexityLimit > 0 || c.complexityLimitFunc != nil { reqCtx.ComplexityLimit = c.complexityLimit operationComplexity := complexity.Calculate(es, op, variables) reqCtx.OperationComplexity = operationComplexity @@ -112,6 +111,15 @@ func ComplexityLimit(limit int) Option { } } +// ComplexityLimitFunc allows you to define a function to dynamically set the maximum query complexity that is allowed +// to be executed. +// If a query is submitted that exceeds the limit, a 422 status code will be returned. +func ComplexityLimitFunc(complexityLimitFunc graphql.ComplexityLimitFunc) Option { + return func(cfg *Config) { + cfg.complexityLimitFunc = complexityLimitFunc + } +} + // ResolverMiddleware allows you to define a function that will be called around every resolver, // useful for logging. func ResolverMiddleware(middleware graphql.FieldMiddleware) Option { @@ -243,19 +251,23 @@ func CacheSize(size int) Option { } } -const DefaultCacheSize = 1000 - -// WebsocketKeepAliveDuration allows you to reconfigure the keepAlive behavior. -// By default, keep-alive is disabled. +// WebsocketKeepAliveDuration allows you to reconfigure the keepalive behavior. +// By default, keepalive is enabled with a DefaultConnectionKeepAlivePingInterval +// duration. Set handler.connectionKeepAlivePingInterval = 0 to disable keepalive +// altogether. func WebsocketKeepAliveDuration(duration time.Duration) Option { return func(cfg *Config) { cfg.connectionKeepAlivePingInterval = duration } } +const DefaultCacheSize = 1000 +const DefaultConnectionKeepAlivePingInterval = 25 * time.Second + func GraphQL(exec graphql.ExecutableSchema, options ...Option) http.HandlerFunc { cfg := &Config{ - cacheSize: DefaultCacheSize, + cacheSize: DefaultCacheSize, + connectionKeepAlivePingInterval: DefaultConnectionKeepAlivePingInterval, upgrader: websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, @@ -269,7 +281,7 @@ func GraphQL(exec graphql.ExecutableSchema, options ...Option) http.HandlerFunc var cache *lru.Cache if cfg.cacheSize > 0 { var err error - cache, err = lru.New(DefaultCacheSize) + cache, err = lru.New(cfg.cacheSize) if err != nil { // An error is only returned for non-positive cache size // and we already checked for that. @@ -305,10 +317,11 @@ func (gh *graphqlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if strings.Contains(r.Header.Get("Upgrade"), "websocket") { - connectWs(gh.exec, w, r, gh.cfg) + connectWs(gh.exec, w, r, gh.cfg, gh.cache) return } + w.Header().Set("Content-Type", "application/json") var reqParams params switch r.Method { case http.MethodGet: @@ -330,7 +343,6 @@ func (gh *graphqlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusMethodNotAllowed) return } - w.Header().Set("Content-Type", "application/json") ctx := r.Context() @@ -379,6 +391,10 @@ func (gh *graphqlHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } }() + if gh.cfg.complexityLimitFunc != nil { + reqCtx.ComplexityLimit = gh.cfg.complexityLimitFunc(ctx) + } + 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 |