Implementing a GraphQL server
What you will build
You will build a GraphQL server to manage your personal tasks (a.k.a. the todo list).
The application consists in a few files:
What you need
A text editor or IDE
Java 8 higher
Maven or Gradle
Create a project
The code of this project contains Maven and Gradle build files that are functionally equivalent.
Here is the content of the
pom.xml file you should be using:
Assuming you use Gradle with the Kotlin DSL, here is what your
build.gradle.kts file should look like:
Managing tasks with Vert.x Web and GraphQL
Creating the schema
First things first, let’s create a GraphQL schema for the task management app:
The schema defines:
Tasktype with 3 fields:
allTasksquery that returns an array of tasks and optionally takes a parameter
completemutation that takes the task
idas parameter and returns a boolean indicating whether the operation was successful
Implementing the server
In the application, tasks will be modeled by the
On startup, we shall create a few items:
Then the GraphQL-Java engine must be setup:
So far, so good. Now how does one implement a data fetcher?
allTasks data fetcher gets the
uncompletedOnly parameter from the
Its value is either provided by the client or, as defined in the schema file, set to
Do not block the Vert.x event loop in your data fetchers.
In this how-to, the data set is small and comes from memory, so it’s safe to implement
The code for the
complete mutation is not much different:
It gets the
id parameter provided by the client, then looks up for the corresponding task.
If a task is found, it is updated and the mutation returns
true to indicate success.
Now let’s put things together in the verticle
Running the application
GraphQLVerticle needs a