Skip to content

Commit

Permalink
Use VerticleBase in graphql examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 6, 2024
1 parent bb31186 commit 695b787
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.vertx.example.web.graphql;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
Expand All @@ -10,33 +12,30 @@
import static io.vertx.core.http.HttpResponseExpectation.JSON;
import static io.vertx.core.http.HttpResponseExpectation.SC_OK;

public class Client extends AbstractVerticle {
public class Client extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Client.class.getName()});
}

private WebClient webClient;

@Override
public void start() {
WebClient webClient = WebClient.create(vertx, new WebClientOptions().setDefaultPort(8080));
public Future<?> start() {

webClient = WebClient.create(vertx, new WebClientOptions().setDefaultPort(8080));

JsonObject request = new JsonObject()
.put("query", "query($secure: Boolean) { allLinks(secureOnly: $secure) { url, postedBy { name } } }")
.put("variables", new JsonObject().put("secure", true));

webClient.post("/graphql")
return webClient
.post("/graphql")
.as(BodyCodec.jsonObject())
.sendJsonObject(request)
.expecting(SC_OK.and(JSON))
.onComplete(ar -> {

if (ar.succeeded()) {
JsonObject response = ar.result().body();
System.out.println("response = " + response.encodePrettily());
} else {
ar.cause().printStackTrace();
}

.onSuccess(response -> {
System.out.println("response = " + response.body().encodePrettily());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import graphql.schema.idl.SchemaGenerator;
import graphql.schema.idl.SchemaParser;
import graphql.schema.idl.TypeDefinitionRegistry;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
import io.vertx.ext.web.handler.graphql.GraphQLHandler;
Expand All @@ -21,7 +21,7 @@
import static graphql.schema.idl.RuntimeWiring.newRuntimeWiring;
import static java.util.stream.Collectors.toList;

public class Server extends AbstractVerticle {
public class Server extends VerticleBase {

public static void main(String[] args) {
VertxApplication.main(new String[]{Server.class.getName()});
Expand All @@ -30,14 +30,14 @@ public static void main(String[] args) {
private List<Link> links;

@Override
public void start() {
public Future<?> start() {
prepareData();

Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.route("/graphql").handler(GraphQLHandler.create(createGraphQL()));

vertx.createHttpServer()
return vertx.createHttpServer()
.requestHandler(router)
.listen(8080);
}
Expand Down

0 comments on commit 695b787

Please sign in to comment.