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);
}
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);
}
Aggregations