public async Task<ExecutionResult> Execute(ComplexityConfiguration complexityConfig, string query) =>
     await StarWarsTestBase.Executer.ExecuteAsync(options =>
     {
         options.Schema = new StarWarsTestBase().Schema;
         options.Query = query;
         options.ComplexityConfiguration = complexityConfig;
     });
        public void fail_when_too_complex()
        {
            var query = @"
                query BasicQuery {
                  hero {
                    id
                    name
                    appearsIn
                  }
                }";

            var complexityConfiguration = new ComplexityConfiguration { FieldImpact = 5, MaxComplexity = 10 };
            var res = Execute(complexityConfiguration, query);

            res.Result.Errors.ShouldNotBe(null);
            res.Result.Errors.Count.ShouldBe(1);
            res.Result.Errors.First().InnerException?.GetType().ShouldBe(typeof(InvalidOperationException));
        }
        public void Validate(Document document, ComplexityConfiguration complexityParameters)
        {
            if (complexityParameters == null) return;
            var complexityResult = Analyze(document, complexityParameters.FieldImpact ?? 2.0f);
            #if DEBUG
            Debug.WriteLine($"Complexity: {complexityResult.Complexity}");
            Debug.WriteLine($"Sum(Query depth across all subqueries) : {complexityResult.TotalQueryDepth}");
            foreach (var node in complexityResult.ComplexityMap) Debug.WriteLine($"{node.Key} : {node.Value}");
            #endif
            if (complexityParameters.MaxComplexity.HasValue &&
                complexityResult.Complexity > complexityParameters.MaxComplexity.Value)
                throw new InvalidOperationException(
                    $"Query is too complex to execute. The field with the highest complexity is: {complexityResult.ComplexityMap.OrderByDescending(pair => pair.Value).First().Key}");

            if (complexityParameters.MaxDepth.HasValue &&
                complexityResult.TotalQueryDepth > complexityParameters.MaxDepth)
                throw new InvalidOperationException(
                    $"Query is too nested to execute. Depth is {complexityResult.TotalQueryDepth} levels, maximum allowed on this endpoint is {complexityParameters.MaxDepth}.");
        }
        public void error_when_too_nested()
        {
            var query = @"
                query FriendsOfFriends {
                  hero {
                    friends
                    {
                      friends
                      {
                        id
                        name
                      }
                    }
                  }
                }";

            var complexityConfiguration = new ComplexityConfiguration { MaxDepth = 2 };
            var res = Execute(complexityConfiguration, query);

            res.Result.Errors.ShouldNotBe(null);
            res.Result.Errors.Count.ShouldBe(1);
            res.Result.Errors.First().InnerException?.GetType().ShouldBe(typeof(InvalidOperationException));
        }
        public void should_work_when_complexity_within_params()
        {
            var query = @"
                query HeroNameQuery {
                  hero {
                    name
                  }
                }
            ";

            var complexityConfiguration = new ComplexityConfiguration { FieldImpact = 2, MaxComplexity = 6, MaxDepth = 1 };
            var res = Execute(complexityConfiguration, query);

            res.Result.Errors.ShouldBe(null);
        }