Example #1
0
        public async Task TwoOperations_ShortHand_QueryException()
        {
            // arrange
            Schema schema = Schema.Create(@"
                type Query { a: String }
                ", c =>
            {
                c.BindResolver(() => "hello world")
                .To("Query", "a");
            });

            var request = new QueryRequest("{ a } query a { a }").ToReadOnly();

            var context = new QueryContext(
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request);

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            Func <Task> func = () => middleware.InvokeAsync(context);

            // assert
            QueryException exception =
                await Assert.ThrowsAsync <QueryException>(func);

            Assert.Equal(
                "Only queries that contain one operation can be executed " +
                "without specifying the opartion name.",
                exception.Message);
        }
Example #2
0
        public async Task RooValueIsRegisterdAsService()
        {
            // arrange
            var services = new DictionaryServiceProvider(
                typeof(DisposableQuery), new DisposableQuery());

            Schema schema = Schema.Create(c =>
            {
                c.RegisterQueryType <DisposableQuery>();
            });

            var request = new QueryRequest("{ isDisposable }").ToReadOnly();

            var context = new QueryContext(
                schema,
                services.CreateRequestServiceScope(),
                request);

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            DisposableQuery query = Assert.IsType <DisposableQuery>(
                context.Operation.RootValue);

            Assert.False(query.IsDisposed);
        }
Example #3
0
        public async Task OperationIsResolved()
        {
            // arrange
            Schema schema = Schema.Create(@"
                type Query { a: String }
                ", c =>
            {
                c.BindResolver(() => "hello world")
                .To("Query", "a");
            });

            var request = new QueryRequest("query a { a }");

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request,
                fs => fs.Field.Middleware
                          );

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.NotNull(context.Operation);
            Assert.Equal("a", context.Operation.Name);
        }
Example #4
0
        public async Task ResolveRootTypeWithCustomNames(string rootType)
        {
            // arrange
            Schema schema = Schema.Create(@"
                type Foo { a: String }
                schema { " + rootType + @": Foo }
                ", c =>
            {
                c.BindResolver(() => "hello world")
                .To("Foo", "a");
            });

            var request = new QueryRequest("query a { a }").ToReadOnly();

            var context = new QueryContext(
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request);

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.NotNull(context.Operation.RootType);
        }
Example #5
0
        public async Task RootValueNotProvidedAndRegisteredWithServices()
        {
            // arrange
            Schema schema = Schema.Create(c =>
            {
                c.RegisterQueryType <DisposableQuery>();
            });

            var request = new QueryRequest("{ isDisposable }").ToReadOnly();

            var context = new QueryContext(
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request);

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            DisposableQuery query = Assert.IsType <DisposableQuery>(
                context.Operation.RootValue);

            Assert.True(query.IsDisposed);
        }
Example #6
0
        public async Task RootClrTypeIsObject()
        {
            // arrange
            Schema schema = Schema.Create(@"
                type Query { a: String }
                ", c =>
            {
                c.BindResolver(() => "hello world")
                .To("Query", "a");
            });

            var request = new QueryRequest("query a { a }").ToReadOnly();

            var context = new QueryContext(
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request);

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.Null(context.Operation.RootValue);
        }
Example #7
0
        public async Task RootValueProvidedByRequest()
        {
            // arrange
            Schema schema = Schema.Create(c =>
            {
                c.RegisterQueryType <DisposableQuery>();
            });

            var rootValue = new DisposableQuery();

            var request = new QueryRequest("{ isDisposable }")
            {
                InitialValue = rootValue
            }.ToReadOnly();

            var context = new QueryContext(
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request);

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.True(object.ReferenceEquals(
                            rootValue, context.Operation.RootValue));
        }
Example #8
0
        public async Task ParseQueryMiddleware_ValidQuery_DocumentIsSet()
        {
            // arrange
            var schema = Schema.Create(@"
                type Query { a(b:String): String }
                ", c =>
            {
                c.BindResolver(() => "hello world")
                .To("Query", "a");
            });

            var sourceText = "query foo($a: String) { a }";

            var request = QueryRequestBuilder.New()
                          .SetQuery(sourceText)
                          .SetVariableValue("a", "abc")
                          .Create();

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request,
                (f, s) => f.Middleware
                          );

            context.QueryKey = "Foo";

            context.Document = Utf8GraphQLParser.Parse(sourceText);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.NotNull(context.Operation.Variables);
            Assert.Equal(
                "abc",
                context.Operation.Variables.GetVariable <string>("a"));
        }
Example #9
0
        public async Task TwoOperations_WrongOperationName_QueryException()
        {
            // arrange
            var schema = Schema.Create(@"
                type Query { a: String }
                ", c =>
            {
                c.BindResolver(() => "hello world")
                .To("Query", "a");
            });

            var sourceText = "query a { a } query b { a }";

            var request = QueryRequestBuilder.New()
                          .SetQuery(sourceText)
                          .SetOperation("c")
                          .Create();

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request,
                (f, s) => f.Middleware
                          );

            context.Document = Utf8GraphQLParser.Parse(sourceText);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            Func <Task> func = () => middleware.InvokeAsync(context);

            // assert
            QueryException exception =
                await Assert.ThrowsAsync <QueryException>(func);

            Assert.Equal(
                "The specified operation `c` does not exist.",
                exception.Message);
        }
