Search in sources :

Example 1 with GraphQlRequest

use of org.springframework.graphql.GraphQlRequest in project spring-graphql by spring-projects.

the class GraphQlClientBuilderTests method mutateDocumentSource.

@Test
void mutateDocumentSource() {
    DocumentSource documentSource = name -> name.equals("name") ? Mono.just(DOCUMENT) : Mono.error(new IllegalArgumentException());
    setMockResponse("{}");
    // Original
    GraphQlClient.Builder<?> builder = graphQlClientBuilder().documentSource(documentSource);
    GraphQlClient client = builder.build();
    client.documentName("name").execute().block(TIMEOUT);
    GraphQlRequest request = request();
    assertThat(request.getDocument()).isEqualTo(DOCUMENT);
    // Mutate
    client = client.mutate().build();
    client.documentName("name").execute().block(TIMEOUT);
    assertThat(request().getDocument()).isEqualTo(DOCUMENT);
}
Also used : Test(org.junit.jupiter.api.Test) DocumentSource(org.springframework.graphql.support.DocumentSource) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) GraphQlRequest(org.springframework.graphql.GraphQlRequest) Mono(reactor.core.publisher.Mono) DocumentSource(org.springframework.graphql.support.DocumentSource) GraphQlRequest(org.springframework.graphql.GraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 2 with GraphQlRequest

use of org.springframework.graphql.GraphQlRequest in project spring-graphql by spring-projects.

the class MockWebSocketGraphQlTransportTests method requestCancelled.

@Test
void requestCancelled() {
    GraphQlRequest request = this.mockServer.expectOperation("{Query1}").andRespond(Mono.never());
    StepVerifier.create(this.transport.execute(request)).thenAwait(Duration.ofMillis(200)).thenCancel().verify(TIMEOUT);
    assertActualClientMessages(GraphQlMessage.connectionInit(null), GraphQlMessage.subscribe("1", request));
}
Also used : GraphQlRequest(org.springframework.graphql.GraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 3 with GraphQlRequest

use of org.springframework.graphql.GraphQlRequest in project spring-graphql by spring-projects.

the class MockWebSocketGraphQlTransportTests method requestStreamError.

@Test
void requestStreamError() {
    GraphQlRequest request = this.mockServer.expectOperation("{Sub1}").andStreamWithError(Flux.just(this.result1), GraphqlErrorBuilder.newError().message("boo").build());
    StepVerifier.create(this.transport.executeSubscription(request)).expectNext(this.result1).expectErrorSatisfies(actualEx -> {
        List<GraphQLError> errorList = ((SubscriptionErrorException) actualEx).getErrors();
        assertThat(errorList).extracting(GraphQLError::getMessage).containsExactly("boo");
    }).verify(TIMEOUT);
    assertActualClientMessages(GraphQlMessage.connectionInit(null), GraphQlMessage.subscribe("1", request));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) WebSocketSession(org.springframework.web.reactive.socket.WebSocketSession) CloseStatus(org.springframework.web.reactive.socket.CloseStatus) WebSocketClient(org.springframework.web.reactive.socket.client.WebSocketClient) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TestWebSocketClient(org.springframework.graphql.web.TestWebSocketClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutionResult(graphql.ExecutionResult) GraphQLError(graphql.GraphQLError) Duration(java.time.Duration) Map(java.util.Map) TestWebSocketConnection(org.springframework.graphql.web.TestWebSocketConnection) WebSocketHandler(org.springframework.web.reactive.socket.WebSocketHandler) URI(java.net.URI) MapExecutionResult(org.springframework.graphql.support.MapExecutionResult) HttpHeaders(org.springframework.http.HttpHeaders) GraphQlRequest(org.springframework.graphql.GraphQlRequest) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) GraphqlErrorBuilder(graphql.GraphqlErrorBuilder) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) ClientCodecConfigurer(org.springframework.http.codec.ClientCodecConfigurer) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) GraphQLError(graphql.GraphQLError) List(java.util.List) GraphQlRequest(org.springframework.graphql.GraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 4 with GraphQlRequest

use of org.springframework.graphql.GraphQlRequest in project spring-graphql by spring-projects.

the class MockWebSocketGraphQlTransportTests method requestStreamCancelled.

@Test
void requestStreamCancelled() {
    GraphQlRequest request = this.mockServer.expectOperation("{Sub1}").andStream(Flux.just(this.result1).concatWith(Flux.never()));
    StepVerifier.create(this.transport.executeSubscription(request)).expectNext(this.result1).thenAwait(Duration.ofMillis(200)).thenCancel().verify(TIMEOUT);
    assertActualClientMessages(GraphQlMessage.connectionInit(null), GraphQlMessage.subscribe("1", request), GraphQlMessage.complete("1"));
}
Also used : GraphQlRequest(org.springframework.graphql.GraphQlRequest) Test(org.junit.jupiter.api.Test)

Example 5 with GraphQlRequest

use of org.springframework.graphql.GraphQlRequest in project spring-graphql by spring-projects.

the class GraphQlClientTests method operationNameAndVariables.

@Test
void operationNameAndVariables() {
    String document = "query HeroNameAndFriends($episode: Episode) {" + "  hero(episode: $episode) {" + "    name" + "  }" + "}";
    setMockResponse("{\"hero\": {\"name\":\"R2-D2\"}}");
    GraphQlClient.Response response = graphQlClient().document(document).operationName("HeroNameAndFriends").variable("episode", "JEDI").variable("foo", "bar").variable("keyOnly", null).execute().block(TIMEOUT);
    assertThat(response).isNotNull();
    MovieCharacter character = response.toEntity("hero", MovieCharacter.class);
    assertThat(character).isEqualTo(MovieCharacter.create("R2-D2"));
    GraphQlRequest request = request();
    assertThat(request.getDocument()).contains(document);
    assertThat(request.getOperationName()).isEqualTo("HeroNameAndFriends");
    assertThat(request.getVariables()).hasSize(3);
    assertThat(request.getVariables()).containsEntry("episode", "JEDI");
    assertThat(request.getVariables()).containsEntry("foo", "bar");
    assertThat(request.getVariables()).containsEntry("keyOnly", null);
}
Also used : GraphQlRequest(org.springframework.graphql.GraphQlRequest) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)12 GraphQlRequest (org.springframework.graphql.GraphQlRequest)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 TestWebSocketClient (org.springframework.graphql.web.TestWebSocketClient)3 TestWebSocketConnection (org.springframework.graphql.web.TestWebSocketConnection)3 Mono (reactor.core.publisher.Mono)3 ExecutionResult (graphql.ExecutionResult)2 GraphQLError (graphql.GraphQLError)2 GraphqlErrorBuilder (graphql.GraphqlErrorBuilder)2 IOException (java.io.IOException)2 URI (java.net.URI)2 Duration (java.time.Duration)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Collectors (java.util.stream.Collectors)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.when (org.mockito.Mockito.when)2