This is a demo project for getting started with graphql. In this project there is create
, read
and delete
operations.
Implementing the update operation is left for your self practice.
This Project also includes the tests that covers 100% of the codebase.
We didn’t use an actual database though, we have used a fake in memory database(Simple data structures in this case - List<Author> authors
for Authors and List<Book> books
for Books).
This database will be empty when the application starts, and also will be removed if the application is stopped. So, restarting your application would get you a new fresh empty database everytime.
This project has four packages:.
In this package, there are two controllers, there are two types of mapping in this project: @QueryMapping
and @MutationMapping
.
@QueryMapping
can be compared with the GET
request, though all the requests of graphql is POST
request, and
@MutationMapping
can be compared with the POST
request. Controller classes should be annotated with @Controller
.
AuthorController.java
controls the Author related request-response and the BookController.java
controls the Book related request-response.
In this package, there are two types of data model, one is Author
and another is Book
. These models hold the data of the Book and the Author.
In this package, there are two repository, one is AuthorRepository
and another is BookRepository
. The holds the information about the database and the methods to operate on the database.
Repositories should be annotated with @Repository
.
In this package, there are two Interface
named AuthorService
and BookService
. They both hold the functionality of what a user can do with these.
We define the business logic, or the service we want to get with this model and controller. Services should be annotated with @Service
.
We can use postman or the graphiql
interface. Graphiql interface is a default graphical user interface to work with graphql request and response.
To enable the graphiql interface for a graphql project, we need to enable it from the application.properties
file of our project. To enable it, we
need to add spring.graphql.graphiql.enabled=true
in our application.properties
file.
After running the application, the graphiql can be accessed via http://localhost:8080/graphiql
url.
These are the queries this project supports for Authors.
mutation{
addAuthor(firstName: "Kazi", lastName: "Afsana")
}
mutation{
removeAuthor(firstName: "kazi", lastName: "Afsana")
}
{
author(firstName: "Kazi", lastName: "Afsana"){
id
firstName
lastName
}
}
{
authors{
id
firstName
lastName
}
}
These are the queries this project supports for Books.
mutation {
addBook(name: "101 ways to bake", pageCount: 290, authorFirstName: "Kazi")
}
mutation {
removeBook(name: "101 ways to bake")
}
{
book(name: "101 ways to bake"){
id
name
pageCount
author{
id
firstName
lastName
}
}
}
{
books{
id
name
pageCount
author{
id
firstName
lastName
}
}
}
Feel free to suggest any changes. Happy coding! ❤
You can reach me on LinkedIn from here.