Ejemplo n.º 1
0
        private void ValidateArguments(ITypeCompletionContext context, Directive directive)
        {
            var arguments = directive.ToNode().Arguments.ToDictionary(t => t.Name.Value);

            foreach (ArgumentNode argument in arguments.Values)
            {
                if (directive.Type.Arguments.TryGetField(
                        argument.Name.Value,
                        out Argument? arg))
                {
                    if (!arg.Type.IsInstanceOfType(argument.Value))
                    {
                        context.ReportError(
                            DirectiveCollection_ArgumentValueTypeIsWrong(
                                directive.Type,
                                context.Type,
                                directive.ToNode(),
                                _source,
                                arg.Name));
                    }
                }
                else
                {
                    context.ReportError(
                        DirectiveCollection_ArgumentDoesNotExist(
                            directive.Type,
                            context.Type,
                            directive.ToNode(),
                            _source,
                            argument.Name.Value));
                }
            }

            foreach (Argument argument in directive.Type.Arguments
                     .Where(a => a.Type.IsNonNullType()))
            {
                if (!arguments.TryGetValue(argument.Name, out ArgumentNode? arg) ||
                    arg.Value is NullValueNode)
                {
                    context.ReportError(
                        DirectiveCollection_ArgumentNonNullViolation(
                            directive.Type,
                            context.Type,
                            directive.ToNode(),
                            _source,
                            argument.Name));
                }
            }
        }
Ejemplo n.º 2
0
        public void ConvertCustomDirectiveToDirectiveNode()
        {
            // arrange
            ISchema       schema        = CreateSchema();
            DirectiveType directiveType = schema.GetDirectiveType("Foo");
            var           fooDirective  = new FooDirective
            {
                Bar   = "123",
                Child = new FooChild
                {
                    Bar = "456"
                }
            };

            // act
            var directive = new Directive(
                directiveType, fooDirective, new object());
            DirectiveNode directiveNode = directive.ToNode();

            // assert
            Assert.Equal(directiveType.Name, directiveNode.Name.Value);
            Assert.Collection(directiveNode.Arguments,
                              t =>
            {
                Assert.Equal("bar", t.Name.Value);
                Assert.Equal("123", ((StringValueNode)t.Value).Value);
            },
                              t =>
            {
                Assert.Equal("child", t.Name.Value);
                Assert.Collection(((ObjectValueNode)t.Value).Fields,
                                  x =>
                {
                    Assert.Equal("bar", x.Name.Value);
                    Assert.Equal("456",
                                 ((StringValueNode)x.Value).Value);
                });
            });
        }
        private void ValidateArguments(ICompletionContext context, Directive directive)
        {
            Dictionary <string, ArgumentNode> arguments =
                directive.ToNode().Arguments.ToDictionary(t => t.Name.Value);

            foreach (ArgumentNode argument in arguments.Values)
            {
                if (directive.Type.Arguments.TryGetField(
                        argument.Name.Value, out Argument arg))
                {
                    if (!arg.Type.IsInstanceOfType(argument.Value))
                    {
                        // TODO : resources
                        context.ReportError(SchemaErrorBuilder.New()
                                            .SetMessage(string.Format(
                                                            CultureInfo.InvariantCulture,
                                                            "The argument `{0}` value type is wrong.",
                                                            arg.Name))
                                            .SetCode(ErrorCodes.Schema.ArgumentValueTypeWrong)
                                            .SetTypeSystemObject(context.Type)
                                            .AddSyntaxNode(directive.ToNode())
                                            .Build());
                    }
                }
                else
                {
                    // TODO : resources
                    context.ReportError(SchemaErrorBuilder.New()
                                        .SetMessage(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        "The argument `{0}` does not exist on the " +
                                                        "directive `{1}`.",
                                                        argument.Name.Value,
                                                        directive.Type.Name))
                                        .SetCode(ErrorCodes.Schema.InvalidArgument)
                                        .SetTypeSystemObject(context.Type)
                                        .AddSyntaxNode(directive.ToNode())
                                        .Build());
                }
            }

            foreach (Argument argument in directive.Type.Arguments
                     .Where(a => a.Type.IsNonNullType()))
            {
                if (!arguments.TryGetValue(argument.Name, out ArgumentNode arg) ||
                    arg.Value is NullValueNode)
                {
                    // TODO : resources
                    context.ReportError(SchemaErrorBuilder.New()
                                        .SetMessage(string.Format(
                                                        CultureInfo.InvariantCulture,
                                                        "The argument `{0}` of directive `{1}` " +
                                                        "mustn't be null.",
                                                        argument.Name.Value,
                                                        directive.Type.Name))
                                        .SetCode(ErrorCodes.Schema.NonNullArgument)
                                        .SetTypeSystemObject(context.Type)
                                        .AddSyntaxNode(directive.ToNode())
                                        .Build());
                }
            }
        }