Skip to content

Ridhorezi/Spring-GraphQL-Playground

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Spring Boot GraphQL Playground

This project is a Spring Boot application using GraphQL to demonstrate CRUD operations on Customer entities.

🛠 Tech Stack

  • Java 21
  • Spring Boot 3 (GraphQL Starter)
  • Spring Data JPA
  • GraphQL Java
  • H2 Database (in-memory)
  • Lombok
  • Maven

📦 Project Structure

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 dependencies

📌 Features

GraphQL-based API for Customer service:

  1. 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.
  2. Schema-driven Development

    • Menggunakan file schema.graphql untuk mendefinisikan struktur query, mutation, dan tipe data.
  3. Integrated GraphiQL Playground

    • Akses GraphiQL UI untuk eksplorasi API secara interaktif.

🚀 How to Run the Project

1. Clone the repository

git clone https://github.com/Ridhorezi/graphql-playground.git
cd graphql-playground

2. Build and run

./mvnw spring-boot:run

The application will start on http://localhost:8082 by default.


📡 GraphQL Endpoints

GraphQL Endpoint

http://localhost:8082/graphql

GraphiQL Playground

http://localhost:8082/graphiql

📊 Example GraphQL Operations

Query: Get All Customers

{
  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"
      }
    ]
  }
}

Query: Get Customer by ID

{
  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: Create Customer

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: Update Customer

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: Delete Customer

mutation {
  deleteCustomer(id: 1)
}

Example Response

{
  "data": {
    "deleteCustomer": true
  }
}

🗂 Database

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)

📜 License

This project is licensed under the MIT License.

✍️ Author

Ridho Suhaebi Arrowi
Feel free to contribute or fork!

About

Spring Boot GraphQL Playground

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages