Search in sources :

Example 1 with Complete

use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete in project elide by yahoo.

the class RequestHandler method safeSendComplete.

protected void safeSendComplete() {
    log.debug("Sending Complete");
    ObjectMapper mapper = elide.getElideSettings().getMapper().getObjectMapper();
    Complete complete = Complete.builder().id(protocolID).build();
    try {
        sendMessage(mapper.writeValueAsString(complete));
    } catch (JsonProcessingException e) {
        log.error("UNEXPECTED Json Serialization Error {}", e.getMessage());
        safeClose();
    }
}
Also used : Complete(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with Complete

use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete 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);
    }
}
Also used : Complete(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete) JsonNode(com.fasterxml.jackson.databind.JsonNode) Subscribe(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) MessageType(com.yahoo.elide.graphql.subscriptions.websocket.protocol.MessageType)

Example 3 with Complete

use of com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete in project elide by yahoo.

the class SubscriptionWebSocketTest method testSubscribeUnsubscribeSubscribe.

@Test
void testSubscribeUnsubscribeSubscribe() 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));
    Complete complete = Complete.builder().id("1").build();
    endpoint.onMessage(session, mapper.writeValueAsString(complete));
    endpoint.onMessage(session, mapper.writeValueAsString(subscribe));
    List<String> expected = List.of("{\"type\":\"connection_ack\"}", "{\"type\":\"complete\",\"id\":\"1\"}", "{\"type\":\"complete\",\"id\":\"1\"}");
    ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
    verify(remote, times(3)).sendText(message.capture());
    assertEquals(expected, message.getAllValues());
}
Also used : SubscriptionWebSocket(com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket) Complete(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete) ConnectionInit(com.yahoo.elide.graphql.subscriptions.websocket.protocol.ConnectionInit) Subscribe(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Aggregations

Complete (com.yahoo.elide.graphql.subscriptions.websocket.protocol.Complete)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Subscribe (com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GraphQLTest (com.yahoo.elide.graphql.GraphQLTest)1 SubscriptionWebSocket (com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket)1 ConnectionInit (com.yahoo.elide.graphql.subscriptions.websocket.protocol.ConnectionInit)1 MessageType (com.yahoo.elide.graphql.subscriptions.websocket.protocol.MessageType)1 Test (org.junit.jupiter.api.Test)1