Search in sources :

Example 1 with ApolloWSMessageType

use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.

the class ApolloWSHandlerTest method testQueryWsCall.

private void testQueryWsCall(BiConsumer<WebSocket, JsonObject> sender) {
    client.webSocket("/graphql", onSuccess(websocket -> {
        websocket.exceptionHandler(this::fail);
        websocket.endHandler(v -> testComplete());
        AtomicReference<String> id = new AtomicReference<>();
        AtomicInteger counter = new AtomicInteger();
        websocket.textMessageHandler(text -> {
            JsonObject obj = new JsonObject(text);
            ApolloWSMessageType type = ApolloWSMessageType.from(obj.getString("type"));
            if (type.equals(CONNECTION_KEEP_ALIVE)) {
                return;
            }
            int current = counter.getAndIncrement();
            if (current == 0) {
                assertEquals(CONNECTION_ACK, type);
            } else if (current == 1) {
                assertTrue(id.compareAndSet(null, obj.getString("id")));
                assertEquals(DATA, type);
                int val = obj.getJsonObject("payload").getJsonObject("data").getJsonObject("staticCounter").getInteger("count");
                assertEquals(STATIC_COUNT, val);
            } else if (current == 2) {
                assertEquals(id.get(), obj.getString("id"));
                assertEquals(COMPLETE, type);
                websocket.close();
            } else {
                fail();
            }
        });
        JsonObject messageInit = new JsonObject().put("type", "connection_init").put("id", "1");
        JsonObject message = new JsonObject().put("payload", new JsonObject().put("query", "query Query { staticCounter { count } }")).put("type", "start").put("id", "1");
        websocket.write(messageInit.toBuffer());
        sender.accept(websocket, message);
    }));
    await();
}
Also used : IntStream(java.util.stream.IntStream) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQL(graphql.GraphQL) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) SchemaParser(graphql.schema.idl.SchemaParser) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) GraphQLSchema(graphql.schema.GraphQLSchema) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) WebTestBase(io.vertx.ext.web.WebTestBase) Publisher(org.reactivestreams.Publisher) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Test(org.junit.Test) WebSocket(io.vertx.core.http.WebSocket) NetClientOptions(io.vertx.core.net.NetClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) RuntimeWiring(graphql.schema.idl.RuntimeWiring) NetServer(io.vertx.core.net.NetServer) Subscription(org.reactivestreams.Subscription) SchemaGenerator(graphql.schema.idl.SchemaGenerator) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) Handler(io.vertx.core.Handler) WebSocketFrame(io.vertx.core.http.WebSocketFrame) GET(io.vertx.core.http.HttpMethod.GET) NetSocket(io.vertx.core.net.NetSocket) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) JsonObject(io.vertx.core.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 2 with ApolloWSMessageType

use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.

the class ApolloWSHandlerTest method testSubscriptionWsCallWithDoubleConnectionInit.

@Test
public void testSubscriptionWsCallWithDoubleConnectionInit() {
    client.webSocket("/graphql", onSuccess(websocket -> {
        websocket.exceptionHandler(this::fail);
        websocket.endHandler(v -> testComplete());
        AtomicInteger counter = new AtomicInteger();
        websocket.textMessageHandler(text -> {
            JsonObject obj = new JsonObject(text);
            ApolloWSMessageType type = ApolloWSMessageType.from(obj.getString("type"));
            if (type.equals(CONNECTION_KEEP_ALIVE)) {
                return;
            }
            int current = counter.getAndIncrement();
            if (current == 0) {
                assertEquals(CONNECTION_ACK, type);
            } else if (current == 1) {
                assertEquals(ERROR, type);
                websocket.close();
            } else {
                fail();
            }
        });
        JsonObject messageInit = new JsonObject().put("type", "connection_init").put("id", "1");
        websocket.write(messageInit.toBuffer());
        websocket.write(messageInit.toBuffer());
    }));
    await();
}
Also used : IntStream(java.util.stream.IntStream) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQL(graphql.GraphQL) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) SchemaParser(graphql.schema.idl.SchemaParser) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) GraphQLSchema(graphql.schema.GraphQLSchema) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) WebTestBase(io.vertx.ext.web.WebTestBase) Publisher(org.reactivestreams.Publisher) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Test(org.junit.Test) WebSocket(io.vertx.core.http.WebSocket) NetClientOptions(io.vertx.core.net.NetClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) RuntimeWiring(graphql.schema.idl.RuntimeWiring) NetServer(io.vertx.core.net.NetServer) Subscription(org.reactivestreams.Subscription) SchemaGenerator(graphql.schema.idl.SchemaGenerator) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) Handler(io.vertx.core.Handler) WebSocketFrame(io.vertx.core.http.WebSocketFrame) GET(io.vertx.core.http.HttpMethod.GET) NetSocket(io.vertx.core.net.NetSocket) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 3 with ApolloWSMessageType

use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.

