This project is a simple Spring Boot application demonstrating a GraphQL API for managing books.
It supports creating new books and querying books from an in-memory H2 database.
- Create a new Book using GraphQL mutation
- Fetch all Books
- Fetch a single Book by ID
- Uses Spring Boot, GraphQL, JPA, and H2 database
- Entity:
Book.java
— Defines the Book object model. - Repository:
BookRepo.java
— Extends JPA repository for database operations. - Service:
BookServiceImpl.java
— Contains business logic for book operations. - Controller:
BookController.java
— GraphQL queries and mutations mapped here. - GraphQL Schema: Defined using
Book
,BookInput
, Query, and Mutation types.
- Java 21+
- Spring Boot 3.x
- Spring for GraphQL
- Spring Data JPA
- H2 Database (in-memory)
- GraphQL Query Language
- Java 21 or higher installed
- Maven installed
- Clone the Repository
git clone https://github.com/athrocks/GraphQL-SpringBoot.git
cd GraphQL-SpringBoot
- Run the Application
./mvnw spring-boot:run
Or if you use Maven installed globally:
mvn spring-boot:run
- Access GraphQL Playground
Visit:
http://localhost:8009/graphql
(Or your configured GraphQL client.)
mutation {
createBook(book: {
title: "Java Techie",
desc: "Java Learn",
price: 453,
pages: 1245,
author: "Atharva"
}) {
id
}
}
query {
allBooks {
id
title
price
}
}
query {
getBook(bookId: 2) {
title
desc
}
}
spring.application.name=
server.port=8009
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
- Access H2 console at
http://localhost:8009/h2-console
- JDBC URL:
jdbc:h2:mem:testdb
type Mutation {
createBook(book: BookInput): Book
}
type Query {
allBooks: [Book]
getBook(bookId: Int): Book
}
type Book {
id: ID!
title: String
desc: String
author: String
price: Float
pages: Int
}
input BookInput {
title: String
desc: String
author: String
price: Float
pages: Int
}