Nikolas Burk
Written By
Nikolas Burk
Developer @ Prisma

Nikolas is a developer and head of content at Prisma. He is excited about GraphQL as a new API technology and has a passion for learning and sharing knowledge.

Getting Started

Initialize the project

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.

Defining the schema

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:

  • programmatically - where type definitions are assembled manually in code
  • using the Schema Definition Language (SDL) - where the schema is generated from a textual language-independent description you’ve seen in the previous chapters with the resolver functions then wired dynamically

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:

Install dependencies

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.

Setup server

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:run in the directory where pom.xml is 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).

Unlock the next chapter
How are GraphQL schemas built?
Only by using the schema language
Only programmatically, because schemas contain functions
Either using the schema language or programmatically
Using Swagger or similar tools