Search in sources :

Example 1 with GraphQlMessage

use of org.springframework.graphql.web.support.GraphQlMessage in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method errorMessagePayloadIsArray.

@Test
void errorMessagePayloadIsArray() {
    final String GREETING_QUERY = "{" + "\"id\":\"" + SUBSCRIPTION_ID + "\"," + "\"type\":\"subscribe\"," + "\"payload\":{\"query\": \"" + "  subscription TestTypenameSubscription {" + "    greeting" + "  }\"}" + "}";
    String schema = "type Subscription { greeting: String! } type Query { greetingUnused: String! }";
    WebGraphQlHandler initHandler = GraphQlSetup.schemaContent(schema).subscriptionFetcher("greeting", env -> Flux.just("a", null, "b")).webInterceptor().toWebGraphQlHandler();
    GraphQlWebSocketHandler handler = new GraphQlWebSocketHandler(initHandler, ServerCodecConfigurer.create(), Duration.ofSeconds(60));
    TestWebSocketSession session = new TestWebSocketSession(Flux.just(toWebSocketMessage("{\"type\":\"connection_init\"}"), toWebSocketMessage(GREETING_QUERY)));
    handler.handle(session).block(TIMEOUT);
    StepVerifier.create(session.getOutput()).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.CONNECTION_ACK)).consumeNextWith((message) -> {
        GraphQlMessage actual = decode(message);
        assertThat(actual.getId()).isEqualTo(SUBSCRIPTION_ID);
        assertThat(actual.resolvedType()).isEqualTo(GraphQlMessageType.NEXT);
        assertThat(actual.<Map<String, Object>>getPayload()).extractingByKey("data", as(InstanceOfAssertFactories.map(String.class, Object.class))).containsEntry("greeting", "a");
    }).consumeNextWith((message) -> {
        GraphQlMessage actual = decode(message);
        assertThat(actual.getId()).isEqualTo(SUBSCRIPTION_ID);
        assertThat(actual.resolvedType()).isEqualTo(GraphQlMessageType.ERROR);
        assertThat(actual.<List<Map<String, Object>>>getPayload()).asList().hasSize(1).allSatisfy(theError -> assertThat(theError).asInstanceOf(InstanceOfAssertFactories.map(String.class, Object.class)).hasSize(3).hasEntrySatisfying("locations", loc -> assertThat(loc).asList().isEmpty()).hasEntrySatisfying("message", msg -> assertThat(msg).asString().contains("null")).extractingByKey("extensions", as(InstanceOfAssertFactories.map(String.class, Object.class))).containsEntry("classification", "DataFetchingException"));
    }).expectComplete().verify(TIMEOUT);
}
Also used : Sinks(reactor.core.publisher.Sinks) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) DefaultDataBufferFactory(org.springframework.core.io.buffer.DefaultDataBufferFactory) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) CloseStatus(org.springframework.web.reactive.socket.CloseStatus) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphQlSetup(org.springframework.graphql.GraphQlSetup) ArrayList(java.util.ArrayList) Assertions.as(org.assertj.core.api.Assertions.as) Duration(java.time.Duration) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) WebInterceptor(org.springframework.graphql.web.WebInterceptor) ResolvableType(org.springframework.core.ResolvableType) Jackson2JsonDecoder(org.springframework.http.codec.json.Jackson2JsonDecoder) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) Mono(reactor.core.publisher.Mono) WebSocketHandlerTestSupport(org.springframework.graphql.web.WebSocketHandlerTestSupport) DataBuffer(org.springframework.core.io.buffer.DataBuffer) ServerCodecConfigurer(org.springframework.http.codec.ServerCodecConfigurer) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) WebSocketMessage(org.springframework.web.reactive.socket.WebSocketMessage) Collections(java.util.Collections) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 2 with GraphQlMessage

use of org.springframework.graphql.web.support.GraphQlMessage in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method query.

