Skip to content

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.

License

Notifications You must be signed in to change notification settings

athrocks/GraphQL-SpringBoot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

πŸ“š GraphQL Book Management API

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.


✨ Features

  • Create a new Book using GraphQL mutation
  • Fetch all Books
  • Fetch a single Book by ID
  • Uses Spring Boot, GraphQL, JPA, and H2 database

πŸ“¦ Project Structure

  • 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.

πŸ”§ Technologies Used

  • Java 21+
  • Spring Boot 3.x
  • Spring for GraphQL
  • Spring Data JPA
  • H2 Database (in-memory)
  • GraphQL Query Language

πŸš€ Running the Project

Prerequisites

  • Java 21 or higher installed
  • Maven installed

Steps

  1. Clone the Repository
git clone https://github.com/athrocks/GraphQL-SpringBoot.git
cd GraphQL-SpringBoot
  1. Run the Application
./mvnw spring-boot:run

Or if you use Maven installed globally:

mvn spring-boot:run
  1. Access GraphQL Playground

Visit:

http://localhost:8009/graphql

(Or your configured GraphQL client.)


πŸ“– Sample Queries & Mutations

Create a Book (Mutation)

mutation {
  createBook(book: {
    title: "Java Techie",
    desc: "Java Learn",
    price: 453,
    pages: 1245,
    author: "Atharva"
  }) {
    id
  }
}

Fetch All Books (Query)

query {
  allBooks {
    id
    title
    price
  }
}

Fetch Book by ID (Query)

query {
  getBook(bookId: 2) {
    title
    desc
  }
}

πŸ› οΈ Application Configuration (application.properties)

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

πŸ“„ GraphQL Schema

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
}

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages