GraphQL Tutorial: Understanding Spring Data JPA/SpringBoot

GraphQL Tutorial: Understanding Spring Data JPA/SpringBoot

Let's understand Spring Data JPA and Spring Boot with a practical example.

GraphQL is a query language for APIs. Generally, while making REST endpoints in our APIs, the normal trend is to make an endpoint for a requirement. Let’s say your endpoint is returning a list of employees, and each employee has general properties, such as name, age, and address (suppose address is another model mapped to the employee in such a way that each employee has an address). Now, at one point in time, you require data only for their address i.e. a list of all addresses in the database (only country, city, and street). For this, you will require an all-new endpoint in your service.

Here comes the power of GraphQL, which lets you deal with only a single endpoint and that changes its output based on the body of the request. Each request will call the same endpoint but with a different RequestBody. It will receive only the result that it requires.

Refer to the code on GitHub for complete code files. It’s a maven project with an H2 database that has data.sql at classpath for database queries. This code rotates around getting a list of all employees from the database.

Now, let’s begin with a practical implementation with Spring Boot.

We have two model classes: employee and address with respective getters and setters.

@Entity
@Table
public class Employee {
String name;
@Id
String id;
int age;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "addid")
Address address;
  //......getters and setters....//
}

@Entity
@Table
public class Address {
@Id
@GeneratedValue
String addid;
String country;
String city;
String flat;
  //......getters and setters....//
}

To implement the repository, we have the EmployeeRepo as:

@Repository
public interface EmployeeRepo extends CrudRepository<Employee, String> {

public List<Employee> findAll();

}

Now, GraphQL requires a .graphqls file at the classpath, which it parses and understands the type of request it needs to handle. You will find employee.graphqls in the code. Let me explain that.

It contains:

type Employee{

.......Employee details

}

and type Address{

........Address Details

}

Here, we are defining the schema of our classes, which will, in one way or another, be returned as a response to the endpoint i.e either it will return all employees, the addresses of all employees, only the names of all employees, etc.

We also defined:

type Query{

allEmployee: [Employee]
}

Type of query (a query that will be sent by the client or that will be present in RequestBody) here is returning list of employees.

So, RequestBody to our endpoint will contain a root query as allEmployee.

Let’s discuss requests here:

1. Requests that require all employees without their addresses:

{
allEmployee{
name
    age
id
}
}

2. Requests that require only employee names and the country that they belong to:

{
allEmployee{
name
address {
country
}
}

}

3. Requests that require only the address of all employees will be:

{
allEmployee{

address {
country
                city
                flat
                addid
}
}

}

For an explanation of the service layer of the code, read part 2, which is coming soon.

Meanwhile, you can access the full code on GitHub.


Learn More

Node.js - Express Persistent Session Store with PostgreSQL + Sequelize

Build a Simple Web App with Express, Angular, and GraphQL

How to Install PostgreSQL on Ubuntu 18.04

Understanding the basics of RESTful APIs

An Introduction to Queries in PostgreSQL

GraphQL with React: The Complete Developers Guide

GraphQL with Angular & Apollo - The Full-stack Guide

GraphQL: Learning GraphQL with Node.Js

Complete guide to building a GraphQL API

GraphQL: Introduction to GraphQL for beginners

Originally published by Piyush Arora at https://dzone.com