This project is a Spring Boot application using GraphQL to demonstrate CRUD operations on Customer entities.
- Java 21
- Spring Boot 3 (GraphQL Starter)
- Spring Data JPA
- GraphQL Java
- H2 Database (in-memory)
- Lombok
- Maven
graphqlplayground/
├── src/main/java/com/grapql/grapqlplayground/
│ ├── config/ # Configuration (Auditing, etc)
│ ├── controller/ # GraphQL Controllers (CustomerController.java)
│ ├── entity/ # Entity model (Customer.java)
│ ├── repository/ # Spring Data JPA repository
│ ├── service/ # Business logic (CustomerService.java)
│ ├── exception/ # Global exception handling
│ └── GrapqlplaygroundApplication.java
├── src/main/resources/
│ ├── schema.graphql # GraphQL schema definition
│ ├── data.sql # Initial sample data
│ ├── schema.sql # Table creation script
│ ├── application.yml # App configuration
└── pom.xml # Maven dependenciesGraphQL-based API for Customer service:
-
CRUD Queries & Mutations
getAllCustomers→ Menampilkan semua customer.getCustomerById→ Mengambil customer berdasarkan ID.getCustomersByLastName→ Menampilkan customer berdasarkan nama belakang.getCustomerByEmail→ Mengambil customer berdasarkan email.createCustomer→ Membuat customer baru.updateCustomer→ Memperbarui data customer.deleteCustomer→ Menghapus customer berdasarkan ID.
-
Schema-driven Development
- Menggunakan file
schema.graphqluntuk mendefinisikan struktur query, mutation, dan tipe data.
- Menggunakan file
-
Integrated GraphiQL Playground
- Akses GraphiQL UI untuk eksplorasi API secara interaktif.
git clone https://github.com/Ridhorezi/graphql-playground.git
cd graphql-playground./mvnw spring-boot:runThe application will start on http://localhost:8082 by default.
http://localhost:8082/graphql
http://localhost:8082/graphiql
{
getAllCustomers {
id
firstName
lastName
email
phone
address
}
}Example Response
{
"data": {
"getAllCustomers": [
{
"id": "1",
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com",
"phone": "081234567890",
"address": "Jl. Sudirman No. 1, Jakarta"
},
{
"id": "2",
"firstName": "Jane",
"lastName": "Smith",
"email": "jane.smith@example.com",
"phone": "082345678901",
"address": "Jl. Thamrin No. 5, Jakarta"
}
]
}
}{
getCustomerById(id: 1) {
id
firstName
lastName
email
phone
address
}
}Example Response
{
"data": {
"getCustomerById": {
"id": "1",
"firstName": "John",
"lastName": "Doe",
"email": "john.doe@example.com",
"phone": "081234567890",
"address": "Jl. Sudirman No. 1, Jakarta"
}
}
}mutation {
createCustomer(
firstName: "Mike",
lastName: "Johnson",
email: "mike.johnson@example.com",
phone: "08561234567",
address: "Jl. Merdeka No. 10, Bandung"
) {
id
firstName
lastName
email
phone
address
}
}Example Response
{
"data": {
"createCustomer": {
"id": "6",
"firstName": "Mike",
"lastName": "Johnson",
"email": "mike.johnson@example.com",
"phone": "08561234567",
"address": "Jl. Merdeka No. 10, Bandung"
}
}
}mutation {
updateCustomer(
id: 1,
firstName: "Updated",
lastName: "Name",
email: "updated@example.com",
phone: "0800000000",
address: "New Address"
) {
id
firstName
lastName
email
phone
address
}
}Example Response
{
"data": {
"updateCustomer": {
"id": "1",
"firstName": "Updated",
"lastName": "Name",
"email": "updated@example.com",
"phone": "0800000000",
"address": "New Address"
}
}
}mutation {
deleteCustomer(id: 1)
}Example Response
{
"data": {
"deleteCustomer": true
}
}The project uses H2 in-memory database.
You can access the H2 console at:
http://localhost:8082/h2-console
Default credentials:
- JDBC URL:
jdbc:h2:mem:testdb - User:
sa - Password: (empty)
This project is licensed under the MIT License.
Ridho Suhaebi Arrowi
Feel free to contribute or fork!