This project is a demo application built with Spring Boot, GraphQL, and Java. It showcases the integration of GraphQL APIs with a backend service for managing inventory items.
- GraphQL API:
- Queries to fetch inventory items.
- Mutations to add new inventory items.
- Subscriptions for real-time updates on inventory items.
- Reactive Programming:
- Utilizes Reactor for handling subscriptions and real-time updates.
- Java 17
- Spring Boot 3.4.5
- GraphQL
- Reactor
- Maven
src/main/java
: Contains the main application code.controller
: Handles GraphQL queries, mutations, and subscriptions.entities
: Defines theInventoryItem
entity.service
: Contains business logic for inventory management.util
: Includes utility classes likeInventoryPublisher
.
src/main/resources/graphql
: Contains GraphQL schema files.src/test/java
: Includes unit tests for the application.
- Prerequisites:
- Java 17 or higher.
- Maven installed.
- Clone the Repository:
git clone https://github.com/ibrahimnadra/GraphQLDemo.git cd GraphQLDemo
- Update Configuration:
- Ensure you have a MySQL database running.
- Create a schema for the application.
- Update the database connection properties in
application.properties
: -
spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name spring.datasource.username=your_username spring.datasource.password=your_password
- Build the Project:
./mvnw clean install
- Run the Application:
./mvnw spring-boot:run
- Access GraphQL Playground:
- Navigate to
http://localhost:8080/graphql
.
- Navigate to
The GraphQL schema defines the following types:
- InventoryItem: Represents an inventory item with fields like
id
,name
,category
,supplier
,costPrice
,sellingPrice
, andquantity
. - Query: Fetch inventory items or a specific item by ID.
- Mutation: Add new inventory items.
- Subscription: Listen for real-time updates on inventory items.
- Unit tests are written using JUnit and Reactor Test.
- Run tests with:
./mvnw test
- Open Postman and click on create from my workspace section.
- Select the GraphQL option.
- Enter the GraphQL endpoint URL:
http://localhost:8080/graphql
. - Under the
Schema
tab, you can import the GraphQL schema file located atsrc/main/resources/graphql/inventory.graphqls
in the project directory to get auto-completion and documentation support. - Under the
Query
tab, you can see all the method which are imported from the schema, under the related operation (query, mutation, subscription). - Check the schema method you want to test, for example,
addInventoryItem
and fill in the required argument fields. - Check the fields you want to return in the response.
- Finally click on the
Query
button to execute the request and view the response. - For subscription, create new request and see the real time updates on adding new inventory items.