Tomek Poniatowicz
3/29/2019
OOne of the best things about GraphQL is that it 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 next?
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:
/graphql
endpoint of your app and accept POST
requests containing your GraphQL payload.So add dependencies you need to Project Object Model file (pom.xml):
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):
query
(asking the server for the data)
mutation
(manipulating the data):
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.
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:
And you are almost there, the next steps are:
GraphQLEndpoint
class for exposing your APIIf 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/