Example #10
0
        public async Task ProvidedRootValueTakesPrecedenceOverService()
        {
            // arrange
            var services = new DictionaryServiceProvider(
                typeof(DisposableQuery), new DisposableQuery());

            var schema = Schema.Create(c =>
            {
                c.RegisterQueryType <DisposableQuery>();
            });

            var rootValue = new DisposableQuery();

            var sourceText = "{ isDisposable }";

            var request = QueryRequestBuilder.New()
                          .SetQuery(sourceText)
                          .SetInitialValue(rootValue)
                          .Create();

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateRequestServiceScope(services),
                request,
                (f, s) => f.Middleware
                          );

            context.Document = Utf8GraphQLParser.Parse(sourceText);
            context.QueryKey = "foo";

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.True(object.ReferenceEquals(
                            rootValue, context.Operation.RootValue));
        }
Example #11
0
        public async Task ParseQueryMiddleware_ValidQuery_DocumentIsSet()
        {
            // arrange
            Schema schema = Schema.Create(@"
                type Query { a(b:String): String }
                ", c =>
            {
                c.BindResolver(() => "hello world")
                .To("Query", "a");
            });

            var request = new QueryRequest("query foo($a: String) { a }")
            {
                VariableValues = new Dictionary <string, object>
                {
                    { "a", "abc" }
                }
            };

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request,
                fs => fs.Field.Middleware
                          );

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.NotNull(context.Operation.Variables);
            Assert.Equal(
                "abc",
                context.Operation.Variables.GetVariable <string>("a"));
        }
Example #12
0
        public async Task ResolveRootTypeWithCustomNames(string rootType)
        {
            // arrange
            string queryType = rootType == "query"
                ? string.Empty
                : "type Query { a : String }";

            var schema = Schema.Create(@"
                type Foo { a: String }
                schema { " + rootType + @": Foo }
                " + queryType, c =>
            {
                c.Use(n => ctx => Task.CompletedTask);
            });

            var sourceText = "query a { a }";

            var request = QueryRequestBuilder.New()
                          .SetQuery(sourceText)
                          .Create();

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request,
                (f, s) => f.Middleware
                          );

            context.QueryKey = "foo";

            context.Document = Utf8GraphQLParser.Parse(sourceText);
            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.NotNull(context.Operation.RootType);
        }
Example #13
0
        public async Task RootValueIsRegisterdAsService()
        {
            // arrange
            var services = new DictionaryServiceProvider(
                typeof(DisposableQuery), new DisposableQuery());

            var schema = Schema.Create(c =>
            {
                c.RegisterQueryType <DisposableQuery>();
            });

            var sourceText = "{ isDisposable }";

            var request = QueryRequestBuilder.New()
                          .SetQuery(sourceText)
                          .Create();

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateRequestServiceScope(services),
                request,
                (f, s) => f.Middleware
                          );

            context.Document = Utf8GraphQLParser.Parse(sourceText);
            context.QueryKey = "foo";

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            DisposableQuery query = Assert.IsType <DisposableQuery>(
                context.Operation.RootValue);

            Assert.False(query.IsDisposed);
        }
Example #14
0
        public async Task ProvidedRootValueTakesPrecedenceOverService()
        {
            // arrange
            var services = new DictionaryServiceProvider(
                typeof(DisposableQuery), new DisposableQuery());

            Schema schema = Schema.Create(c =>
            {
                c.RegisterQueryType <DisposableQuery>();
            });

            var rootValue = new DisposableQuery();

            var request = new QueryRequest("{ isDisposable }")
            {
                InitialValue = rootValue
            };

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateRequestServiceScope(services),
                request,
                fs => fs.Field.Middleware
                          );

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.True(object.ReferenceEquals(
                            rootValue, context.Operation.RootValue));
        }
Example #15
0
        public async Task OperationIsResolved()
        {
            // arrange
            var schema = Schema.Create(@"
                type Query { a: String }
                ", c =>
            {
                c.BindResolver(() => "hello world")
                .To("Query", "a");
            });

            var sourceText = "query a { a }";

            var request = QueryRequestBuilder.New()
                          .SetQuery(sourceText)
                          .Create();

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request,
                (f, s) => f.Middleware
                          );

            context.Document = Utf8GraphQLParser.Parse(sourceText);
            context.QueryKey = "foo";

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.NotNull(context.Operation);
            Assert.Equal("a", context.Operation.Name);
        }
        public async Task ResolveRootTypeWithCustomNames(string rootType)
        {
            // arrange
            string queryType = rootType == "query"
                ? string.Empty
                : "type Query { a : String }";

            var schema = Schema.Create(@"
                type Foo { a: String }
                schema { " + rootType + @": Foo }
                " + queryType, c =>
            {
                c.Use(n => ctx => Task.CompletedTask);
            });

            var request = new QueryRequest("query a { a }");

            var context = new QueryContext
                          (
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request,
                fs => fs.Field.Middleware
                          );

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            await middleware.InvokeAsync(context);

            // assert
            Assert.NotNull(context.Operation.RootType);
        }
Example #17
0
        public async Task TwoOperations_WrongOperationName_QueryException()
        {
            // arrange
            Schema schema = Schema.Create(@"
                type Query { a: String }
                ", c =>
            {
                c.BindResolver(() => "hello world")
                .To("Query", "a");
            });

            var request = new QueryRequest(
                "query a { a } query b { a }", "c")
                          .ToReadOnly();

            var context = new QueryContext(
                schema,
                MiddlewareTools.CreateEmptyRequestServiceScope(),
                request);

            context.Document = Parser.Default.Parse(request.Query);

            var middleware = new ResolveOperationMiddleware(
                c => Task.CompletedTask, null);

            // act
            Func <Task> func = () => middleware.InvokeAsync(context);

            // assert
            QueryException exception =
                await Assert.ThrowsAsync <QueryException>(func);

            Assert.Equal(
                "The specified operation `c` does not exist.",
                exception.Message);
        }