Vert.x Web GraphQL

Vert.x Web GraphQL extends Vert.x Web with the GraphQL-Java library so that you can build a GraphQL server.

This is the reference documentation for Vert.x Web GraphQL. It is highly recommended to get familiar with the GraphQL-Java API first. You may start by reading the GraphQL-Java documentation.
This module has Tech Preview status, this means the API can change between versions.

Getting started

To use this module, add the following to the dependencies section of your Maven POM file:


Or, if you use Gradle:

compile 'io.vertx:vertx-web-graphql:3.9.16'

Handler setup

Create a Vert.x Web Route and a GraphQLHandler for it:

GraphQL graphQL = setupGraphQLJava();


The handler serves both GET and POST requests. However you can restrict the service to one type of HTTP method:

GraphQL graphQL = setupGraphQLJava();"/graphql").handler(GraphQLHandler.create(graphQL));
The GraphQLHandler does not require a BodyHandler to read POST requests content.

GraphiQL client

As you are building your application, testing your GraphQL queries in GraphiQL can be handy.

To do so, create a route for GraphiQL resources and a GraphiQLHandler for them:

GraphiQLHandlerOptions options = new GraphiQLHandlerOptions()

The GraphiQL user interface is disabled by default for security reasons. This is why you must configure the GraphiQLHandlerOptions to enable it.
GraphiQL is enabled automatically when Vert.x Web runs in development mode. To switch the development mode on, use the VERTXWEB_ENVIRONMENT environment variable or vertxweb.environment system property and set it to dev. In this case, create the GraphiQLHandler without changing the enabled property.

If your application is protected by authentication, you can customize the headers sent by GraphiQL dynamically:

graphiQLHandler.graphiQLRequestHeaders(rc -> {
  String token = rc.get("token");
  return MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.AUTHORIZATION, "Bearer " + token);


Please refer to the GraphiQLHandlerOptions documentation for further details.

Enable query batching

Query batching consists in posting an array instead of a single object to the GraphQL endpoint.

Vert.x Web GraphQL can handle such requests but by default the feature is disabled. To enable it, create the GraphQLHandler with options:

GraphQLHandlerOptions options = new GraphQLHandlerOptions()

GraphQLHandler handler = GraphQLHandler.create(graphQL, options);

Building a GraphQL server

The GraphQL-Java API is very well suited for the asynchronous world: the asynchronous execution strategy is the default for queries (serial asynchronous for mutations).

To avoid blocking the event loop, all you have to do is implement data fetchers that return a CompletionStage instead of the result directly.

DataFetcher<CompletionStage<List<Link>>> dataFetcher = environment -> {

  CompletableFuture<List<Link>> completableFuture = new CompletableFuture<>();

  retrieveLinksFromBackend(environment, ar -> {
    if (ar.succeeded()) {
    } else {

  return completableFuture;

RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
  .type("Query", builder -> builder.dataFetcher("allLinks", dataFetcher))

Fetching data with callback-based APIs

Implementing a data fetcher that returns a CompletionStage is not a complex task. But when you work with Vert.x callback-based APIs, it requires a bit of boilerplate.

This is where the VertxDataFetcher can help:

VertxDataFetcher<List<Link>> dataFetcher = new VertxDataFetcher<>((environment, future) -> {

  retrieveLinksFromBackend(environment, future);


RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
  .type("Query", builder -> builder.dataFetcher("allLinks", dataFetcher))

Providing data fetchers with some context

Very often, the GraphQLHandler will be declared after other route handlers. For example, you could protect your application with authentication.

In this case, it is likely that your data fetchers will need to know which user is logged-in to narrow down the results. Let’s say your authentication layer stores a User object in the RoutingContext.

You may retrieve this object by inspecting the DataFetchingEnvironment:

VertxDataFetcher<List<Link>> dataFetcher = new VertxDataFetcher<>((environment, future) -> {

  RoutingContext routingContext = environment.getContext();

  User user = routingContext.get("user");

  retrieveLinksPostedBy(user, future);

The routing context is available with any kind of data fetchers, not just VertxDataFetcher.

If you prefer not to expose the routing context to your data fetchers, configure the GraphQL handler to customize the context object:

VertxDataFetcher<List<Link>> dataFetcher = new VertxDataFetcher<>((environment, future) -> {

  // User as custom context object
  User user = environment.getContext();

  retrieveLinksPostedBy(user, future);


GraphQL graphQL = setupGraphQLJava(dataFetcher);

// Customize the query context object when setting up the handler
GraphQLHandler handler = GraphQLHandler.create(graphQL).queryContext(routingContext -> {

  return routingContext.get("user");



JSON data results

The default GraphQL data fetcher is PropertyDataFetcher. As a consequence, it will be able to read the fields of your domain objects without further configuration.

Nevertheless, some Vert.x data clients return JsonArray and JsonObject results.

If you don’t need (or don’t wish to) use a domain object layer, you can configure GraphQL-Java to use VertxPropertyDataFetcher instead:

RuntimeWiring.Builder builder = RuntimeWiring.newRuntimeWiring();

builder.wiringFactory(new WiringFactory() {

  public DataFetcher getDefaultDataFetcher(FieldWiringEnvironment environment) {

    return new VertxPropertyDataFetcher(environment.getFieldDefinition().getName());

VertxPropertyDataFetcher wraps a PropertyDataFetcher so you can still use it with domain objects.

Using dataloaders

Dataloaders help you to load data efficiently by batching fetch requests and caching results.

First, create a batch loader:

BatchLoaderWithContext<String, Link> linksBatchLoader = (keys, environment) -> {

  return retrieveLinksFromBackend(keys, environment);

If you work with Vert.x callback-based APIs, you may use a VertxBatchLoader or a VertxMappedBatchLoader to simplify your code.

Then, configure the GraphQLHandler to create a DataLoaderRegistry for each request:

GraphQLHandler handler = GraphQLHandler.create(graphQL).dataLoaderRegistry(rc -> {

  DataLoader<String, Link> linkDataLoader = DataLoader.newDataLoader(linksBatchLoader);

  return new DataLoaderRegistry().register("link", linkDataLoader);


You can use an Apollo WebSocketLink which connects over a websocket. This is specially useful if you want to add subscriptions to your GraphQL schema, but you can also use the websocket for queries and mutations.

GraphQL graphQL = setupGraphQLJava();

To support the graphql-ws websocket subprotocol, it has to be added to the server configuration:
HttpServerOptions httpServerOptions = new HttpServerOptions()
If you want to support a WebSocketLink and a HttpLink in the same path, you can add the ApolloWSHandler in first place and then the GraphQLHandler.
GraphQL graphQL = setupGraphQLJava();


Here you can find how to configure the Apollo SubscriptionClient:

A subscription DataFetcher has to return a org.reactivestreams.Publisher instance.

RxJava 2 API

Setting up with an Rxified router

To handle GraphQL requests on a Rxified Route, make sure to import the GraphQLHandler class.

Working with Vert.x Rxified APIs

GraphQL-Java expects CompletionStage for asynchronous results in data fetchers and batch loaders.

Therefore, if you work with the Vert.x Rxified APIs (e.g. the Web Client or the Cassandra Client), you will have to adapt the Single and Maybe objects.

The RxJava2Jdk8Interop library provides the tooling to do just that. Add the following to the dependencies section of your Maven build file:


Or if you use Gradle:

compile 'com.github.akarnokd:rxjava2-jdk8-interop:0.3.5'

Then you can create a data fetcher from a Single result:

Single<String> data = loadDataFromBackend();
DataFetcher<CompletionStage<String>> fetcher = environment -> {

For Maybe results, use MaybeInterop.