Example #1
0
        protected override async Task HandleAsync(
            ISocketConnection connection,
            DataStartMessage message,
            CancellationToken cancellationToken)
        {
            IQueryRequestBuilder requestBuilder =
                QueryRequestBuilder.From(message.Payload)
                .SetServices(connection.RequestServices);

            await _socketSessionInterceptor.OnRequestAsync(
                connection, requestBuilder, cancellationToken);

            IExecutionResult result = await _requestExecutor.ExecuteAsync(
                requestBuilder.Create(), cancellationToken);

            switch (result)
            {
            case IResponseStream responseStream:
                var subscription = new Subscription(connection, responseStream, message.Id);
                connection.Subscriptions.Register(subscription);
                break;

            case IQueryResult queryResult:
                using (queryResult)
                    await HandleQueryResultAsync(
                        connection, message, queryResult, cancellationToken);
                break;

            default:
                throw DataStartMessageHandler_RequestTypeNotSupported();
            }
        }
        protected override async Task HandleAsync(
            ISocketConnection connection,
            DataStartMessage message,
            CancellationToken cancellationToken)
        {
            IQueryRequestBuilder requestBuilder =
                QueryRequestBuilder.New()
                .SetQuery(message.Payload.Query)
                .SetQueryId(message.Payload.QueryId)
                .SetOperation(message.Payload.OperationName)
                .SetVariableValues(message.Payload.Variables)
                .SetProperties(message.Payload.Extensions)
                .SetServices(connection.RequestServices);

            await _socketSessionInterceptor.OnRequestAsync(
                connection, requestBuilder, cancellationToken);

            IExecutionResult result = await _requestExecutor.ExecuteAsync(
                requestBuilder.Create(), cancellationToken);

            switch (result)
            {
            case IResponseStream responseStream:
                var subscription = new Subscription(connection, responseStream, message.Id);
                connection.Subscriptions.Register(subscription);
                break;

            case IQueryResult queryResult:
                using (queryResult)
                    await HandleQueryResultAsync(
                        connection, message, queryResult, cancellationToken);
                break;

            default:
                throw new NotSupportedException("The response type is not supported.");
            }
        }
Example #3
0
    protected override async Task HandleAsync(
        ISocketConnection connection,
        DataStartMessage message,
        CancellationToken cancellationToken)
    {
        var session  = new CancellationTokenSource();
        var combined = CancellationTokenSource.CreateLinkedTokenSource(
            session.Token, cancellationToken);
        var sessionIsHandled = false;

        IExecutionResult result = await ExecuteAsync(combined.Token);

        try
        {
            switch (result)
            {
            case SubscriptionResult subscriptionResult:
                // first we add the cts to the result so that they are disposed when the
                // subscription is disposed.
                subscriptionResult.RegisterDisposable(combined);

                // while a subscription result must be disposed we are not handling it here
                // and leave this responsibility to the subscription session.
                ISubscription subscription = GetSubscription(result);

                var subscriptionSession = new SubscriptionSession(
                    session,
                    _socketSessionInterceptor,
                    connection,
                    subscriptionResult,
                    subscription,
                    _diagnosticEvents,
                    message.Id);

                connection.Subscriptions.Register(subscriptionSession);
                sessionIsHandled = true;
                break;

            case IResponseStream streamResult:
                // stream results represent deferred execution streams that use execution
                // resources. We need to ensure that these are disposed when we are
                // finished.
                await using (streamResult)
                {
                    await HandleStreamResultAsync(
                        connection,
                        message,
                        streamResult,
                        cancellationToken);
                }

                break;

            case IQueryResult queryResult:
                // query results use pooled memory an need to be disposed after we have
                // used them.
                using (queryResult)
                {
                    await HandleQueryResultAsync(
                        connection,
                        message,
                        queryResult,
                        cancellationToken);
                }

                break;

            default:
                throw DataStartMessageHandler_RequestTypeNotSupported();
            }
        }
        finally
        {
            if (!sessionIsHandled)
            {
                session.Dispose();
                combined.Dispose();
            }
        }

        async ValueTask <IExecutionResult> ExecuteAsync(CancellationToken cancellationToken)
        {
            try
            {
                IQueryRequestBuilder requestBuilder =
                    QueryRequestBuilder.From(message.Payload)
                    .SetServices(connection.RequestServices);

                await _socketSessionInterceptor.OnRequestAsync(
                    connection, requestBuilder, cancellationToken);

                return(await _requestExecutor.ExecuteAsync(
                           requestBuilder.Create(), cancellationToken));
            }
            catch (Exception ex)
            {
                IErrorBuilder error = _errorHandler.CreateUnexpectedError(ex);
                return(QueryResultBuilder.CreateError(error.Build()));
            }
        }
    }
Example #4
0
        protected override async Task HandleAsync(
            ISocketConnection connection,
            DataStartMessage message,
            CancellationToken cancellationToken)
        {
            IQueryRequestBuilder requestBuilder =
                QueryRequestBuilder.From(message.Payload)
                .SetServices(connection.RequestServices);

            await _socketSessionInterceptor.OnRequestAsync(
                connection, requestBuilder, cancellationToken);

            IExecutionResult result = await _requestExecutor.ExecuteAsync(
                requestBuilder.Create(), cancellationToken);

            switch (result)
            {
            case ISubscriptionResult subscriptionResult:
                // while a subscription result must be disposed we are not handling it here
                // and leave this responsibility to the subscription session.
                ISubscription subscription = GetSubscription(result);

                var subscriptionSession = new SubscriptionSession(
                    connection,
                    subscriptionResult,
                    subscription,
                    _diagnosticEvents,
                    message.Id);

                connection.Subscriptions.Register(subscriptionSession);
                break;

            case IResponseStream streamResult:
                // stream results represent deferred execution streams that use execution
                // resources. We need to ensure that these are disposed when we are
                // finished.
                await using (streamResult)
                {
                    await HandleStreamResultAsync(
                        connection,
                        message,
                        streamResult,
                        cancellationToken);
                }
                break;

            case IQueryResult queryResult:
                // query results use pooled memory an need to be disposed after we have
                // used them.
                using (queryResult)
                {
                    await HandleQueryResultAsync(
                        connection,
                        message,
                        queryResult,
                        cancellationToken);
                }
                break;

            default:
                throw DataStartMessageHandler_RequestTypeNotSupported();
            }
        }