This project is an implementation of a simple order listing service using gRPC, GraphQL, and REST API. It demonstrates how to create a multi-service application with a shared PostgreSQL database using Docker Compose.
- Prerequisites
- Create .env file
- Run the Application
- Run Migrations
- Insert Sample Data
- Testing the Services
- Clean Up
- Docker
- Docker Compose
- Go
- grpcurl (for testing gRPC services)
Create a .env
file in the root directory of the project with the following content:
DB_USER=user
DB_PASSWORD=password
DB_NAME=orders_db
DB_HOST=db
DB_PORT=5432
First, make sure Docker is running, then execute the following commands:
docker-compose build
docker-compose up -d
This will build the application and start all the services defined in the docker-compose.yml
file.
Migrations will be automatically run by the migrate service defined in docker-compose.yml.
You can test the REST API using curl
or any API testing tool like Postman. The REST API will be available at http://localhost:8080
.
curl -X POST -H "Content-Type: application/json" -d '{"user_id": 1, "product_id": 101, "quantity": 2, "status": "pending"}' http://localhost:8080/order
curl http://localhost:8080/order
You can test the GraphQL API using the GraphQL Playground available at http://localhost:8081/playground
.
Create Order:
mutation {
createOrder(user_id: 1, product_id: 101, quantity: 3, status: "pending") {
id
user_id
product_id
quantity
status
created_at
updated_at
}
}
List Orders:
query {
listOrders {
id
user_id
product_id
quantity
status
created_at
updated_at
}
}
You can test the gRPC service using grpcurl
. The gRPC service will be available at localhost:50051
.
grpcurl -plaintext localhost:50051 list
grpcurl -plaintext -d '{
"user_id": 1,
"product_id": 101,
"quantity": 3,
"status": "pending"
}' localhost:50051 order.OrderService/CreateOrder
grpcurl -plaintext -d '{}' localhost:50051 order.OrderService/ListOrders
To stop and remove all the containers, network, and volumes, run the following command:
docker-compose down -v