protected static async Task <IQueryResult> ExecuteQueryAsync(
            IExecutionContext executionContext,
            BatchOperationHandler batchOperationHandler,
            CancellationToken cancellationToken)
        {
            ResolverContext[] initialBatch =
                CreateInitialBatch(executionContext,
                                   executionContext.Result.Data);
            try
            {
                await ExecuteResolversAsync(
                    executionContext,
                    initialBatch,
                    batchOperationHandler,
                    cancellationToken)
                .ConfigureAwait(false);

                EnsureRootValueNonNullState(
                    executionContext.Result,
                    initialBatch);

                return(executionContext.Result);
            }
            finally
            {
                ResolverContext.Return(initialBatch);
                ArrayPool <ResolverContext> .Shared.Return(initialBatch);
            }
        }
Exemple #2
0
        private async Task <IExecutionResult> ExecuteMutationAsync(
            IExecutionContext executionContext,
            CancellationToken cancellationToken)
        {
            ResolverContext[] initialBatch =
                CreateInitialBatch(executionContext,
                                   executionContext.Result.Data);

            BatchOperationHandler batchOperationHandler =
                CreateBatchOperationHandler(executionContext);

            try
            {
                await ExecuteResolverBatchSeriallyAsync(
                    executionContext,
                    initialBatch,
                    batchOperationHandler,
                    cancellationToken)
                .ConfigureAwait(false);

                EnsureRootValueNonNullState(
                    executionContext.Result,
                    initialBatch);

                return(executionContext.Result);
            }
            finally
            {
                batchOperationHandler?.Dispose();
                ResolverContext.Return(initialBatch);
                ArrayPool <ResolverContext> .Shared.Return(initialBatch);
            }
        }
        private async Task <IExecutionResult> ExecuteInternalAsync(
            IExecutionContext executionContext)
        {
            object rootValue = executionContext.Operation.RootValue;

            FieldSelection fieldSelection = executionContext.CollectFields(
                executionContext.Schema.SubscriptionType,
                executionContext.Operation.Definition.SelectionSet,
                null)
                                            .Single();

            ImmutableStack <object> source = ImmutableStack.Create(rootValue);

            var subscribeContext = ResolverContext.Rent(
                executionContext,
                fieldSelection,
                source,
                new FieldData(1));

            SubscribeResolverDelegate subscribeResolver =
                fieldSelection.Field.SubscribeResolver
                ?? DefaultSubscribeResolverAsync;

            try
            {
                IAsyncEnumerable <object> sourceStream =
                    await subscribeResolver(subscribeContext)
                    .ConfigureAwait(false);

                return(new SubscriptionResult(
                           sourceStream,
                           message =>
                {
                    IExecutionContext cloned = executionContext.Clone();
                    cloned.ContextData[WellKnownContextData.EventMessage] = message;
                    return cloned;
                },
                           ExecuteSubscriptionQueryAsync,
                           executionContext.ServiceScope,
                           executionContext.RequestAborted));
            }
            finally
            {
                ResolverContext.Return(subscribeContext);
            }
        }
Exemple #4
0
        private static async Task EndExecuteResolverBatchAsync(
            IEnumerable <ResolverContext> batch,
            Action <ResolverContext> enqueueNext,
            CancellationToken cancellationToken)
        {
            foreach (ResolverContext resolverContext in batch)
            {
                if (resolverContext.Task.Status != TaskStatus.RanToCompletion)
                {
                    await resolverContext.Task.ConfigureAwait(false);
                }

                ValueCompletion.CompleteValue(enqueueNext, resolverContext);

                if (!resolverContext.IsRoot)
                {
                    ResolverContext.Return(resolverContext);
                }
            }
        }
Exemple #5
0
        private static async Task ExecuteResolverBatchSeriallyAsync(
            IExecutionContext executionContext,
            IEnumerable <ResolverContext> batch,
            BatchOperationHandler batchOperationHandler,
            CancellationToken cancellationToken)
        {
            var next = new List <ResolverContext>();

            foreach (ResolverContext resolverContext in batch)
            {
                if (resolverContext is null)
                {
                    break;
                }

                await ExecuteResolverSeriallyAsync(
                    resolverContext,
                    next.Add,
                    batchOperationHandler,
                    executionContext.ErrorHandler,
                    cancellationToken)
                .ConfigureAwait(false);

                cancellationToken.ThrowIfCancellationRequested();

                // execute child fields with the default parallel flow logic
                await ExecuteResolversAsync(
                    executionContext,
                    next,
                    batchOperationHandler,
                    cancellationToken)
                .ConfigureAwait(false);

                ResolverContext.Return(next);
                next.Clear();
            }
        }