DEV Community

Cover image for Getting started with GraphQL Java
Tomek Poniatowicz for GraphQL Editor

Posted on • Edited on • Originally published at blog.graphqleditor.com

28 13

Getting started with GraphQL Java

GraphQL can be implemented in any language. Today let’s take a look at Java server implementation for GraphQL. Let’s use Maven and assume that we already have set up the project structure. What's next?

Dependencies

To run GraphQL in your Java project you only need one library which is GraphQL Java implementation. However, there are a couple of libs you might find useful when using GraphQL Java:

  • Spring Boot - GraphQL Java Spring and Spring Boot integration that will handle any HTTP related tasks, expose the GraphQL service on the /graphql endpoint of your app and accept POST requests containing your GraphQL payload.
  • GraphQL Java Tools - this library allows you to use the GraphQL SDL to build your graphql-java schema.
  • GraphQL Java Servlet - implementation of GraphQL Java Servlet including support for libraries like Relay.js, Apollo etc. and wraps the Java implementation of GraphQL provided by GraphQL Java.

So add dependencies you need to Project Object Model file (pom.xml):
Add GraphQL Java dependency

Define the schema

GraphQL Schema is the centerpiece of any GraphQL implementation so we definitely need one before going any further. To make it easier to understand the operation that a server can perform GraphQL defined a universal schema syntax know as SDL (Schema Definition Language).

The SDL defines the elements of your project like:

  • type (the most basic GraphQL schema components): GraphQL Type
  • query (asking the server for the data) GraphQL Query
  • mutation (manipulating the data): GraphQL Mutation The fastest way to define your schema is to use GraphQL Editor. It allows you to define your schema traditionally (code) or shape it from visual nodes. Low-code GraphQL Editor

Server

There plenty of servlet containers to choose from, both open source and commercial, so just pick the one you feel comfortable with. Let's use Jetty for our example and implement it via a Maven Plugins:
Jetty

And you are almost there, the next steps are:

  • adding some more basic server configurations like right Java version or servlet specification
  • creating GraphQLEndpoint class for exposing your API
  • defining resolvers

If you want to read more details about GraphQL Java implementation to take a look at Bojan's tutorial regarding setting up GraphQL Java server on https://www.howtographql.com/

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (1)

Collapse
 
mjamsek profile image
Miha Jamsek

Hi, another option for Java developers wanting to use GraphQL is KumuluzEE, open-source microservice oriented framework. Here is a sample: github.com/kumuluz/kumuluzee-sampl...

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay