Beispiel #1
0
        public Task InvokeAsync(IQueryContext context)
        {
            if (_options.UseComplexityMultipliers == true &&
                _options.MaxOperationComplexity.HasValue)
            {
                if (IsContextIncomplete(context))
                {
                    context.Result = QueryResult.CreateError(
                        ErrorBuilder.New()
                        .SetMessage(CoreResources
                                    .MaxComplexityMiddleware_Prerequisite)
                        .Build());
                    return(Task.CompletedTask);
                }
                else
                {
                    var visitorContext = MaxComplexityVisitorContext.New(
                        context.Schema,
                        context.Operation.Variables,
                        _calculation);

                    int complexity = _visitor.Visit(
                        context.Document,
                        context.Operation.Definition,
                        visitorContext);

                    if (IsAllowedComplexity(complexity))
                    {
                        return(_next(context));
                    }
                    else
                    {
                        IError error = ErrorBuilder.New()
                                       .SetMessage(string.Format(
                                                       CultureInfo.InvariantCulture,
                                                       CoreResources
                                                       .MaxComplexityMiddleware_NotAllowed,
                                                       complexity,
                                                       _options.MaxOperationComplexity))
                                       .AddLocation(context.Operation.Definition)
                                       .Build();

                        context.Result = QueryResult.CreateError(error);
                        return(Task.CompletedTask);
                    }
                }
            }

            return(_next(context));
        }
        public Task InvokeAsync(IQueryContext context)
        {
            if (_options.UseComplexityMultipliers == true &&
                _options.MaxOperationComplexity.HasValue)
            {
                if (IsContextIncomplete(context))
                {
                    context.Result = QueryResult.CreateError(new QueryError(
                                                                 "The max complexity middleware expects the " +
                                                                 "query document to be parsed and the operation " +
                                                                 "to be resolved."));
                    return(Task.CompletedTask);
                }
                else
                {
                    var visitorContext = MaxComplexityVisitorContext.New(
                        context.Schema,
                        context.Operation.Variables,
                        _calculation);

                    int complexity = _visitor.Visit(
                        context.Document,
                        context.Operation.Definition,
                        visitorContext);

                    if (IsAllowedComplexity(complexity))
                    {
                        return(_next(context));
                    }
                    else
                    {
                        Location[] locations =
                            context.Operation.Definition.Location == null
                            ? null
                            : QueryError.ConvertLocation(
                                context.Operation.Definition.Location);

                        context.Result = QueryResult.CreateError(new QueryError(
                                                                     "The operation that shall be executed has a " +
                                                                     $"complexity of {complexity}. \n" +
                                                                     "The maximum allowed query complexity is " +
                                                                     $"{_options.MaxOperationComplexity}.",
                                                                     locations));
                        return(Task.CompletedTask);
                    }
                }
            }

            return(_next(context));
        }