diff --git a/extensions/vertx-graphql/deployment/src/main/java/io/quarkus/vertx/graphql/deployment/VertxGraphqlProcessor.java b/extensions/vertx-graphql/deployment/src/main/java/io/quarkus/vertx/graphql/deployment/VertxGraphqlProcessor.java index 19cf2734691da..894c352ef7b33 100644 --- a/extensions/vertx-graphql/deployment/src/main/java/io/quarkus/vertx/graphql/deployment/VertxGraphqlProcessor.java +++ b/extensions/vertx-graphql/deployment/src/main/java/io/quarkus/vertx/graphql/deployment/VertxGraphqlProcessor.java @@ -22,7 +22,6 @@ import io.vertx.core.Handler; import io.vertx.ext.web.RoutingContext; import io.vertx.ext.web.handler.graphql.impl.GraphQLBatch; -// import io.vertx.ext.web.handler.graphql.impl.GraphQLInputDeserializer; import io.vertx.ext.web.handler.graphql.impl.GraphQLQuery; class VertxGraphqlProcessor { @@ -34,14 +33,18 @@ FeatureBuildItem feature() { } @BuildStep - WebsocketSubProtocolsBuildItem websocketSubProtocols() { + WebsocketSubProtocolsBuildItem graphQLWSProtocol() { + return new WebsocketSubProtocolsBuildItem("graphql-transport-ws"); + } + + @BuildStep + WebsocketSubProtocolsBuildItem appoloWSProtocol() { return new WebsocketSubProtocolsBuildItem("graphql-ws"); } @BuildStep List registerForReflection() { return Arrays.asList( - //new ReflectiveClassBuildItem(true, true, GraphQLInputDeserializer.class.getName()), ReflectiveClassBuildItem.builder(GraphQLBatch.class.getName()).methods().fields().build(), ReflectiveClassBuildItem.builder(GraphQLQuery.class.getName()).methods().fields().build()); } diff --git a/integration-tests/vertx-graphql/src/main/java/io/quarkus/vertx/graphql/it/VertxGraphqlResource.java b/integration-tests/vertx-graphql/src/main/java/io/quarkus/vertx/graphql/it/VertxGraphqlResource.java index c6ad21e33b550..528f4e310a61e 100644 --- a/integration-tests/vertx-graphql/src/main/java/io/quarkus/vertx/graphql/it/VertxGraphqlResource.java +++ b/integration-tests/vertx-graphql/src/main/java/io/quarkus/vertx/graphql/it/VertxGraphqlResource.java @@ -28,8 +28,8 @@ import graphql.schema.idl.TypeDefinitionRegistry; import io.vertx.ext.web.Router; import io.vertx.ext.web.handler.BodyHandler; -import io.vertx.ext.web.handler.graphql.ApolloWSHandler; import io.vertx.ext.web.handler.graphql.GraphQLHandler; +import io.vertx.ext.web.handler.graphql.ws.GraphQLWSHandler; @ApplicationScoped public class VertxGraphqlResource { @@ -50,8 +50,9 @@ public void setupRouter(@Observes Router router) { GraphQL graphQL = GraphQL.newGraphQL(graphQLSchema).build(); router.post().handler(BodyHandler.create()); - router.route("/graphql").handler(ApolloWSHandler.create(graphQL)); - router.route("/graphql").handler(GraphQLHandler.create(graphQL)); + router.route("/graphql") + .handler(GraphQLWSHandler.create(graphQL)) + .handler(GraphQLHandler.create(graphQL)); } } diff --git a/integration-tests/vertx-graphql/src/test/java/io/quarkus/vertx/graphql/it/VertxGraphqlTest.java b/integration-tests/vertx-graphql/src/test/java/io/quarkus/vertx/graphql/it/VertxGraphqlTest.java index eb594c3feda26..165e7b50f56dd 100644 --- a/integration-tests/vertx-graphql/src/test/java/io/quarkus/vertx/graphql/it/VertxGraphqlTest.java +++ b/integration-tests/vertx-graphql/src/test/java/io/quarkus/vertx/graphql/it/VertxGraphqlTest.java @@ -1,6 +1,10 @@ package io.quarkus.vertx.graphql.it; import static io.restassured.RestAssured.given; +import static io.vertx.ext.web.handler.graphql.ws.MessageType.COMPLETE; +import static io.vertx.ext.web.handler.graphql.ws.MessageType.CONNECTION_ACK; +import static io.vertx.ext.web.handler.graphql.ws.MessageType.CONNECTION_INIT; +import static io.vertx.ext.web.handler.graphql.ws.MessageType.NEXT; import static java.lang.String.format; import static org.hamcrest.CoreMatchers.is; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -24,7 +28,7 @@ import io.vertx.core.http.WebSocket; import io.vertx.core.http.WebSocketConnectOptions; import io.vertx.core.json.JsonObject; -import io.vertx.ext.web.handler.graphql.ApolloWSMessageType; +import io.vertx.ext.web.handler.graphql.ws.MessageType; @QuarkusTest class VertxGraphqlTest { @@ -57,9 +61,9 @@ public void testGraphQlQuery() { public void testWebSocketSubProtocol() throws Exception { HttpClient httpClient = vertx.createHttpClient(); WebSocketConnectOptions options = new WebSocketConnectOptions().setPort(getPortFromConfig()) - .addSubProtocol("graphql-ws").setURI("/graphql"); - JsonObject init = new JsonObject().put("type", ApolloWSMessageType.CONNECTION_INIT.getText()); - String graphql = "{\"id\" : \"2\", \"type\" : \"start\", \"payload\" : { \"query\" : \"{ hello }\" } }"; + .addSubProtocol("graphql-transport-ws").setURI("/graphql"); + JsonObject init = new JsonObject().put("type", CONNECTION_INIT.getText()); + String graphql = "{\"id\" : \"2\", \"type\" : \"subscribe\", \"payload\" : { \"query\" : \"{ hello }\" } }"; CompletableFuture wsFuture = new CompletableFuture<>(); wsFuture.whenComplete((r, t) -> httpClient.close()); @@ -73,25 +77,31 @@ public void testWebSocketSubProtocol() throws Exception { */ httpClient.webSocket(options, ws -> { - AtomicReference lastReceivedType = new AtomicReference<>(); + AtomicReference lastReceivedType = new AtomicReference<>(); + AtomicReference result = new AtomicReference<>(); if (ws.succeeded()) { WebSocket webSocket = ws.result(); webSocket.handler(message -> { - if (lastReceivedType.compareAndSet(null, ApolloWSMessageType.CONNECTION_ACK.getText())) { - // Go ack, wait for the next message (ka) - } else if (lastReceivedType.compareAndSet("connection_ack", - ApolloWSMessageType.CONNECTION_KEEP_ALIVE.getText())) { - webSocket.write(Buffer.buffer(graphql)); - } else { - JsonObject json = message.toJsonObject(); - String type = json.getString("type"); - if (ApolloWSMessageType.DATA.getText().equals(type)) { - wsFuture.complete(message.toJsonObject()); - } else { - wsFuture.completeExceptionally(new RuntimeException( - format("Unexpected message type: %s\nMessage: %s", type, message.toString()))); + JsonObject json = message.toJsonObject(); + MessageType messageType = MessageType.from(json.getString("type")); + if (messageType == CONNECTION_ACK) { + if (lastReceivedType.compareAndSet(null, CONNECTION_ACK)) { + webSocket.write(Buffer.buffer(graphql)); + return; + } + } else if (messageType == NEXT) { + if (lastReceivedType.compareAndSet(CONNECTION_ACK, NEXT)) { + result.set(json); + return; + } + } else if (messageType == COMPLETE) { + if (lastReceivedType.compareAndSet(NEXT, COMPLETE)) { + wsFuture.complete(result.get()); + return; } } + wsFuture.completeExceptionally(new RuntimeException( + format("Unexpected message type: %s\nMessage: %s", messageType.getText(), message))); }); webSocket.write(init.toBuffer()); } else { @@ -102,7 +112,7 @@ public void testWebSocketSubProtocol() throws Exception { JsonObject json = wsFuture.get(1, TimeUnit.MINUTES); assertNotNull(json); assertEquals("2", json.getString("id")); - assertEquals("data", json.getString("type")); + assertEquals(NEXT.getText(), json.getString("type")); assertEquals("world", json.getJsonObject("payload").getJsonObject("data").getString("hello")); }