@Test
void query() throws Exception {
    handle(this.handler, new TextMessage("{\"type\":\"connection_init\"}"), new TextMessage(BOOK_QUERY));
    StepVerifier.create(this.session.getOutput()).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.CONNECTION_ACK)).consumeNextWith((message) -> {
        GraphQlMessage actual = decode(message);
        assertThat(actual.getId()).isEqualTo(SUBSCRIPTION_ID);
        assertThat(actual.resolvedType()).isEqualTo(GraphQlMessageType.NEXT);
        assertThat(actual.<Map<String, Object>>getPayload()).extractingByKey("data", as(InstanceOfAssertFactories.map(String.class, Object.class))).extractingByKey("bookById", as(InstanceOfAssertFactories.map(String.class, Object.class))).containsEntry("name", "Nineteen Eighty-Four");
    }).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.COMPLETE)).then(// Complete output Flux
    this.session::close).expectComplete().verify(TIMEOUT);
}
Also used : GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphQlSetup(org.springframework.graphql.GraphQlSetup) CloseStatus(org.springframework.web.socket.CloseStatus) ArrayList(java.util.ArrayList) TextMessage(org.springframework.web.socket.TextMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.as(org.assertj.core.api.Assertions.as) Duration(java.time.Duration) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) WebInterceptor(org.springframework.graphql.web.WebInterceptor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) HttpHeaders(org.springframework.http.HttpHeaders) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) WebSocketHandlerTestSupport(org.springframework.graphql.web.WebSocketHandlerTestSupport) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) HttpInputMessage(org.springframework.http.HttpInputMessage) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Collections(java.util.Collections) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) InputStream(java.io.InputStream) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) Map(java.util.Map) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 3 with GraphQlMessage

use of org.springframework.graphql.web.support.GraphQlMessage in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method assertMessageType.

private void assertMessageType(WebSocketMessage<?> webSocketMessage, GraphQlMessageType messageType) {
    GraphQlMessage message = decode(webSocketMessage);
    assertThat(message.resolvedType()).isEqualTo(messageType);
    if (messageType != GraphQlMessageType.CONNECTION_ACK && messageType != GraphQlMessageType.PONG) {
        assertThat(message.getId()).isEqualTo(SUBSCRIPTION_ID);
    }
}
Also used : GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage)

Example 4 with GraphQlMessage

use of org.springframework.graphql.web.support.GraphQlMessage in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method subscription.

@Test
void subscription() throws Exception {
    handle(this.handler, new TextMessage("{\"type\":\"connection_init\"}"), new TextMessage(BOOK_SUBSCRIPTION));
    BiConsumer<WebSocketMessage<?>, String> bookPayloadAssertion = (message, bookId) -> {
        GraphQlMessage actual = decode(message);
        assertThat(actual.getId()).isEqualTo(SUBSCRIPTION_ID);
        assertThat(actual.resolvedType()).isEqualTo(GraphQlMessageType.NEXT);
        assertThat(actual.<Map<String, Object>>getPayload()).extractingByKey("data", as(InstanceOfAssertFactories.map(String.class, Object.class))).extractingByKey("bookSearch", as(InstanceOfAssertFactories.map(String.class, Object.class))).containsEntry("id", bookId);
    };
    StepVerifier.create(this.session.getOutput()).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.CONNECTION_ACK)).consumeNextWith((message) -> bookPayloadAssertion.accept(message, "1")).consumeNextWith((message) -> bookPayloadAssertion.accept(message, "5")).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.COMPLETE)).then(// Complete output Flux
    this.session::close).expectComplete().verify(TIMEOUT);
}
Also used : GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphQlSetup(org.springframework.graphql.GraphQlSetup) CloseStatus(org.springframework.web.socket.CloseStatus) ArrayList(java.util.ArrayList) TextMessage(org.springframework.web.socket.TextMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.as(org.assertj.core.api.Assertions.as) Duration(java.time.Duration) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) WebInterceptor(org.springframework.graphql.web.WebInterceptor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) HttpHeaders(org.springframework.http.HttpHeaders) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) WebSocketHandlerTestSupport(org.springframework.graphql.web.WebSocketHandlerTestSupport) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) HttpInputMessage(org.springframework.http.HttpInputMessage) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Collections(java.util.Collections) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) InputStream(java.io.InputStream) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Example 5 with GraphQlMessage

use of org.springframework.graphql.web.support.GraphQlMessage in project spring-graphql by spring-projects.

the class GraphQlWebSocketHandlerTests method errorMessagePayloadIsCorrectArray.

