Example #1
0
        public void InvalidScope()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var context = new Mock <IMiddlewareContext>();

            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.Argument <object>(It.IsAny <string>()))
            .Returns("Baz");

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("foo"),
                new NameNode("b"));

            // act
            var    resolver = new FieldScopedVariableResolver();
            Action a        = () => resolver.Resolve(context.Object, scopedVariable,
                                                     new NamedTypeNode(new NameNode("abc")));

            // assert
            Assert.Equal("variable",
                         Assert.Throws <ArgumentException>(a).ParamName);
        }
Example #2
0
        public void ContextIsNull()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("fields"),
                new NameNode("b"));

            // act
            var    resolver = new FieldScopedVariableResolver();
            Action a        = () => resolver.Resolve(null, scopedVariable,
                                                     new NamedTypeNode(new NameNode("abc")));

            // assert
            Assert.Equal("context",
                         Assert.Throws <ArgumentNullException>(a).ParamName);
        }
        public void InvalidScope()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { foo(a: String = \"bar\") : String }")
                             .Use(_ => _)
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            var context = new Mock <IMiddlewareContext>();

            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.ArgumentValue <object>(It.IsAny <NameString>()))
            .Returns("Baz");

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("foo"),
                new NameNode("b"));

            // act
            var    resolver = new FieldScopedVariableResolver();
            Action a        = () => resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Equal("variable", Assert.Throws <ArgumentException>(a).ParamName);
        }
        public void FieldDoesNotExist()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.ObjectType).Returns(
                schema.GetType <ObjectType>("Query"));
            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.Parent <IReadOnlyDictionary <string, object> >())
            .Returns(new Dictionary <string, object> {
                { "a", "baz" }
            });
            context.Setup(t => t.FieldSelection)
            .Returns(new FieldNode(
                         null,
                         new NameNode("foo"),
                         null,
                         Array.Empty <DirectiveNode>(),
                         Array.Empty <ArgumentNode>(),
                         null));
            context.Setup(t => t.Path).Returns(Path.New("foo"));

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("fields"),
                new NameNode("b"));

            // act
            var    resolver = new FieldScopedVariableResolver();
            Action a        = () => resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Collection(
                Assert.Throws <QueryException>(a).Errors,
                t => Assert.Equal(ErrorCodes.FieldNotDefined, t.Code));
        }
        public void CreateVariableValue()
        {
            // arrange
            var inputFormatter = new InputFormatter();

            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { foo(a: String = \"bar\") : String a: String }")
                             .Use(_ => _)
                             .ModifyOptions(o => o.StrictValidation = false)
                             .Create();

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.ObjectType).Returns(
                schema.GetType <ObjectType>("Query"));
            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.Parent <object>())
            .Returns(new Dictionary <string, object> {
                { "a", "baz" }
            });
            context.Setup(t => t.Service <InputFormatter>()).Returns(inputFormatter);

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("fields"),
                new NameNode("a"));

            // act
            var resolver = new FieldScopedVariableResolver();
            ScopedVariableValue value = resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Null(value.DefaultValue);
            Assert.Equal("__fields_a", value.Name);
            Assert.IsType <NamedTypeNode>(value.Type);
            Assert.Equal("baz", value.Value.Value);
        }
        public void CreateVariableValue()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo(a: String = \"bar\") : String a: String }",
                c =>
            {
                c.UseNullResolver();
                c.Options.StrictValidation = false;
            });

            var context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.SetupGet(t => t.ObjectType).Returns(
                schema.GetType <ObjectType>("Query"));
            context.SetupGet(t => t.Field).Returns(
                schema.GetType <ObjectType>("Query").Fields["foo"]);
            context.Setup(t => t.Parent <IReadOnlyDictionary <string, object> >())
            .Returns(new Dictionary <string, object> {
                { "a", "baz" }
            });

            var scopedVariable = new ScopedVariableNode(
                null,
                new NameNode("fields"),
                new NameNode("a"));

            // act
            var           resolver = new FieldScopedVariableResolver();
            VariableValue value    = resolver.Resolve(
                context.Object,
                scopedVariable,
                schema.GetType <StringType>("String"));

            // assert
            Assert.Null(value.DefaultValue);
            Assert.Equal("fields_a", value.Name);
            Assert.IsType <NamedTypeNode>(value.Type);
            Assert.Equal("baz", value.Value.Value);
        }