/// <inheritdoc />
        public async ValueTask <IInMemoryClient> CreateAsync(
            string name,
            CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw ThrowHelper.Argument_IsNullOrEmpty(nameof(name));
            }

            InMemoryClientFactoryOptions options = _optionsMonitor.Get(name);
            var client = new InMemoryClient(name);

            for (var i = 0; i < options.InMemoryClientActions.Count; i++)
            {
                await options.InMemoryClientActions[i](client, cancellationToken);
            }

            if (client.Executor is not null)
            {
                client.SchemaName = client.Executor.Schema.Name;
            }
            else
            {
                client.Executor = await _requestExecutorResolver
                                  .GetRequestExecutorAsync(client.SchemaName, cancellationToken);
            }

            return(client);
        }
예제 #2
0
        public async Task ExecuteAsync_Default_CallInterceptor()
        {
            // arrange
            var interceptorMock  = new Mock <IInMemoryRequestInterceptor>();
            var client           = new InMemoryClient("Foo");
            var variables        = new Dictionary <string, object?>();
            var operationRequest = new OperationRequest("foo", new StubDocument(), variables);
            var executor         = new StubExecutor();

            client.Executor = executor;
            client.RequestInterceptors.Add(interceptorMock.Object);
            client.RequestInterceptors.Add(interceptorMock.Object);
            interceptorMock
            .Setup(x => x
                   .OnCreateAsync(
                       StubExecutor.ApplicationServiceProvider,
                       operationRequest,
                       It.IsAny <IQueryRequestBuilder>(),
                       It.IsAny <CancellationToken>()));

            // act
            await client.ExecuteAsync(operationRequest);

            // assert
            interceptorMock
            .Verify(x => x
                    .OnCreateAsync(
                        StubExecutor.ApplicationServiceProvider,
                        operationRequest,
                        It.IsAny <IQueryRequestBuilder>(),
                        It.IsAny <CancellationToken>()),
                    Times.Exactly(2));
        }
예제 #3
0
        public async Task ExecuteAsync_NoRequest_ThrowException()
        {
            // arrange
            var client = new InMemoryClient("Foo");

            // act
            Exception?ex =
                await Record.ExceptionAsync(async() => await client.ExecuteAsync(null !));

            // assert
            Assert.IsType <ArgumentNullException>(ex);
        }
예제 #4
0
        public async Task ExecuteAsync_NoExecutor_ThrowException()
        {
            // arrange
            var client           = new InMemoryClient("Foo");
            var operationRequest =
                new OperationRequest("foo", new StubDocument());

            // act
            Exception?ex =
                await Record.ExceptionAsync(async() =>
                                            await client.ExecuteAsync(operationRequest));

            // assert
            Assert.IsType <GraphQLClientException>(ex);
        }
예제 #5
0
        public async Task ExecuteAsync_Default_ExecuteQuery()
        {
            // arrange
            var client           = new InMemoryClient("Foo");
            var variables        = new Dictionary <string, object?>();
            var operationRequest = new OperationRequest("foo", new StubDocument(), variables);
            var executor         = new StubExecutor();

            client.Executor = executor;

            // act
            await client.ExecuteAsync(operationRequest);

            // assert
            Assert.Equal(operationRequest.Name, executor.Request.OperationName);
            Assert.Equal(variables, executor.Request.VariableValues);
            Assert.Equal("{ foo }", Encoding.UTF8.GetString(executor.Request.Query !.AsSpan()));
        }