GraphQL with Java

Okan Sungur
3 min readJan 17, 2022

--

GraphQL can be defined as an open-sourced query language, that API consumers can use when asking for data. It was first developed by Facebook, according to the necessities, and now it is governed by the GraphQL Foundation. Pinterest, Twitter, Airbnb, and GitHub are other companies that use the technology. It is not specific for frontend, backend . It provides standards and structures for data implementation. When compared with the Rest API, you don’t have to get the full block of data, for your requests. You can get only what you need. (efficiency)
GraphQL is served over HTTP and optimizes data communication. You don’t have to make multiple requests to the rest endpoint, to get some relational data. With GraphQL; one call is enough. For microservices, it is recommended to use one GraphQL schema as an API gateway. As the number of requests can be reduced by this way. Another advantage is, when developing apps, frontend and backend developers can go on developing applications independently and separately from each other.

GraphQL is can be an alternative to REST, but it can not be a replacement. GraphQL is a specification that can be implemented in any language. Javascript, Java, Python, Kotlin, C#, Go, Rust, etc.

GraphQL Language Support

We can use it on the backend. But we have to define a schema to describe the data that we are exposing outside. Then an API consumer can use the GraphQL language for the requests. In that way, clients know everything about the data, located on the server.
Queries can be used as reading operations (like select queries) and Mutations are used for write-then-read operations(like create, insert, update queries). After updates we can get notified. Also queries can be executed in parallel but Mutations have side effects and are executed serially.

In frontend web or mobile applications GraphQL is used to communicate with GraphQL server. The client applications make the asynchronous calls. Here Apollo Client, a popular library for JavaScript can also be used for managing the asynchronous calls.
GraphQL uses JSON for data requests. But for client applications, a GraphQL query is used. It is similar to JSON but it has a shorter syntax.

GraphQL default scalar types :

Int: A signed 32-bit integer.
Float: A signed double-precision floating-point value.
String: A UTF 8 character sequence.
Boolean: true or false.
ID: The ID scalar type represents a unique identifier.

For databases we can not directly start using GraphQL, instead we have to use a runtime service layer. We are going to create a tutorial about it with Java. We will be using a student and a lecture schema to make the subject more clear.

We will use the IntelliJ Idea. It has GraphQL support. You can install the plugin from the settings menu.

IntelliJ Idea GraphQL plugin

We will use two entities for this tutorial. Student class will be our first entity and the other one will be the Lecture class. We will not use any database, instead we will use TheDataFetcher class for creating our data. Here we have to create a schema file “students. graphql” which will be located under the resources folder. You can get the source files from GitHub.

students.graphql

Please don’t forget to check the pom.xml file for the necessary dependencies.

TheDataFetcher.java

Now we will create a controller class. We will send our queries with the help of our StudentController class.

StudentController.java

Here be careful about the queries. As you can see from the code, the queries are embedded. For mapping getStudentLecture, we asked for studentid=2. And in return, we want the student name and lecture name.

query { student(id:2) { studentName lecture {lectureName} } }

Result of the Query

Thanks for reading. You can get the source code from GitHub.

--

--