the class ApolloWSHandlerTest method testSubscriptionWsCallWithFailedPromise.

@Test
public void testSubscriptionWsCallWithFailedPromise() {
    String rejectMessage = "test";
    client.webSocket("/graphql", onSuccess(websocket -> {
        websocket.exceptionHandler(this::fail);
        websocket.endHandler(v -> testComplete());
        websocket.textMessageHandler(text -> {
            JsonObject obj = new JsonObject(text);
            ApolloWSMessageType type = ApolloWSMessageType.from(obj.getString("type"));
            assertEquals(CONNECTION_ERROR, type);
            assertEquals(rejectMessage, obj.getString("payload"));
            websocket.close();
        });
        JsonObject messageInit = new JsonObject().put("payload", new JsonObject().put("rejectMessage", rejectMessage)).put("type", "connection_init").put("id", "1");
        websocket.write(messageInit.toBuffer());
    }));
    await();
}
Also used : IntStream(java.util.stream.IntStream) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQL(graphql.GraphQL) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) SchemaParser(graphql.schema.idl.SchemaParser) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) GraphQLSchema(graphql.schema.GraphQLSchema) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) WebTestBase(io.vertx.ext.web.WebTestBase) Publisher(org.reactivestreams.Publisher) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Test(org.junit.Test) WebSocket(io.vertx.core.http.WebSocket) NetClientOptions(io.vertx.core.net.NetClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) RuntimeWiring(graphql.schema.idl.RuntimeWiring) NetServer(io.vertx.core.net.NetServer) Subscription(org.reactivestreams.Subscription) SchemaGenerator(graphql.schema.idl.SchemaGenerator) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) Handler(io.vertx.core.Handler) WebSocketFrame(io.vertx.core.http.WebSocketFrame) GET(io.vertx.core.http.HttpMethod.GET) NetSocket(io.vertx.core.net.NetSocket) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Example 4 with ApolloWSMessageType

use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.

the class ApolloWSConnectionHandler method handleMessage.

private void handleMessage(JsonObject jsonObject) {
    String opId = jsonObject.getString("id");
    ApolloWSMessageType type = from(jsonObject.getString("type"));
    if (type == null) {
        sendMessage(opId, ERROR, "Unknown message type: " + jsonObject.getString("type"));
        return;
    }
    ApolloWSMessageImpl message = new ApolloWSMessageImpl(serverWebSocket, type, jsonObject);
    Handler<ApolloWSMessage> mh = apolloWSHandler.getMessageHandler();
    if (mh != null) {
        mh.handle(message);
    }
    Handler<ApolloWSConnectionInitEvent> connectionInitHandler = apolloWSHandler.getConnectionInitHandler();
    switch(type) {
        case CONNECTION_INIT:
            if (!connectionInitialized.compareAndSet(false, true)) {
                sendMessage(opId, ERROR, "CONNECTION_INIT can only be sent once").onComplete(v -> serverWebSocket.close(WS_INTERNAL_ERROR));
                break;
            }
            if (connectionInitHandler != null) {
                connectionInitHandler.handle(new ApolloWSConnectionInitEvent() {

                    @Override
                    public ApolloWSMessage message() {
                        return message;
                    }

                    @Override
                    public boolean tryComplete(Object o) {
                        return connectionPromise.tryComplete(o);
                    }

                    @Override
                    public boolean tryFail(Throwable throwable) {
                        return connectionPromise.tryFail(throwable);
                    }

                    @Override
                    public Future<Object> future() {
                        return connectionPromise.future();
                    }
                });
            } else {
                connectionPromise.complete();
            }
            connectionPromise.future().onComplete(ar -> {
                if (ar.succeeded()) {
                    connect();
                } else {
                    sendMessage(opId, CONNECTION_ERROR, ar.cause().getMessage()).onComplete(v -> serverWebSocket.close(WS_INTERNAL_ERROR));
                }
            });
            break;
        case CONNECTION_TERMINATE:
            serverWebSocket.close();
            break;
        case START:
            if (!connectionInitialized.get()) {
                sendMessage(opId, ERROR, "CONNECTION_INIT has to be sent before START").onComplete(v -> serverWebSocket.close(WS_INTERNAL_ERROR));
                break;
            }
            connectionPromise.future().onComplete(ar -> {
                if (ar.succeeded()) {
                    ApolloWSMessage messageWithParams = new ApolloWSMessageImpl(serverWebSocket, type, jsonObject, ar.result());
                    start(messageWithParams);
                } else {
                    sendMessage(opId, ERROR, ar.cause().getMessage());
                    stop(opId);
                }
            });
            break;
        case STOP:
            stop(opId);
            break;
        default:
            sendMessage(opId, ERROR, "Unsupported message type: " + type);
            break;
    }
}
Also used : ApolloWSConnectionInitEvent(io.vertx.ext.web.handler.graphql.ApolloWSConnectionInitEvent) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) ApolloWSMessage(io.vertx.ext.web.handler.graphql.ApolloWSMessage) Future(io.vertx.core.Future) JsonObject(io.vertx.core.json.JsonObject) ErrorUtil.toJsonObject(io.vertx.ext.web.handler.graphql.impl.ErrorUtil.toJsonObject)

