Search in sources :

Example 1 with WebInterceptor

use of org.springframework.graphql.web.WebInterceptor 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 WebInterceptor

use of org.springframework.graphql.web.WebInterceptor 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

Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 BiConsumer (java.util.function.BiConsumer)2 Assertions.as (org.assertj.core.api.Assertions.as)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 InstanceOfAssertFactories (org.assertj.core.api.InstanceOfAssertFactories)2 Test (org.junit.jupiter.api.Test)2 GraphQlSetup (org.springframework.graphql.GraphQlSetup)2 ConsumeOneAndNeverCompleteInterceptor (org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor)2 WebGraphQlHandler (org.springframework.graphql.web.WebGraphQlHandler)2 WebInterceptor (org.springframework.graphql.web.WebInterceptor)2 WebSocketHandlerTestSupport (org.springframework.graphql.web.WebSocketHandlerTestSupport)2 WebSocketInterceptor (org.springframework.graphql.web.WebSocketInterceptor)2 GraphQlMessage (org.springframework.graphql.web.support.GraphQlMessage)2 GraphQlMessageType (org.springframework.graphql.web.support.GraphQlMessageType)2 Flux (reactor.core.publisher.Flux)2