public Task UnsubscribeAsync()
 {
     _logger.LogDebug("Subscription: {subscriptionId} unsubscribing", Id);
     _unsubscribe.Dispose();
     return(_writer.SendAsync(new OperationMessage
     {
         Type = MessageType.GQL_COMPLETE,
         Id = Id
     }));
 }
        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);
        }