Getting started with GraphQL Java
Tags: graphql, java, beginners, webdev
Published: 2019-03-29 12:29:10

---
title: Getting started with GraphQL Java
published: true
description: GraphQL Java. GraphQL can be used with any language. Today let’s take a look at GraphQL Java implementation.
tags: graphql,java,beginners,webdev
cover_image: https://thepracticaldev.s3.amazonaws.com/i/4m1d5u8bc0phszru265p.png
canonical_url: https://blog.graphqleditor.com/Getting-started-with-GraphQL-Java/
series: Let's learn GraphQL Java
---
[GraphQL](https://graphql.org/) can be implemented in any language. Today let’s take a look at Java server implementation for GraphQL. Let’s use [Maven](https://maven.apache.org/) 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](https://github.com/graphql-java/graphql-java) implementation. However, there are a couple of libs you might find useful when using GraphQL Java:
- [Spring Boot](https://github.com/graphql-java/graphql-java-spring) - 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](https://github.com/graphql-java-kickstart/graphql-java-tools) - this library allows you to use the GraphQL SDL to build your graphql-java schema. 
- [GraphQL Java Servlet](https://github.com/graphql-java-kickstart/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](https://thepracticaldev.s3.amazonaws.com/i/iwxghrh4bx0qpsd7jbrq.png)

### Define the schema
[GraphQL Schema](https://blog.graphqleditor.com/Five-mistakes-to-avoid-when-getting-started-with-GraphQL/) 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)](https://blog.graphqleditor.com/GraphQL-tutorial-dictionary/). 

The SDL defines the elements of your project like:

- **`type`** (the most basic GraphQL schema components):
![GraphQL Type](https://thepracticaldev.s3.amazonaws.com/i/fkgrrdltxjaptikr55ee.png)
- **`query`** (asking the server for the data) 
![GraphQL Query](https://thepracticaldev.s3.amazonaws.com/i/51nxj15zqozcei1382i8.png)
- **`mutation`** (manipulating the data):
![GraphQL Mutation](https://thepracticaldev.s3.amazonaws.com/i/fkgrrdltxjaptikr55ee.png)
 The fastest way to define your schema is to use [GraphQL Editor](https://graphqleditor.com/). It allows you to define your schema traditionally (code) or shape it from visual nodes.
[![Low-code GraphQL Editor](https://thepracticaldev.s3.amazonaws.com/i/sjaue0skobsp62kedwbt.png)](https://graphqleditor.com/)

### 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](https://maven.apache.org/pom.html#Plugins):
![Jetty](https://thepracticaldev.s3.amazonaws.com/i/8za6ezzwuuc83khlaaf6.png)

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](https://www.howtographql.com/graphql-java/1-getting-started/) server on https://www.howtographql.com/