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.

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
}
}
  1. Requests that require only employee names and the country that they belong to:
{
allEmployee{
name
address {
country
}
}

}
  1. 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

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Getting Started with GraphQL and Spring Boot

GraphQL is a query language for APIs that allows clients to request limited data they need, making it possible for clients to gather data in a limited number of requests. In this article, we will build a simple GraphQL server with Spring Boot.

Spring Boot Tutorials - Spring Boot Full Course

Spring Boot Tutorials | Full Course - What is Spring? Spring Boot is an open source Java-based framework used to create a Micro Service. Spring Boot contains a comprehensive infrastructure support for developing a micro service and enables you to develop enterprise-ready applications that you can “just run”.

How to Build a GraphQL API with Java and Spring Boot

In this tutorial, I’ll show you how to use Java and Spring Boot to build a GraphQL API. How to GraphQL in Java. Create a GraphQL API with Java and Spring Boot. Run Your Java GraphQL API. Test Your Java GraphQL API with JUnit 5. Secure Your Java GraphQL API. Further Reading on GraphQL and Java.