Ejemplo n.º 1
0
    protected static async Task <IBatchQueryResult> ExecuteOperationBatchAsync(
        HttpContext context,
        IRequestExecutor requestExecutor,
        IHttpRequestInterceptor requestInterceptor,
        IServerDiagnosticEvents diagnosticEvents,
        GraphQLRequest request,
        IReadOnlyList <string> operationNames)
    {
        diagnosticEvents.StartOperationBatchRequest(context, request, operationNames);

        var requestBatch = new IReadOnlyQueryRequest[operationNames.Count];

        for (var i = 0; i < operationNames.Count; i++)
        {
            QueryRequestBuilder requestBuilder = QueryRequestBuilder.From(request);
            requestBuilder.SetOperation(operationNames[i]);

            await requestInterceptor.OnCreateAsync(
                context, requestExecutor, requestBuilder, context.RequestAborted);

            requestBatch[i] = requestBuilder.Create();
        }

        return(await requestExecutor.ExecuteBatchAsync(
                   requestBatch, cancellationToken : context.RequestAborted));
    }
        public async Task ExecuteExportLeafList()
        {
            // arrange
            Snapshot.FullName();

            IRequestExecutor executor = await CreateExecutorAsync(c => c
                                                                  .AddQueryType(d => d.Name("Query")
                                                                                .Field("foo")
                                                                                .Argument("bar", a => a.Type <ListType <StringType> >())
                                                                                .Type <ListType <StringType> >()
                                                                                .Resolver <List <string> >(c =>
            {
                var list = c.ArgumentValue <List <string> >("bar");
                if (list is null)
                {
                    return(new List <string>
                    {
                        "123",
                        "456"
                    });
                }
                else
                {
                    list.Add("789");
                    return(list);
                }
            }))
                                                                  .AddExportDirectiveType());

            // act
            var batch = new List <IReadOnlyQueryRequest>
            {
                QueryRequestBuilder.New()
                .SetQuery(
                    @"{
                            foo @export(as: ""b"")
                        }")
                .Create(),
                QueryRequestBuilder.New()
                .SetQuery(
                    @"{
                            foo(bar: $b)
                        }")
                .Create()
            };

            IBatchQueryResult batchResult = await executor.ExecuteBatchAsync(batch);

            // assert
            await batchResult.ToJsonAsync().MatchSnapshotAsync();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Executes the given GraphQL <paramref name="requestBatch" />.
        /// </summary>
        /// <param name="services">
        /// The service provider that contains the executor.
        /// </param>
        /// <param name="requestBatch">
        /// The GraphQL request batch.
        /// </param>
        /// <param name="allowParallelExecution">
        /// Defines if the executor is allowed to execute the batch in parallel.
        /// </param>
        /// <param name="schemaName">
        /// The schema name.
        /// </param>
        /// <param name="cancellationToken">
        /// The cancellation token.
        /// </param>
        /// <returns>
        /// Returns a stream of query results.
        /// </returns>
        public static async Task <IBatchQueryResult> ExecuteBatchRequestAsync(
            this IServiceProvider services,
            IEnumerable <IQueryRequest> requestBatch,
            bool allowParallelExecution         = false,
            NameString schemaName               = default,
            CancellationToken cancellationToken = default)
        {
            IRequestExecutor executor =
                await GetRequestExecutorAsync(services, schemaName, cancellationToken)
                .ConfigureAwait(false);

            return(await executor.ExecuteBatchAsync(
                       requestBatch, allowParallelExecution, cancellationToken));
        }
        public async Task Convert_List_To_Single_Value_With_Converters()
        {
            // arrange
            Snapshot.FullName();

            IRequestExecutor executor = await CreateExecutorAsync(c => c
                                                                  .AddQueryType(d =>
            {
                d.Name("Query");

                d.Field("foo")
                .Argument("bar", a => a.Type <ListType <StringType> >())
                .Type <ListType <StringType> >()
                .Resolve(ctx =>
                {
                    List <string> list = ctx.ArgumentValue <List <string> >("bar");
                    list.Add("789");
                    return(list);
                });

                d.Field("baz")
                .Argument("bar", a => a.Type <StringType>())
                .Resolve(ctx => ctx.ArgumentValue <string>("bar"));
            })
                                                                  .AddExportDirectiveType());

            // act
            var batch = new List <IReadOnlyQueryRequest>
            {
                QueryRequestBuilder.New()
                .SetQuery(
                    @"query foo1($b1: [String]) {
                            foo(bar: $b1) @export(as: ""b2"")
                        }")
                .AddVariableValue("b1", new[] { "123" })
                .Create(),
                QueryRequestBuilder.New()
                .SetQuery(
                    @"query foo2($b2: String) {
                            baz(bar: $b2)
                        }")
                .Create()
            };

            IBatchQueryResult batchResult = await executor.ExecuteBatchAsync(batch);

            // assert
            await batchResult.ToJsonAsync().MatchSnapshotAsync();
        }
        public async Task ExecuteExportObject()
        {
            // arrange
            Snapshot.FullName();

            IRequestExecutor executor = await CreateExecutorAsync(c => c
                                                                  .AddStarWarsTypes()
                                                                  .AddExportDirectiveType()
                                                                  .AddInMemorySubscriptions()
                                                                  .Services
                                                                  .AddStarWarsRepositories());

            // act
            var batch = new List <IReadOnlyQueryRequest>
            {
                QueryRequestBuilder.New()
                .SetQuery(
                    @"
                        mutation firstReview {
                            createReview(
                                episode: NEW_HOPE
                                review: { commentary: ""foo"", stars: 4 })
                                    @export(as: ""r"") {
                                commentary
                                stars
                            }
                        }")
                .Create(),
                QueryRequestBuilder.New()
                .SetQuery(
                    @"
                        mutation secondReview {
                            createReview(
                                episode: EMPIRE
                                review: $r) {
                                commentary
                                stars
                            }
                        }")
                .Create()
            };

            IBatchQueryResult batchResult = await executor.ExecuteBatchAsync(batch);

            // assert
            await batchResult.ToJsonAsync().MatchSnapshotAsync();
        }
        public async Task Batch_Is_Null()
        {
            // arrange
            Snapshot.FullName();

            IRequestExecutor executor = await CreateExecutorAsync(c => c
                                                                  .AddStarWarsTypes()
                                                                  .AddExportDirectiveType()
                                                                  .Services
                                                                  .AddStarWarsRepositories());

            // act
            Func <Task> action = () => executor.ExecuteBatchAsync(null);

            // assert
            await Assert.ThrowsAsync <ArgumentNullException>(action);
        }
Ejemplo n.º 7
0
        public async Task Add_Value_To_Variable_List()
        {
            // arrange
            Snapshot.FullName();

            IRequestExecutor executor = await CreateExecutorAsync(c => c
                                                                  .AddQueryType(d => d.Name("Query")
                                                                                .Field("foo")
                                                                                .Argument("bar", a => a.Type <ListType <StringType> >())
                                                                                .Type <ListType <StringType> >()
                                                                                .Resolver <List <string> >(c =>
            {
                var list = c.ArgumentValue <List <string> >("bar");
                list.Add("789");
                return(list);
            }))
                                                                  .AddExportDirectiveType());

            // act
            var batch = new List <IReadOnlyQueryRequest>
            {
                QueryRequestBuilder.New()
                .SetQuery(
                    @"query foo1($b: [String]) {
                            foo(bar: $b) @export(as: ""b"")
                        }")
                .AddVariableValue("b", new[] { "123" })
                .Create(),
                QueryRequestBuilder.New()
                .SetQuery(
                    @"query foo2($b: [String]) {
                            foo(bar: $b)
                        }")
                .Create()
            };

            IBatchQueryResult batchResult = await executor.ExecuteBatchAsync(batch);

            // assert
            await batchResult.ToJsonAsync().MatchSnapshotAsync();
        }
Ejemplo n.º 8
0
    /// <summary>
    /// Executes the given GraphQL <paramref name="requestBatch" />.
    /// </summary>
    /// <param name="requestBatch">
    /// The GraphQL request batch.
    /// </param>
    /// <param name="allowParallelExecution">
    /// Defines if the executor is allowed to execute the batch in parallel.
    /// </param>
    /// <param name="cancellationToken">
    /// The cancellation token.
    /// </param>
    /// <returns>
    /// Returns a stream of query results.
    /// </returns>
    public async Task <IBatchQueryResult> ExecuteBatchAsync(
        IEnumerable <IQueryRequest> requestBatch,
        bool allowParallelExecution         = false,
        CancellationToken cancellationToken = default)
    {
        if (requestBatch == null)
        {
            throw new ArgumentNullException(nameof(requestBatch));
        }

        IRequestExecutor executor =
            await GetRequestExecutorAsync(cancellationToken)
            .ConfigureAwait(false);

        IBatchQueryResult result =
            await executor
            .ExecuteBatchAsync(requestBatch, allowParallelExecution, cancellationToken)
            .ConfigureAwait(false);

        return(result);
    }
Ejemplo n.º 9
0
        protected async Task <IBatchQueryResult> ExecuteBatchAsync(
            HttpContext context,
            IRequestExecutor requestExecutor,
            IHttpRequestInterceptor requestInterceptor,
            IReadOnlyList <GraphQLRequest> requests)
        {
            var requestBatch = new IReadOnlyQueryRequest[requests.Count];

            for (var i = 0; i < requests.Count; i++)
            {
                QueryRequestBuilder requestBuilder = QueryRequestBuilder.From(requests[0]);

                await requestInterceptor.OnCreateAsync(
                    context, requestExecutor, requestBuilder, context.RequestAborted);

                requestBatch[i] = requestBuilder.Create();
            }

            return(await requestExecutor.ExecuteBatchAsync(
                       requestBatch, cancellationToken : context.RequestAborted));
        }
Ejemplo n.º 10
0
        public async Task ExecuteExportScalar()
        {
            // arrange
            Snapshot.FullName();

            IRequestExecutor executor = await CreateExecutorAsync(c => c
                                                                  .AddStarWarsTypes()
                                                                  .AddExportDirectiveType()
                                                                  .Services
                                                                  .AddStarWarsRepositories());

            // act
            var batch = new List <IReadOnlyQueryRequest>
            {
                QueryRequestBuilder.New()
                .SetQuery(
                    @"
                        query getHero {
                            hero(episode: EMPIRE) {
                                id @export
                            }
                        }")
                .Create(),
                QueryRequestBuilder.New()
                .SetQuery(
                    @"
                        query getHuman {
                            human(id: $id) {
                                name
                            }
                        }")
                .Create()
            };

            IBatchQueryResult batchResult = await executor.ExecuteBatchAsync(batch);

            // assert
            await batchResult.ToJsonAsync().MatchSnapshotAsync();
        }
Ejemplo n.º 11
0
        public async Task ExecuteExportObjectList()
        {
            // arrange
            Snapshot.FullName();

            IRequestExecutor executor = await CreateExecutorAsync(c => c
                                                                  .AddDocumentFromString(
                                                                      @"
                    type Query {
                        foo(f: [FooInput]) : [Foo]
                    }

                    type Foo {
                        bar: String!
                    }

                    input FooInput {
                        bar: String!
                    }")
                                                                  .AddResolver("Query", "foo", ctx =>
            {
                List <object> list = ctx.ArgumentValue <List <object> >("f");

                if (list is null)
                {
                    return(new List <object>
                    {
                        new Dictionary <string, object>
                        {
                            { "bar", "123" }
                        }
                    });
                }

                list.Add(new Dictionary <string, object>
                {
                    { "bar", "456" }
                });
                return(list);
            })
                                                                  .UseField(next => context =>
            {
                var o = context.Parent <object>();
                if (o is Dictionary <string, object> d &&
                    d.TryGetValue(context.ResponseName, out var v))
                {
                    context.Result = v;
                }
                return(next(context));
            })
                                                                  .AddExportDirectiveType());

            // act
            var batch = new List <IReadOnlyQueryRequest>
            {
                QueryRequestBuilder.New()
                .SetQuery(
                    @"{
                            foo @export(as: ""b"")
                            {
                                bar
                            }
                        }")
                .Create(),
                QueryRequestBuilder.New()
                .SetQuery(
                    @"{
                            foo(f: $b)
                            {
                                bar
                            }
                        }")
                .Create()
            };

            IBatchQueryResult batchResult = await executor.ExecuteBatchAsync(batch);

            // assert
            await batchResult.ToJsonAsync().MatchSnapshotAsync();
        }
 /// <summary>
 /// Executes the given GraphQL <paramref name="requestBatch" />.
 /// </summary>
 /// <param name="requestBatch">
 /// The GraphQL request batch.
 /// </param>
 /// <param name="allowParallelExecution">
 /// Defines if the executor is allowed to execute the batch in parallel.
 /// </param>
 /// <param name="cancellationToken">
 /// The cancellation token.
 /// </param>
 /// <returns>
 /// Returns a stream of query results.
 /// </returns>
 public Task <IBatchQueryResult> ExecuteBatchAsync(
     IEnumerable <IQueryRequest> requestBatch,
     bool allowParallelExecution         = false,
     CancellationToken cancellationToken = default) =>
 _executor.ExecuteBatchAsync(requestBatch, allowParallelExecution, cancellationToken);