DZone
Database Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > GraphQL: Understanding Spring Data JPA/Spring Boot

GraphQL: Understanding Spring Data JPA/Spring Boot

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

Piyush Arora user avatar by
Piyush Arora
·
Mar. 01, 19 · Database Zone · Tutorial
Like (5)
Save
Tweet
25.79K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Meanwhile, you can access the full code on GitHub.



If you enjoyed this article and want to learn more about GraphQL, check out this collection of tutorials and articles on all things GraphQL.

GraphQL Spring Data Database Data (computing) Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Upload Files to AWS S3 in JMeter Using Groovy
  • Making Machine Learning More Accessible for Application Developers
  • A Guide to Understanding Vue Lifecycle Hooks
  • How to Integrate Zoom in React Application?

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo