public async Task DispatchMultipleQueriesWithGlobalException()
        {
            // arrange
            var executor = new Mock <IQueryExecutor>();

            executor.Setup(t => t.ExecuteAsync(
                               It.IsAny <IReadOnlyQueryRequest>(),
                               It.IsAny <CancellationToken>()))
            .Returns(new Func <IReadOnlyQueryRequest,
                               CancellationToken, Task <IExecutionResult> >((r, ct) =>
            {
                return(Task.FromException <IExecutionResult>(
                           new Exception("foo")));
            }));

            var request_a = new QueryRequest("query a { a }");
            var request_b = new QueryRequest("query b { a b }");

            var client = new RemoteQueryClient(
                new EmptyServiceProvider(),
                executor.Object);

            // act
            Task <IExecutionResult> task_a = client.ExecuteAsync(request_a);
            Task <IExecutionResult> task_b = client.ExecuteAsync(request_b);
            await client.DispatchAsync(CancellationToken.None);

            // assert
            Assert.Equal("foo",
                         (await Assert.ThrowsAsync <Exception>(() => task_a)).Message);
            Assert.Equal("foo",
                         (await Assert.ThrowsAsync <Exception>(() => task_b)).Message);
        }
        public async Task DispatchMultipleQueriesAndRewriteErrors()
        {
            // arrange
            string query = null;
            int    count = 0;

            var result = QueryResultBuilder.New();
            var data   = new OrderedDictionary();

            data["__0__a"] = "a";
            data["__1__a"] = "b";
            data["__1__b"] = "c";
            result.SetData(data);
            result.AddError(ErrorBuilder.New()
                            .SetMessage("foo")
                            .SetPath(Path.New("__1__b"))
                            .Build());

            var executor = new Mock <IQueryExecutor>();

            executor.Setup(t => t.ExecuteAsync(
                               It.IsAny <IReadOnlyQueryRequest>(),
                               It.IsAny <CancellationToken>()))
            .Returns(new Func <IReadOnlyQueryRequest,
                               CancellationToken, Task <IExecutionResult> >((r, ct) =>
            {
                count++;
                query = r.Query.ToString();
                return(Task.FromResult <IExecutionResult>(result.Create()));
            }));

            var request_a = QueryRequestBuilder.Create("query a { a }");
            var request_b = QueryRequestBuilder.Create("query b { a b }");

            var client = new RemoteQueryClient(
                new EmptyServiceProvider(),
                executor.Object);

            // act
            Task <IExecutionResult> task_a = client.ExecuteAsync(request_a);
            Task <IExecutionResult> task_b = client.ExecuteAsync(request_b);
            await client.DispatchAsync(CancellationToken.None);

            // assert
            Assert.Equal(1, count);

            IExecutionResult result_a = await task_a;

            result_a.MatchSnapshot("DispatchMultipleQueriesAndRewriteErrors_A");

            IExecutionResult result_b = await task_b;

            result_b.MatchSnapshot("DispatchMultipleQueriesAndRewriteErrors_B");
        }
        public async Task DispatchMultipleQueriesWithVariables()
        {
            // arrange
            IReadOnlyQueryRequest mergedRequest = null;
            int count = 0;

            var result = new QueryResult();

            result.Data["__0__a"] = "a";
            result.Data["__1__a"] = "b";
            result.Data["__1__b"] = "c";

            var executor = new Mock <IQueryExecutor>();

            executor.Setup(t => t.ExecuteAsync(
                               It.IsAny <IReadOnlyQueryRequest>(),
                               It.IsAny <CancellationToken>()))
            .Returns(new Func <IReadOnlyQueryRequest,
                               CancellationToken, Task <IExecutionResult> >((r, ct) =>
            {
                count++;
                mergedRequest = r;
                return(Task.FromResult <IExecutionResult>(result));
            }));

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

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

            var client = new RemoteQueryClient(
                new EmptyServiceProvider(),
                executor.Object);

            // act
            Task <IExecutionResult> task_a = client.ExecuteAsync(request_a);
            Task <IExecutionResult> task_b = client.ExecuteAsync(request_b);
            await client.DispatchAsync(CancellationToken.None);

            // assert
            await task_a;
            await task_b;

            Assert.Equal(1, count);
            mergedRequest.MatchSnapshot();
        }
        public async Task DispatchMultipleQueries()
        {
            // arrange
            string query = null;
            int    count = 0;

            var result = new QueryResult();

            result.Data["__0__a"] = "a";
            result.Data["__1__a"] = "b";
            result.Data["__1__b"] = "c";

            var executor = new Mock <IQueryExecutor>();

            executor.Setup(t => t.ExecuteAsync(
                               It.IsAny <IReadOnlyQueryRequest>(),
                               It.IsAny <CancellationToken>()))
            .Returns(new Func <IReadOnlyQueryRequest,
                               CancellationToken, Task <IExecutionResult> >((r, ct) =>
            {
                count++;
                query = r.Query.ToString();
                return(Task.FromResult <IExecutionResult>(result));
            }));

            var request_a = QueryRequestBuilder.Create("query a { a }");
            var request_b = QueryRequestBuilder.Create("query b { a b }");

            var client = new RemoteQueryClient(
                new EmptyServiceProvider(),
                executor.Object);

            // act
            Task <IExecutionResult> task_a = client.ExecuteAsync(request_a);
            Task <IExecutionResult> task_b = client.ExecuteAsync(request_b);
            await client.DispatchAsync(CancellationToken.None);

            // assert
            Assert.Equal(1, count);
            query.MatchSnapshot("DispatchMultipleQueries_MergedQuery");

            IExecutionResult result_a = await task_a;

            result_a.MatchSnapshot("DispatchMultipleQueries_Result_A");

            IExecutionResult result_b = await task_b;

            result_b.MatchSnapshot("DispatchMultipleQueries_Result_B");
        }
        public async Task DispatchSingleQuery()
        {
            // arrange
            string query = null;

            var executor = new Mock <IQueryExecutor>();

            executor.Setup(t => t.ExecuteAsync(
                               It.IsAny <IReadOnlyQueryRequest>(),
                               It.IsAny <CancellationToken>()))
            .Returns(new Func <IReadOnlyQueryRequest,
                               CancellationToken, Task <IExecutionResult> >((r, ct) =>
            {
                query = r.Query.ToString();
                return(Task.FromResult <IExecutionResult>(null));
            }));

            var client = new RemoteQueryClient(
                new EmptyServiceProvider(),
                executor.Object);

            // act
            Task result = client.ExecuteAsync(
                QueryRequestBuilder.New()
                .SetQuery("{ a }")
                .Create());
            await client.DispatchAsync(CancellationToken.None);

            await result;


            query.MatchSnapshot();
        }
        public async Task DispatchMultipleQueriesWithVariables()
        {
            // arrange
            IReadOnlyQueryRequest mergedRequest = null;
            int count = 0;

            var result = QueryResultBuilder.New();
            var data   = new OrderedDictionary();

            data["__0__a"] = "a";
            data["__1__a"] = "b";
            data["__1__b"] = "c";
            result.SetData(data);

            var schema = SchemaBuilder.New()
                         .AddDocumentFromString("type Query { foo: String }")
                         .AddResolver("Query", "foo", c => "bar")
                         .Create();

            var executor = new Mock <IQueryExecutor>();

            executor.Setup(t => t.ExecuteAsync(
                               It.IsAny <IReadOnlyQueryRequest>(),
                               It.IsAny <CancellationToken>()))
            .Returns(new Func <IReadOnlyQueryRequest,
                               CancellationToken, Task <IExecutionResult> >((r, ct) =>
            {
                count++;
                mergedRequest = r;
                return(Task.FromResult <IExecutionResult>(result.Create()));
            }));
            executor.Setup(t => t.Schema).Returns(schema);

            var request_a = QueryRequestBuilder.New()
                            .SetQuery("query a($a: String) { a(b: $a) }")
                            .SetVariableValue("a", "foo")
                            .Create();

            var request_b = QueryRequestBuilder.Create(
                "query b { a b }");

            var client = new RemoteQueryClient(
                new EmptyServiceProvider(),
                executor.Object);

            // act
            Task <IExecutionResult> task_a = client.ExecuteAsync(request_a);
            Task <IExecutionResult> task_b = client.ExecuteAsync(request_b);
            await client.DispatchAsync(CancellationToken.None);

            // assert
            await task_a;
            await task_b;

            Assert.Equal(1, count);
            new
            {
                Query         = mergedRequest.Query.ToString(),
                QueryName     = mergedRequest.QueryName,
                QueryHash     = mergedRequest.QueryHash,
                OperationName = mergedRequest.OperationName,
                Varables      = mergedRequest.VariableValues,
                Extensions    = mergedRequest.Extensions,
            }.MatchSnapshot();
        }