Exemplo n.º 1
0
        public async Task Interceptor_Set_ContextData()
        {
            // arrange
            CancellationToken ct  = new CancellationTokenSource(20_000).Token;
            var serviceCollection = new ServiceCollection();

            string?result = null !;

            serviceCollection
            .AddGraphQLServer()
            .AddStarWarsTypes()
            .AddExportDirectiveType()
            .AddStarWarsRepositories()
            .AddInMemorySubscriptions()
            .UseField(next => context =>
            {
                result = context.ContextData["Foo"] as string;
                return(next(context));
            });

            serviceCollection
            .AddProtocol <GraphQLWebSocketProtocolFactory>()
            .AddInMemoryClient("Foo")
            .ConfigureRequestInterceptor(new StubInterceptor());

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

            List <JsonDocument> results  = new();
            MockDocument        document = new("query Foo { hero { name } }");
            OperationRequest    request  = new("Foo", document);

            IInMemoryClientFactory factory = services
                                             .GetRequiredService <IInMemoryClientFactory>();

            // act
            var connection =
                new InMemoryConnection(async ct => await factory.CreateAsync("Foo", ct));

            await foreach (var response in connection.ExecuteAsync(request, ct))
            {
                if (response.Body is not null)
                {
                    results.Add(response.Body);
                }
            }

            // assert
            Assert.Equal("bar", result);
        }
Exemplo n.º 2
0
        public async Task Configure_SchemaName()
        {
            // arrange
            CancellationToken ct  = new CancellationTokenSource(20_000).Token;
            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddGraphQLServer("Foo")
            .AddStarWarsTypes()
            .AddExportDirectiveType()
            .AddStarWarsRepositories()
            .AddInMemorySubscriptions();

            serviceCollection
            .AddProtocol <GraphQLWebSocketProtocolFactory>()
            .AddInMemoryClient("Foo")
            .ConfigureInMemoryClient(x => x.SchemaName = "Foo");

            IServiceProvider services =
                serviceCollection.BuildServiceProvider();

            List <JsonDocument> results  = new();
            MockDocument        document = new("query Foo { hero { name } }");
            OperationRequest    request  = new("Foo", document);

            IInMemoryClientFactory factory = services
                                             .GetRequiredService <IInMemoryClientFactory>();

            // act
            var connection =
                new InMemoryConnection(async ct => await factory.CreateAsync("Foo", ct));

            await foreach (var response in connection.ExecuteAsync(request, ct))
            {
                if (response.Body is not null)
                {
                    results.Add(response.Body);
                }
            }

            // assert
            results.Select(x => x.RootElement.ToString()).ToList().MatchSnapshot();
        }