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