예제 #1
0
        public void CanHandle_DataStartMessage_True()
        {
            // arrange
            var errorHandler          = new Mock <IErrorHandler>();
            var interceptor           = new DefaultSocketSessionInterceptor();
            IRequestExecutor executor = SchemaBuilder.New()
                                        .AddStarWarsTypes()
                                        .Create()
                                        .MakeExecutable();
            DocumentNode query   = Utf8GraphQLParser.Parse("{ hero { name } }");
            var          handler = new DataStartMessageHandler(
                executor,
                interceptor,
                errorHandler.Object,
                new NoopExecutionDiagnosticEvents());

            var message = new DataStartMessage(
                "123",
                new GraphQLRequest(query));

            // act
            var result = handler.CanHandle(message);

            // assert
            Assert.True(result);
        }
예제 #2
0
        public async Task Handle_Query_With_Inter_DataReceived_And_Completed()
        {
            // arrange
            var errorHandler = new Mock <IErrorHandler>();

            IServiceProvider services = new ServiceCollection()
                                        .AddGraphQL()
                                        .AddStarWarsTypes()
                                        .AddStarWarsRepositories()
                                        .AddInMemorySubscriptions()
                                        .Services
                                        .BuildServiceProvider();

            IRequestExecutor executor = await services
                                        .GetRequiredService <IRequestExecutorResolver>()
                                        .GetRequestExecutorAsync();

            var interceptor = new SocketSessionInterceptorMock();
            var connection  = new SocketConnectionMock {
                RequestServices = services
            };
            DocumentNode query   = Utf8GraphQLParser.Parse("{ hero { name } }");
            var          handler = new DataStartMessageHandler(
                executor,
                interceptor,
                errorHandler.Object,
                new NoopExecutionDiagnosticEvents());
            var message = new DataStartMessage("123", new GraphQLRequest(query));

            var result = (IReadOnlyQueryResult)await executor.ExecuteAsync(
                QueryRequestBuilder.New()
                .SetQuery(query)
                .SetServices(services)
                .Create());

            // act
            await handler.HandleAsync(
                connection,
                message,
                CancellationToken.None);

            // assert
            Assert.Collection(connection.SentMessages,
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new DataResultMessage(message.Id, result).Serialize()));
            },
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new DataCompleteMessage(message.Id).Serialize()));
            });
        }
예제 #3
0
        public async Task Handle_Query_With_Inter_DataReceived_And_Completed()
        {
            // arrange
            var interceptor = new Mock <ISocketQueryRequestInterceptor>();

            interceptor.Setup(t => t.OnCreateAsync(
                                  It.IsAny <ISocketConnection>(),
                                  It.IsAny <IQueryRequestBuilder>(),
                                  It.IsAny <CancellationToken>()))
            .Returns(Task.CompletedTask);

            var connection = new SocketConnectionMock();

            IQueryExecutor executor = SchemaBuilder.New()
                                      .AddStarWarsTypes()
                                      .Create()
                                      .MakeExecutable();

            DocumentNode query = Utf8GraphQLParser.Parse("{ hero { name } }");

            var handler = new DataStartMessageHandler(
                executor,
                new List <ISocketQueryRequestInterceptor> {
                interceptor.Object
            });

            var message = new DataStartMessage(
                "123",
                new GraphQLRequest(query));

            var result = (IReadOnlyQueryResult)await executor.ExecuteAsync(
                QueryRequestBuilder.New()
                .SetQuery(query)
                .Create());

            // act
            await handler.HandleAsync(
                connection,
                message,
                CancellationToken.None);

            // assert
            Assert.Collection(connection.SentMessages,
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new DataResultMessage(message.Id, result).Serialize()));
            },
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new DataCompleteMessage(message.Id).Serialize()));
            });
        }
예제 #4
0
        public void CanHandle_DataStartMessage_True()
        {
            // arrange
            IQueryExecutor executor = SchemaBuilder.New()
                                      .AddStarWarsTypes()
                                      .Create()
                                      .MakeExecutable();

            DocumentNode query = Utf8GraphQLParser.Parse("{ hero { name } }");

            var handler = new DataStartMessageHandler(executor, null);

            var message = new DataStartMessage(
                "123",
                new GraphQLRequest(query));

            // act
            bool result = handler.CanHandle(message);

            // assert
            Assert.True(result);
        }