@Test
void errorMessagePayloadIsCorrectArray() throws Exception {
    final String GREETING_QUERY = "{" + "\"id\":\"" + SUBSCRIPTION_ID + "\"," + "\"type\":\"subscribe\"," + "\"payload\":{\"query\": \"" + "  subscription TestTypenameSubscription {" + "    greeting" + "  }\"}" + "}";
    String schema = "type Subscription { greeting: String! }type Query { greetingUnused: String! }";
    WebGraphQlHandler initHandler = GraphQlSetup.schemaContent(schema).subscriptionFetcher("greeting", env -> Flux.just("a", null, "b")).webInterceptor().toWebGraphQlHandler();
    GraphQlWebSocketHandler handler = new GraphQlWebSocketHandler(initHandler, converter, Duration.ofSeconds(60));
    handle(handler, new TextMessage("{\"type\":\"connection_init\"}"), new TextMessage(GREETING_QUERY));
    StepVerifier.create(this.session.getOutput()).consumeNextWith((message) -> assertMessageType(message, GraphQlMessageType.CONNECTION_ACK)).consumeNextWith((message) -> {
        GraphQlMessage actual = decode(message);
        assertThat(actual.getId()).isEqualTo(SUBSCRIPTION_ID);
        assertThat(actual.resolvedType()).isEqualTo(GraphQlMessageType.NEXT);
        assertThat(actual.<Map<String, Object>>getPayload()).extractingByKey("data", as(InstanceOfAssertFactories.map(String.class, Object.class))).containsEntry("greeting", "a");
    }).consumeNextWith((message) -> {
        GraphQlMessage actual = decode(message);
        assertThat(actual.getId()).isEqualTo(SUBSCRIPTION_ID);
        assertThat(actual.resolvedType()).isEqualTo(GraphQlMessageType.ERROR);
        assertThat(actual.<List<Map<String, Object>>>getPayload()).asList().hasSize(1).allSatisfy(theError -> assertThat(theError).asInstanceOf(InstanceOfAssertFactories.map(String.class, Object.class)).hasSize(3).hasEntrySatisfying("locations", loc -> assertThat(loc).asList().isEmpty()).hasEntrySatisfying("message", msg -> assertThat(msg).asString().contains("null")).extractingByKey("extensions", as(InstanceOfAssertFactories.map(String.class, Object.class))).containsEntry("classification", "DataFetchingException"));
    }).then(this.session::close).expectComplete().verify(TIMEOUT);
}
Also used : GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) StepVerifier(reactor.test.StepVerifier) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) GraphQlSetup(org.springframework.graphql.GraphQlSetup) CloseStatus(org.springframework.web.socket.CloseStatus) ArrayList(java.util.ArrayList) TextMessage(org.springframework.web.socket.TextMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) Assertions.as(org.assertj.core.api.Assertions.as) Duration(java.time.Duration) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) WebInterceptor(org.springframework.graphql.web.WebInterceptor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) WebSocketMessage(org.springframework.web.socket.WebSocketMessage) HttpHeaders(org.springframework.http.HttpHeaders) ConsumeOneAndNeverCompleteInterceptor(org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) WebSocketHandlerTestSupport(org.springframework.graphql.web.WebSocketHandlerTestSupport) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) GraphQlMessageType(org.springframework.graphql.web.support.GraphQlMessageType) HttpInputMessage(org.springframework.http.HttpInputMessage) MappingJackson2HttpMessageConverter(org.springframework.http.converter.json.MappingJackson2HttpMessageConverter) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Collections(java.util.Collections) WebSocketInterceptor(org.springframework.graphql.web.WebSocketInterceptor) InputStream(java.io.InputStream) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter) GraphQlMessage(org.springframework.graphql.web.support.GraphQlMessage) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) Map(java.util.Map) TextMessage(org.springframework.web.socket.TextMessage) Test(org.junit.jupiter.api.Test)

Aggregations

GraphQlMessage (org.springframework.graphql.web.support.GraphQlMessage)12 Map (java.util.Map)8 Test (org.junit.jupiter.api.Test)8 WebSocketInterceptor (org.springframework.graphql.web.WebSocketInterceptor)8 Mono (reactor.core.publisher.Mono)8 Duration (java.time.Duration)7 ArrayList (java.util.ArrayList)7 Collections (java.util.Collections)7 List (java.util.List)7 ConsumeOneAndNeverCompleteInterceptor (org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor)7 WebGraphQlHandler (org.springframework.graphql.web.WebGraphQlHandler)7 Flux (reactor.core.publisher.Flux)7 GraphQlMessageType (org.springframework.graphql.web.support.GraphQlMessageType)6 CloseStatus (org.springframework.web.socket.CloseStatus)6 TextMessage (org.springframework.web.socket.TextMessage)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 BiConsumer (java.util.function.BiConsumer)5