Since you’ll be using Maven (still the most widely used build tool for Java) in this tutorial, make sure you have a reasonably fresh version installed.
Next you’ll setup the project structure.
It is important to note that the resolver functions are an integral part of the field definitions, and thus a part of the schema. This means the schema isn’t just a document, but a runtime object instance (for the purposes of this track, that would mean a Java object).
The schema can be defined in two ways:
Both approaches have merit, and come down to a matter of preference. The former collocates the fields and their associated resolves, while the latter makes a clear cut between data and behavior. We’ll use SDL for the most part of this track as it allows for succinct examples.
The SDL definition for a simple type representing a link might look like this:
To build a GraphQL-enabled application, only
graphql-java (the GraphQL implementation itself) is strictly required. Still, to make dynamic resolver wiring easy, you’ll also want to use
graphql-java-tools, the library inspired by Apollo’s
graphql-tools. Additionally, because the goal is to expose the API over the web, you’ll also make use of
graphql-java-servlet (a simple helper library containing a ready-made servlet for accepting GraphQL queries) and
javax.servlet-api (the servlet specification implementation).
The versions listed above were the latest at the time of writing, but they change quickly as bugs are fixed and features are added. Make sure you always check for updates before going further.
Any servlet container will do here, and the simplest way to use one during development is via a Maven plugin.
This also a good opportunity to configure some basics.
You can run the app just by executing
mvn jetty:runin the directory where
pom.xmlis located, and Jetty will start on port 8080.
But opening it at this moment won’t bring you much joy, as the server still isn’t configured to do anything.
Starting the server now and accessing http://localhost:8080/graphql will still result in an error because no resolver functions have been wired in (so the defined
allLinks query has no way to execute).