use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe in project elide by yahoo.
the class SubscriptionWebSocketTestClient method onMessage.
@OnMessage
public void onMessage(String text) throws Exception {
JsonNode type = mapper.readTree(text).get("type");
MessageType messageType = MessageType.valueOf(type.textValue().toUpperCase(Locale.ROOT));
switch(messageType) {
case CONNECTION_ACK:
{
Integer id = 1;
for (String query : queries) {
Subscribe subscribe = Subscribe.builder().id(id.toString()).payload(Subscribe.Payload.builder().query(query).build()).build();
session.getBasicRemote().sendText(mapper.writeValueAsString(subscribe));
id++;
}
break;
}
case NEXT:
{
Next next = mapper.readValue(text, Next.class);
results.add(next.getPayload());
expectedNumberOfMessages--;
if (expectedNumberOfMessages <= 0) {
sessionLatch.countDown();
}
break;
}
case PING:
{
expectedNumberOfSubscribes--;
if (expectedNumberOfSubscribes <= 0) {
subscribeLatch.countDown();
}
break;
}
case ERROR:
{
Error error = mapper.readValue(text, Error.class);
log.error("ERROR: {}", error.getPayload());
sessionLatch.countDown();
break;
}
default:
{
break;
}
}
}
use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe in project elide by yahoo.
the class SubscriptionWebSocketTest method testErrorInStream.
@Test
void testErrorInStream() throws IOException {
SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
Book book1 = new Book();
book1.setTitle("Book 1");
book1.setId(1);
Book book2 = new Book();
book2.setTitle("Book 2");
book2.setId(2);
reset(dataStoreTransaction);
when(dataStoreTransaction.getAttribute(any(), any(), any())).thenThrow(new BadRequestException("Bad Request"));
when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(book1, book2)).build());
ConnectionInit init = new ConnectionInit();
endpoint.onOpen(session);
endpoint.onMessage(session, mapper.writeValueAsString(init));
Subscribe subscribe = Subscribe.builder().id("1").payload(Subscribe.Payload.builder().query("subscription {book(topic: ADDED) {id title}}").build()).build();
endpoint.onMessage(session, mapper.writeValueAsString(subscribe));
List<String> expected = List.of("{\"type\":\"connection_ack\"}", "{\"type\":\"next\",\"id\":\"1\",\"payload\":{\"data\":{\"book\":{\"id\":\"1\",\"title\":null}},\"errors\":[{\"message\":\"Exception while fetching data (/book/title) : Bad Request\",\"locations\":[{\"line\":1,\"column\":38}],\"path\":[\"book\",\"title\"],\"extensions\":{\"classification\":\"DataFetchingException\"}}]}}", "{\"type\":\"next\",\"id\":\"1\",\"payload\":{\"data\":{\"book\":{\"id\":\"2\",\"title\":null}},\"errors\":[{\"message\":\"Exception while fetching data (/book/title) : Bad Request\",\"locations\":[{\"line\":1,\"column\":38}],\"path\":[\"book\",\"title\"],\"extensions\":{\"classification\":\"DataFetchingException\"}}]}}", "{\"type\":\"complete\",\"id\":\"1\"}");
ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
verify(remote, times(4)).sendText(message.capture());
assertEquals(expected, message.getAllValues());
}
use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe in project elide by yahoo.
the class SubscriptionWebSocketTest method testActualComplete.
@Test
void testActualComplete() throws IOException {
SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
ConnectionInit init = new ConnectionInit();
endpoint.onOpen(session);
endpoint.onMessage(session, mapper.writeValueAsString(init));
Subscribe subscribe = Subscribe.builder().id("1").payload(Subscribe.Payload.builder().query("subscription {book(topic: ADDED) {id title}}").build()).build();
endpoint.onMessage(session, mapper.writeValueAsString(subscribe));
String complete = "{\"id\":\"5d585eff-ed05-48c2-8af7-ad662930ba74\",\"type\":\"complete\"}";
endpoint.onMessage(session, complete);
}
use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe in project elide by yahoo.
the class SessionHandler method handleRequest.
/**
* Handles an incoming graphql-ws protocol message.
* @param message The protocol message.
*/
public void handleRequest(String message) {
log.debug("Received Message: {} {}", wrappedSession.getId(), message);
try {
JsonNode type = mapper.readTree(message).get("type");
if (type == null) {
safeClose(INVALID_MESSAGE);
return;
}
MessageType messageType;
try {
messageType = MessageType.valueOf(type.textValue().toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
safeClose(INVALID_MESSAGE);
return;
}
switch(messageType) {
case PING:
{
handlePing();
return;
}
case PONG:
{
// Ignore
return;
}
case CONNECTION_INIT:
{
handleConnectionInit();
return;
}
case COMPLETE:
{
Complete complete = mapper.readValue(message, Complete.class);
handleComplete(complete);
return;
}
case SUBSCRIBE:
{
Subscribe subscribe = mapper.readValue(message, Subscribe.class);
handleSubscribe(subscribe);
return;
}
default:
{
safeClose(INVALID_MESSAGE);
return;
}
}
} catch (JsonProcessingException e) {
safeClose(INVALID_MESSAGE);
}
}
use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe in project elide by yahoo.
the class SubscriptionWebSocketTest method testSchemaQuery.
@Test
void testSchemaQuery() throws IOException {
SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
String graphQLRequest = "{" + "__schema {" + "types {" + " name" + "}" + "}" + "__type(name: \"Author\") {" + " name" + " fields {" + " name" + " type { name }" + " }" + "}" + "}";
ConnectionInit init = new ConnectionInit();
endpoint.onOpen(session);
endpoint.onMessage(session, mapper.writeValueAsString(init));
Subscribe subscribe = Subscribe.builder().id("1").payload(Subscribe.Payload.builder().query(graphQLRequest).build()).build();
endpoint.onMessage(session, mapper.writeValueAsString(subscribe));
List<String> expected = List.of("{\"type\":\"connection_ack\"}", "{\"type\":\"next\",\"id\":\"1\",\"payload\":{\"data\":{\"__schema\":{\"types\":[{\"name\":\"Author\"},{\"name\":\"AuthorTopic\"},{\"name\":\"AuthorType\"},{\"name\":\"Book\"},{\"name\":\"BookTopic\"},{\"name\":\"Boolean\"},{\"name\":\"DeferredID\"},{\"name\":\"String\"},{\"name\":\"Subscription\"},{\"name\":\"__Directive\"},{\"name\":\"__DirectiveLocation\"},{\"name\":\"__EnumValue\"},{\"name\":\"__Field\"},{\"name\":\"__InputValue\"},{\"name\":\"__Schema\"},{\"name\":\"__Type\"},{\"name\":\"__TypeKind\"},{\"name\":\"address\"}]},\"__type\":{\"name\":\"Author\",\"fields\":[{\"name\":\"id\",\"type\":{\"name\":\"DeferredID\"}},{\"name\":\"homeAddress\",\"type\":{\"name\":\"address\"}},{\"name\":\"name\",\"type\":{\"name\":\"String\"}},{\"name\":\"type\",\"type\":{\"name\":\"AuthorType\"}}]}}}}", "{\"type\":\"complete\",\"id\":\"1\"}");
ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
verify(remote, times(3)).sendText(message.capture());
assertEquals(expected, message.getAllValues());
}
Aggregations