Search in sources :

Example 6 with ApolloWSMessageType

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

the class ApolloWSHandlerTest method testSubscriptionWsCallWithConnectionParams.

@Test
public void testSubscriptionWsCallWithConnectionParams() {
    int countInConnectionParams = 2;
    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(DATA, type);
                int val = obj.getJsonObject("payload").getJsonObject("data").getJsonObject("counter").getInteger("count");
                assertEquals(countInConnectionParams, val);
            } else if (current == 2) {
                assertEquals(COMPLETE, type);
                websocket.close();
            } else {
                fail();
            }
        });
        JsonObject messageInit = new JsonObject().put("payload", new JsonObject().put("count", countInConnectionParams)).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) 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