예제 #1
0
        private static Subscriber DefaultCreateRemoteSubscriber(ExecutionResultLink link)
        {
            return(async(context, unsubscribe) =>
            {
                var document = CreateDocument(context);
                var variables = context.ExecutionContext.CoercedVariableValues;

                var result = await link(document, variables, unsubscribe);

                var stream = new EventChannel <ExecutionResult>();

                var _ = Task.Run(async() =>
                {
                    while (await result.WaitToReadAsync(unsubscribe))
                    {
                        if (result.TryRead(out var executionResult))
                        {
                            await stream.WriteAsync(
                                executionResult);
                        }
                    }

                    await result.Completion;
                }, unsubscribe);

                return Resolve.Subscribe(stream, unsubscribe);
            });

            GraphQLDocument CreateDocument(ResolverContext context)
            {
                return(context.ExecutionContext.Document);
            }
        }
예제 #2
0
        public async Task Should_stream_a_lot()
        {
            /* Given */
            const int count       = 10_000;
            var       unsubscribe = new CancellationTokenSource(TimeSpan.FromMinutes(3));

            var query = @"
                subscription MessageAdded {
                    messageAdded {
                        content
                    }
                }
                ";

            /* When */
            var result = await Executor.SubscribeAsync(new ExecutionOptions
            {
                Document = Parser.ParseDocument(query),
                Schema   = _executable
            }, unsubscribe.Token).ConfigureAwait(false);

            for (var i = 0; i < count; i++)
            {
                var expected = new Message {
                    Content = i.ToString()
                };
                await _messagesChannel.WriteAsync(expected);
            }

            /* Then */
            for (var i = 0; i < count; i++)
            {
                var actualResult = await result.Source.Reader.ReadAsync(unsubscribe.Token).ConfigureAwait(false);

                actualResult.ShouldMatchJson(@"{
                    ""data"":{
                        ""messageAdded"": {
                            ""content"": ""{counter}""
                        }
                    }
                }".Replace("{counter}", i.ToString()));
            }

            unsubscribe.Cancel();
        }
예제 #3
0
        public async Task <Message> AddMessageAsync(
            string fromId,
            string content)
        {
            var from = await GetFromAsync(fromId);

            var message = new Message
            {
                Id        = $"{++_lastId}",
                Content   = content,
                Timestamp = DateTimeOffset.UtcNow,
                From      = from
            };

            _messages.Enqueue(message);
            await _messageStream.WriteAsync(message);

            return(message);
        }