예제 #5
0
        public async Task Handle_Subscription_DataReceived_And_Completed()
        {
            // arrange
            var errorHandler = new Mock <IErrorHandler>();

            IServiceProvider services = new ServiceCollection()
                                        .AddGraphQL()
                                        .AddStarWarsTypes()
                                        .AddStarWarsRepositories()
                                        .AddInMemorySubscriptions()
                                        .Services
                                        .BuildServiceProvider();

            IRequestExecutor executor = await services
                                        .GetRequiredService <IRequestExecutorResolver>()
                                        .GetRequestExecutorAsync();

            var interceptor = new SocketSessionInterceptorMock();
            var connection  = new SocketConnectionMock {
                RequestServices = services
            };
            DocumentNode query = Utf8GraphQLParser.Parse(
                "subscription { onReview(episode: NEW_HOPE) { stars } }");
            var handler = new DataStartMessageHandler(
                executor,
                interceptor,
                errorHandler.Object,
                new NoopExecutionDiagnosticEvents());
            var message = new DataStartMessage("123", new GraphQLRequest(query));

            // act
            await handler.HandleAsync(
                connection,
                message,
                CancellationToken.None);

            // assert
            Assert.Empty(connection.SentMessages);
            Assert.NotEmpty(connection.Subscriptions);

            var stream =
                (IResponseStream)await executor.ExecuteAsync(
                    "subscription { onReview(episode: NEW_HOPE) { stars } }");

            await executor.ExecuteAsync(@"
                mutation {
                    createReview(episode:NEW_HOPE review:
                        {
                            commentary: ""foo""
                            stars: 5
                        }) {
                        stars
                    }
                }");

            using var cts = new CancellationTokenSource(15000);
            ConfiguredCancelableAsyncEnumerable <IQueryResult> .Enumerator enumerator =
                stream.ReadResultsAsync().WithCancellation(cts.Token).GetAsyncEnumerator();
            Assert.True(await enumerator.MoveNextAsync());

            await Task.Delay(2000, cts.Token);

            Assert.Collection(connection.SentMessages,
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new DataResultMessage(message.Id, enumerator.Current).Serialize()));
            });
        }
예제 #6
0
        public async Task Handle_Subscription_DataReceived_And_Completed()
        {
            // arrange
            var connection = new SocketConnectionMock();

            var services = new ServiceCollection();

            services.AddInMemorySubscriptionProvider();
            services.AddStarWarsRepositories();

            IQueryExecutor executor = SchemaBuilder.New()
                                      .AddServices(services.BuildServiceProvider())
                                      .AddStarWarsTypes()
                                      .Create()
                                      .MakeExecutable();

            DocumentNode query = Utf8GraphQLParser.Parse(
                "subscription { onReview(episode: NEWHOPE) { stars } }");

            var handler = new DataStartMessageHandler(executor, null);

            var message = new DataStartMessage(
                "123",
                new GraphQLRequest(query));

            // act
            await handler.HandleAsync(
                connection,
                message,
                CancellationToken.None);

            // assert
            Assert.Empty(connection.SentMessages);
            Assert.NotEmpty(connection.Subscriptions);

            IResponseStream stream =
                (IResponseStream)await executor.ExecuteAsync(
                    "subscription { onReview(episode: NEWHOPE) { stars } }");

            await executor.ExecuteAsync(@"
                mutation {
                    createReview(episode:NEWHOPE review:
                        {
                            commentary: ""foo""
                            stars: 5
                        }) {
                        stars
                    }
                }");

            IReadOnlyQueryResult result = await stream.ReadAsync();

            await Task.Delay(2000);

            Assert.Collection(connection.SentMessages,
                              t =>
            {
                Assert.True(t.SequenceEqual(
                                new DataResultMessage(message.Id, result).Serialize()));
            });
        }