public TestableServerOperations(
     IReaderPipeline reader,
     IWriterPipeline writer,
     ISubscriptionManager subscriptions)
 {
     TransportReader = reader;
     TransportWriter = writer;
     Subscriptions   = subscriptions;
 }
        /// <inheritdoc />
        public async Task SubscribeOrExecuteAsync(
            string id,
            OperationMessagePayload payload,
            IWriterPipeline writer)
        {
            var subscription = await ExecuteAsync(id, payload, writer);

            if (subscription == null)
            {
                return;
            }

            _subscriptions[id] = subscription;
        }
Beispiel #3
0
        public Subscription(string id,
                            OperationMessagePayload payload,
                            SubscriptionExecutionResult result,
                            IWriterPipeline writer,
                            Action <Subscription> completed,
                            ILogger <Subscription> logger)
        {
            _writer         = writer;
            _completed      = completed;
            _logger         = logger;
            Id              = id;
            OriginalPayload = payload;

            Subscribe(result);
        }
Beispiel #4
0
    public Subscription(string id,
                        GraphQLRequest payload,
                        ExecutionResult result,
                        IWriterPipeline writer,
                        Action <Subscription>?completed,
                        ILogger <Subscription> logger)
    {
        _writer         = writer;
        _completed      = completed;
        _logger         = logger;
        Id              = id;
        OriginalPayload = payload;

        Subscribe(result);
    }
 public Task OnAfterHandleAsync(IReaderPipeline reader, IWriterPipeline writer, OperationMessage message)
 {
     HandledMessages.Add(message);
     return(Task.CompletedTask);
 }
Beispiel #6
0
 private void LinkToTransportWriter()
 {
     _logger.LogDebug("Creating writer pipeline");
     TransportWriter = Transport.Writer;
     _logger.LogDebug("Writer pipeline created");
 }
Beispiel #7
0
 public Task OnBeforeHandleAsync(IReaderPipeline reader, IWriterPipeline writer, OperationMessage message)
 {
     _logger.LogDebug("Received message: {message}", message);
     return(Task.CompletedTask);
 }
        private async Task <Subscription> ExecuteAsync(
            string id,
            OperationMessagePayload payload,
            IWriterPipeline writer)
        {
            _logger.LogDebug("Executing operation: {operationName} query: {query}",
                             payload.OperationName,
                             payload.Query);

            var result = await _executer.ExecuteAsync(
                payload.OperationName,
                payload.Query,
                payload.Variables);

            if (result.Errors != null && result.Errors.Any())
            {
                _logger.LogError("Execution errors: {errors}", ResultHelper.GetErrorString(result));
                await writer.SendAsync(new OperationMessage
                {
                    Type    = MessageType.GQL_ERROR,
                    Id      = id,
                    Payload = JObject.FromObject(result)
                });

                return(null);
            }

            // is sub
            if (result is SubscriptionExecutionResult subscriptionExecutionResult)
            {
                using (_logger.BeginScope("Subscribing to: {subscriptionId}", id))
                {
                    if (subscriptionExecutionResult.Streams?.Values.SingleOrDefault() == null)
                    {
                        _logger.LogError("Cannot subscribe as no result stream available");
                        await writer.SendAsync(new OperationMessage
                        {
                            Type    = MessageType.GQL_ERROR,
                            Id      = id,
                            Payload = JObject.FromObject(result)
                        });

                        return(null);
                    }

                    _logger.LogInformation("Creating subscription");
                    return(new Subscription(
                               id,
                               payload,
                               subscriptionExecutionResult,
                               writer,
                               sub => _subscriptions.TryRemove(id, out _),
                               _loggerFactory.CreateLogger <Subscription>()));
                }
            }

            //is query or mutation
            await writer.SendAsync(new OperationMessage
            {
                Type    = MessageType.GQL_DATA,
                Id      = id,
                Payload = JObject.FromObject(result)
            });

            await writer.SendAsync(new OperationMessage
            {
                Type = MessageType.GQL_COMPLETE,
                Id   = id
            });

            return(null);
        }