CCCPaste

86299f1c06a1a

import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.client.HttpResponse;
import io.vertx.ext.web.client.WebClient;

public class Sandbox {
    final private static int PORT_TDG = 8181;
    final private static int PORT_MY = 8080;
    final private static String HOST = "localhost";
    final private static String TOKEN = "9f226286-e0d8-4125-ad43-b123b4e464da";
    final private static String TYPE = "application/json;charset=utf-8";

    public static void main(String[] args) {
        Vertx vertx = Vertx.vertx();
        WebClient client = WebClient.create(vertx);
        Router router = Router.router(vertx);
        Sandbox sandbox = new Sandbox();
        HttpServer server = vertx.createHttpServer();
        JsonObject request = new JsonObject().put("query", "{Book { uuid, book_name, author }}");

        router
                //.get("/get")
                .post("/post")

                .respond(
                        ctx -> sandbox.graphql(request, client)
                                .onSuccess(httpResp -> {
                                    if (httpResp.statusCode() == 200) {
                                        ctx.end(httpResp.bodyAsBuffer());
                                    }
                                })
                        );

        server.requestHandler(router).listen(8080);
    }
    
    public Future> graphql(JsonObject request, WebClient client) {
        Promise> promise = Promise.promise();
        client
                .post(PORT_TDG, HOST, "/graphql")
                .putHeader("Content-Type", "application/json")
                .sendJson(request, t -> {
                    if (t.succeeded()) {
                        promise.complete(t.result());
                    }
                });
        return promise.future();
    }
}