Ejemplo n.º 1
0
        private async ValueTask ExecuteBufferedRequestBatchAsync(
            CancellationToken cancellationToken)
        {
            // first we take all buffered requests and merge them into a single request.
            // we however have to group requests by operation type. This means we should
            // end up with one or two requests (query and mutation).
            foreach ((IQueryRequest Merged, IEnumerable <BufferedRequest> Requests)batch in
                     MergeRequestHelper.MergeRequests(_bufferedRequests))
            {
                // now we take this merged request and run it against the executor.
                IExecutionResult result = await _executor
                                          .ExecuteAsync(batch.Merged, cancellationToken)
                                          .ConfigureAwait(false);

                if (result is IQueryResult queryResult)
                {
                    // last we will extract the results for the original buffered requests
                    // and fulfil the promises.
                    MergeRequestHelper.DispatchResults(queryResult, batch.Requests);
                }
                else
                {
                    // since we only support query/mutation at this point we will just fail
                    // in the event that something else was returned.
                    foreach (BufferedRequest request in batch.Requests)
                    {
                        request.Promise.SetException(new NotSupportedException(
                                                         "Only IQueryResult is supported when batching."));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public async Task Create_BufferedRequest_With_Mixed_Operations()
        {
            // arrange
            ISchema schema =
                await new ServiceCollection()
                .AddGraphQL()
                .AddCustomerSchema()
                .BuildSchemaAsync();

            var variableNames = new HashSet <string> {
                "id"
            };

            var queryA = "query abc($id: ID) { customer(id: $id) { name } }";
            var queryB = "query abc($id: ID) { customer(id: $id) { id } }";
            var queryC = "mutation { createCustomer(input: { name: \"a\" }) { customer { id } } }";

            IQueryRequest requestA =
                QueryRequestBuilder.New()
                .SetQuery(queryA)
                .SetVariableValue("id", "1")
                .Create();

            IQueryRequest requestB =
                QueryRequestBuilder.New()
                .SetQuery(queryB)
                .SetVariableValue("id", "1")
                .Create();

            IQueryRequest requestC =
                QueryRequestBuilder.New()
                .SetQuery(queryC)
                .Create();

            var bufferedRequestA = BufferedRequest.Create(requestA, schema);
            var bufferedRequestB = BufferedRequest.Create(requestB, schema);
            var bufferedRequestC = BufferedRequest.Create(requestC, schema);

            // act
            IEnumerable <(IQueryRequest, IEnumerable <BufferedRequest>)> mergeResult =
                MergeRequestHelper.MergeRequests(new[]
            {
                bufferedRequestA,
                bufferedRequestB,
                bufferedRequestC
            },
                                                 variableNames);

            // assert
            string.Join(Environment.NewLine + "-------" + Environment.NewLine,
                        mergeResult
                        .Select(t => t.Item1)
                        .Select(t => Utf8GraphQLParser.Parse(t.Query !.AsSpan()).ToString(true)))
            .MatchSnapshot();
        }
Ejemplo n.º 3
0
        public async Task Merge_Requests_With_Variables_On_Directives()
        {
            // arrange
            ISchema schema =
                await new ServiceCollection()
                .AddGraphQL()
                .AddCustomerSchema()
                .BuildSchemaAsync();

            var queryA =
                @"query abc($id: ID $if: Boolean) {
                    customer(id: $id) {
                        name @include(id: $if)
                    }
                }";

            var queryB =
                @"query abc($id: ID  $if: Boolean) {
                    customer(id: $id) {
                        id @include(id: $if)
                    }
                }";

            IQueryRequest requestA =
                QueryRequestBuilder.New()
                .SetQuery(queryA)
                .SetVariableValue("id", "1")
                .SetVariableValue("if", true)
                .Create();

            IQueryRequest requestB =
                QueryRequestBuilder.New()
                .SetQuery(queryB)
                .SetVariableValue("id", "1")
                .SetVariableValue("if", true)
                .Create();

            var bufferedRequestA = BufferedRequest.Create(requestA, schema);
            var bufferedRequestB = BufferedRequest.Create(requestB, schema);

            // act
            IEnumerable <(IQueryRequest, IEnumerable <BufferedRequest>)> mergeResult =
                MergeRequestHelper.MergeRequests(new[] { bufferedRequestA, bufferedRequestB });

            // assert
            string.Join(Environment.NewLine + "-------" + Environment.NewLine,
                        mergeResult
                        .Select(t => t.Item1)
                        .Select(t => Utf8GraphQLParser.Parse(t.Query !.AsSpan()).ToString(true)))
            .MatchSnapshot();
        }
Ejemplo n.º 4
0
        public async Task Create_BufferedRequest_AutoGenerated()
        {
            // arrange
            ISchema schema =
                await new ServiceCollection()
                .AddGraphQL()
                .AddCustomerSchema()
                .BuildSchemaAsync();

            var queryA = "query abc($id: ID) { customer(id: $id) { name } }";
            var queryB = "query abc($id: ID) { customer(id: $id) { id } }";

            IQueryRequest requestA =
                QueryRequestBuilder.New()
                .SetQuery(queryA)
                .SetVariableValue("id", "1")
                .SetProperty(WellKnownContextData.IsAutoGenerated, true)
                .Create();

            IQueryRequest requestB =
                QueryRequestBuilder.New()
                .SetQuery(queryB)
                .SetVariableValue("id", "1")
                .SetProperty(WellKnownContextData.IsAutoGenerated, true)
                .Create();

            var bufferedRequestA = BufferedRequest.Create(requestA, schema);
            var bufferedRequestB = BufferedRequest.Create(requestB, schema);

            // act
            IEnumerable <(IQueryRequest, IEnumerable <BufferedRequest>)> mergeResult =
                MergeRequestHelper.MergeRequests(
                    new[] { bufferedRequestA, bufferedRequestB });

            // assert
            string.Join(Environment.NewLine + "-------" + Environment.NewLine,
                        mergeResult
                        .Select(t => t.Item1)
                        .Select(t => Utf8GraphQLParser.Parse(t.Query !.AsSpan()).ToString(true)))
            .MatchSnapshot();
        }