Getting Started in GraphQL With Spring Boot in Java
1. Getting Started in GraphQL With Spring Boot in Java
We are just trying to integrate GraphQL with Spring Boot. I am assuming that you have some knowledge about GraphQL and Spring Boot. We are creating controllers for CRUD operations using GraphQL. I have created a sample spring boot microservice for crud operation on Person
object to understand the concept.
In GraphQL read operations means query which can be denoted by @QueryMapping
annotation.
create/update/delete operations means mutations which are denoted by @MutationMapping
annotation.
Data passed using Postman tool to the controller, can be captured using @Argument
annotation provided by spring-boot-graphql
, it's alternative of @RequestParam
for GraphQL.
2. GitHub repo for working project
Follow this link for code on Github
3. Dependencies needed
Java 8 version is used with Spring boot framework
4. Mapping between table, graphql schema & java class
5. GraphQL Schema:-
Write following schema in schema.graphqls
file under resources/graphql
folder in the spring boot project
type Person {
id: ID!
name: String!
country: String!
}
# comments can be written with # symbol
type Query {
# Retrieve one person by ID
getPerson(id: ID!): Person
# Retrieve all persons
getAllPersons: [Person!]!
}
type Mutation {
# Add a new person
addPerson(name: String, country: String): Person
# we can write like this also
# addPerson(input: AddPersonInput!): Person
# Update a person by ID
updatePerson(id: ID!, name: String, country: String): Person
# we can write like this also
# updatePerson(id: ID!, input: UpdatePersonInput!): Person
# Delete a person by ID
deletePerson(id: ID!): Person
}
# if we want to use input as argument while adding/updating person
# or else not needed
input AddPersonInput {
name: String!
country: String!
}
# if we want to use input as argument while adding/updating person
# or else not needed
input UpdatePersonInput {
name: String
country: String
}
6. Java Code
class Person {
private long id;
private String name;
private String country;
}
@RestController
public class PersonController {
@Autowired
private PersonRepository personRepository;
// get all persons
@QueryMapping("allPersons")
public List<Person> persons(){
return personRepository.findAll();
}
//get one person
@QueryMapping
public Person person(@Argument int id){
// use optional to get data, refer github repo
return personRepository.findById((long)id);
}
//add one person
@MutationMapping("addPerson")
public Person person(@Argument String name, @Argument String country){
// refer github for correct code, this is just for understanding
Person p = new Person(name, country);
personRepository.save(p);
}
}
7. Postman Request
# postman request
url - http://localhost:8080/graphql
# getPerson(id: ID!): Person
query{
getPerson(id: "1") {
name
country
}
}
# getAllPersons: [Person!]!
query{
getAllPersons {
persons {
name
country
}
}
}
# addPerson(name: String, country: String): Person
mutation{
addPerson(name: "ABD", country: "SA"){
id # this will be generated automatically for newly added person
name
country
}
}