Example 5 with ApolloWSMessageType

use of io.vertx.ext.web.handler.graphql.ApolloWSMessageType in project vertx-web by vert-x3.

the class ApolloWSHandlerTest method testSubscriptionWsCall.

@Test
public void testSubscriptionWsCall() {
    client.webSocket("/graphql", onSuccess(websocket -> {
        websocket.exceptionHandler(this::fail);
        websocket.endHandler(v -> testComplete());
        AtomicReference<String> id = new AtomicReference<>();
        AtomicInteger counter = new AtomicInteger();
        websocket.textMessageHandler(text -> {
            JsonObject obj = new JsonObject(text);
            ApolloWSMessageType type = ApolloWSMessageType.from(obj.getString("type"));
            if (type.equals(CONNECTION_KEEP_ALIVE)) {
                return;
            }
            int current = counter.getAndIncrement();
            if (current == 0) {
                assertEquals(CONNECTION_ACK, type);
            } else if (current >= 1 && current <= MAX_COUNT) {
                if (current == 1) {
                    assertTrue(id.compareAndSet(null, obj.getString("id")));
                } else {
                    assertEquals(id.get(), obj.getString("id"));
                }
                assertEquals(DATA, ApolloWSMessageType.from(obj.getString("type")));
                int val = obj.getJsonObject("payload").getJsonObject("data").getJsonObject("counter").getInteger("count");
                assertEquals(current - 1, val);
            } else if (current == MAX_COUNT + 1) {
                assertEquals(id.get(), obj.getString("id"));
                assertEquals(COMPLETE, ApolloWSMessageType.from(obj.getString("type")));
                websocket.close();
            } else {
                fail();
            }
        });
        JsonObject messageInit = new JsonObject().put("type", "connection_init").put("id", "1");
        JsonObject message = new JsonObject().put("payload", new JsonObject().put("query", "subscription Subscription { counter { count } }")).put("type", "start").put("id", "1");
        websocket.write(messageInit.toBuffer());
        websocket.write(message.toBuffer());
    }));
    await();
}
Also used : IntStream(java.util.stream.IntStream) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) GraphQL(graphql.GraphQL) RuntimeWiring.newRuntimeWiring(graphql.schema.idl.RuntimeWiring.newRuntimeWiring) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) SchemaParser(graphql.schema.idl.SchemaParser) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) GraphQLSchema(graphql.schema.GraphQLSchema) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) HttpClientOptions(io.vertx.core.http.HttpClientOptions) WebTestBase(io.vertx.ext.web.WebTestBase) Publisher(org.reactivestreams.Publisher) TypeDefinitionRegistry(graphql.schema.idl.TypeDefinitionRegistry) Test(org.junit.Test) WebSocket(io.vertx.core.http.WebSocket) NetClientOptions(io.vertx.core.net.NetClientOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Buffer(io.vertx.core.buffer.Buffer) RuntimeWiring(graphql.schema.idl.RuntimeWiring) NetServer(io.vertx.core.net.NetServer) Subscription(org.reactivestreams.Subscription) SchemaGenerator(graphql.schema.idl.SchemaGenerator) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) Handler(io.vertx.core.Handler) WebSocketFrame(io.vertx.core.http.WebSocketFrame) GET(io.vertx.core.http.HttpMethod.GET) NetSocket(io.vertx.core.net.NetSocket) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) JsonObject(io.vertx.core.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)6 ApolloWSMessageType (io.vertx.ext.web.handler.graphql.ApolloWSMessageType)6 GraphQL (graphql.GraphQL)5 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)5 GraphQLSchema (graphql.schema.GraphQLSchema)5 RuntimeWiring (graphql.schema.idl.RuntimeWiring)5 RuntimeWiring.newRuntimeWiring (graphql.schema.idl.RuntimeWiring.newRuntimeWiring)5 SchemaGenerator (graphql.schema.idl.SchemaGenerator)5 SchemaParser (graphql.schema.idl.SchemaParser)5 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)5 AsyncResult (io.vertx.core.AsyncResult)5 Handler (io.vertx.core.Handler)5 Buffer (io.vertx.core.buffer.Buffer)5 HttpClientOptions (io.vertx.core.http.HttpClientOptions)5 GET (io.vertx.core.http.HttpMethod.GET)5 WebSocket (io.vertx.core.http.WebSocket)5 WebSocketFrame (io.vertx.core.http.WebSocketFrame)5 NetClientOptions (io.vertx.core.net.NetClientOptions)5 NetServer (io.vertx.core.net.NetServer)5 NetSocket (io.vertx.core.net.NetSocket)5