public void AnnotateCostToInterfaceFieldCodeFirst()
        {
            // arrange
            // act
            var schema = Schema.Create(
                t =>
            {
                t.RegisterQueryType(new ObjectType(
                                        o => o.Name("Query")
                                        .Field("field")
                                        .Argument("a", a => a.Type <StringType>())
                                        .Type <StringType>()));

                t.RegisterType(new InterfaceType(
                                   o => o.Name("IQuery")
                                   .Field("field")
                                   .Argument("a", a => a.Type <StringType>())
                                   .Type <StringType>()
                                   .Cost(5)));

                t.RegisterDirective <CostDirectiveType>();

                t.Use(next => context => Task.CompletedTask);
            });

            InterfaceType queryInterface = schema.GetType <InterfaceType>("IQuery");
            IDirective    directive      = queryInterface.Fields["field"].Directives
                                           .Single(t => t.Name == "cost");
            CostDirective obj = directive.ToObject <CostDirective>();

            Assert.Equal(5, obj.Complexity);
        }
        public void AnnotateCostToInterfaceFieldSchemaFirst()
        {
            // arrange
            // act
            var schema = SchemaBuilder.New()
                         .AddDocumentFromString(
                @"
                    type Query {
                        field(a: Int): String
                            @cost(complexity: 5 multipliers: [""a""])
                    }

                    interface IQuery {
                        field(a: Int): String
                            @cost(complexity: 5 multipliers: [""a""])
                    }
                    ")
                         .AddDirectiveType <CostDirectiveType>()
                         .Use(next => context => Task.CompletedTask)
                         .Create();

            InterfaceType queryInterface = schema.GetType <InterfaceType>("IQuery");
            IDirective    directive      =
                queryInterface.Fields["field"].Directives.Single(t => t.Name == "cost");
            CostDirective obj = directive.ToObject <CostDirective>();

            Assert.Equal(5, obj.Complexity);
            Assert.Collection(obj.Multipliers, t => Assert.Equal("a", t));
        }
        public void AnnotateCostToObjectFieldCodeFirstTwoMultiplier()
        {
            // arrange
            // act
            var schema = Schema.Create(
                t =>
            {
                t.RegisterQueryType(new ObjectType(
                                        o => o.Name("Query")
                                        .Field("field")
                                        .Argument("a", a => a.Type <StringType>())
                                        .Type <StringType>()
                                        .Cost(5, "a", "b")));

                t.RegisterDirective <CostDirectiveType>();

                t.Use(next => context => Task.CompletedTask);
            });

            ObjectType query     = schema.GetType <ObjectType>("Query");
            IDirective directive = query.Fields["field"].Directives
                                   .Single(t => t.Name == "cost");
            CostDirective obj = directive.ToObject <CostDirective>();

            Assert.Equal(5, obj.Complexity);
            Assert.Collection(obj.Multipliers,
                              t => Assert.Equal("a", t),
                              t => Assert.Equal("b", t));
        }
        private static void CollectComputeDependencies(
            Context context,
            IComplexOutputType type,
            IOutputField field)
        {
            IDirective directive = field.Directives[DirectiveNames.Computed]
                                   .FirstOrDefault();

            if (directive != null)
            {
                NameString[] dependantOn = directive
                                           .ToObject <ComputedDirective>()
                                           .DependantOn;

                if (dependantOn != null)
                {
                    foreach (string fieldName in dependantOn)
                    {
                        if (type.Fields.TryGetField(
                                fieldName,
                                out IOutputField dependency))
                        {
                            context.Dependencies.Add(new FieldDependency(
                                                         type.Name, dependency.Name));
                        }
                    }
                }
            }
        }
Example #5
0
        public override MaxComplexityVisitorContext AddField(
            IOutputField fieldDefinition,
            FieldNode fieldSelection)
        {
            IDirective directive = fieldDefinition.Directives
                                   .FirstOrDefault(t => t.Type is CostDirectiveType);
            int complexity;

            CostDirective cost = directive == null
                ? DefaultCost
                : directive.ToObject <CostDirective>();

            complexity = Complexity + CalculateComplexity(
                new ComplexityContext(
                    fieldDefinition, fieldSelection,
                    FieldPath, _variables, cost));

            if (complexity > MaxComplexity)
            {
                MaxComplexity = complexity;
            }

            return(new MaxComplexityWithMultipliersVisitorContext(
                       FragmentPath,
                       FieldPath.Add(fieldDefinition),
                       this));
        }
        public void AnnotateCostToInterfaceFieldCodeFirst()
        {
            // arrange
            // act
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(d => d
                                           .Name("Query")
                                           .Field("field")
                                           .Argument("a", a => a.Type <StringType>())
                                           .Type <StringType>())
                             .AddInterfaceType(d => d
                                               .Name("IQuery")
                                               .Field("field")
                                               .Argument("a", a => a.Type <StringType>())
                                               .Type <StringType>()
                                               .Cost(5))
                             .AddDirectiveType <CostDirectiveType>()
                             .Use(_ => _)
                             .Create();

            InterfaceType queryInterface = schema.GetType <InterfaceType>("IQuery");
            IDirective    directive      = queryInterface.Fields["field"].Directives
                                           .Single(t => t.Name == "cost");
            CostDirective obj = directive.ToObject <CostDirective>();

            Assert.Equal(5, obj.Complexity);
        }
        public void AnnotateCostToObjectFieldCodeFirstTwoMultiplier()
        {
            // arrange
            // act
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(d => d
                                           .Name("Query")
                                           .Field("field")
                                           .Argument("a", a => a.Type <StringType>())
                                           .Type <StringType>()
                                           .Cost(5, "a", "b"))
                             .AddDirectiveType <CostDirectiveType>()
                             .Use(_ => _)
                             .Create();

            ObjectType query     = schema.GetType <ObjectType>("Query");
            IDirective directive = query.Fields["field"].Directives
                                   .Single(t => t.Name == "cost");
            CostDirective obj = directive.ToObject <CostDirective>();

            Assert.Equal(5, obj.Complexity);
            Assert.Collection(obj.Multipliers,
                              t => Assert.Equal("a", t),
                              t => Assert.Equal("b", t));
        }
        private static void CollectDelegationDependencies(
            Context context,
            Types.IHasName type,
            IOutputField field)
        {
            IDirective directive = field.Directives[DirectiveNames.Delegate]
                                   .FirstOrDefault();

            if (directive != null)
            {
                CollectFieldNames(
                    directive.ToObject <DelegateDirective>(),
                    type,
                    context.Dependencies);
            }
        }
Example #9
0
        public void AnnotateCostToObjectFieldSchemaFirst()
        {
            // arrange
            // act
            var schema = Schema.Create(
                @"type Query {
                    field(a: Int): String
                        @cost(complexity: 5 multipliers: [""a""])
                }",
                t => t.Use(next => context => Task.CompletedTask));

            ObjectType query     = schema.GetType <ObjectType>("Query");
            IDirective directive = query.Fields["field"].Directives
                                   .Single(t => t.Name == "cost");
            CostDirective obj = directive.ToObject <CostDirective>();

            Assert.Equal(5, obj.Complexity);
            Assert.Collection(obj.Multipliers,
                              t => Assert.Equal("a", t));
        }
        public void AnnotateCostToObjectFieldSchemaFirst()
        {
            // arrange
            // act
            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString(
                @"type Query {
                        field(a: Int): String
                            @cost(complexity: 5 multipliers: [""a""])
                    }")
                             .AddDirectiveType <CostDirectiveType>()
                             .Use(_ => _)
                             .Create();

            ObjectType query     = schema.GetType <ObjectType>("Query");
            IDirective directive = query.Fields["field"].Directives
                                   .Single(t => t.Name == "cost");
            CostDirective obj = directive.ToObject <CostDirective>();

            Assert.Equal(5, obj.Complexity);
            Assert.Collection(obj.Multipliers,
                              t => Assert.Equal("a", t));
        }