This project implements a microservices architecture using Go, gRPC, and GraphQL. It consists of three microservices (Account, Catalog, Order) with a GraphQL gateway that provides a unified API.
- Account Service: Manages user accounts using PostgreSQL
- Catalog Service: Manages products using Elasticsearch
- Order Service: Manages orders using PostgreSQL and integrates with Account and Catalog services
- GraphQL Gateway: Provides a unified GraphQL API that aggregates all services
- Database: PostgreSQL
- Features: Create, read, and list accounts
- gRPC Endpoints: PostAccount, GetAccount, GetAccounts
- Database: Elasticsearch
- Features: Create, read, list, and search products
- gRPC Endpoints: PostProduct, GetProduct, GetProducts
- Database: PostgreSQL
- Features: Create orders and retrieve orders for accounts
- gRPC Endpoints: PostOrder, GetOrdersForAccount
- Dependencies: Account Service, Catalog Service
- Features: Unified GraphQL API
- Playground: http://localhost:8080/playground
- GraphQL Endpoint: http://localhost:8080/graphql
- Docker and Docker Compose
- Go 1.21+ (for local development)
-
Clone the repository
git clone <repository-url> cd go_microservices
-
Start all services with Docker Compose
docker-compose up --build
-
Access the GraphQL Playground
- Open http://localhost:8080/playground in your browser
- Explore the GraphQL schema and test queries/mutations
# Get all accounts with pagination
query GetAccounts($pagination: PaginationInput) {
accounts(pagination: $pagination) {
id
name
orders {
id
createdAt
totalPrice
products {
id
name
description
price
quantity
}
}
}
}
# Get all products with search
query GetProducts($pagination: PaginationInput, $query: String) {
products(pagination: $pagination, query: $query) {
id
name
description
price
}
}# Create a new account
mutation CreateAccount($account: AccountInput!) {
createAccount(account: $account) {
id
name
}
}
# Create a new product
mutation CreateProduct($product: ProductInput!) {
createProduct(product: $product) {
id
name
description
price
}
}
# Create a new order
mutation CreateOrder($order: OrderInput!) {
createOrder(order: $order) {
id
createdAt
totalPrice
products {
id
name
description
price
quantity
}
}
}-
Start databases only
docker-compose up account_db order_db catalog_db
-
Run services individually
# Account service cd account/cmd/account DATABASE_URL="postgres://account_user:account_pass@localhost:5433/account_db?sslmode=disable" go run . # Catalog service cd catalog/cmd/catalog ELASTICSEARCH_URL="http://localhost:9200" go run . # Order service cd order/cmd/order DATABASE_URL="postgres://order_user:order_pass@localhost:5434/order_db?sslmode=disable" \ ACCOUNT_SERVICE_URL="localhost:8081" \ CATALOG_SERVICE_URL="localhost:8083" go run . # GraphQL gateway cd graphql ACCOUNT_SERVICE_URL="localhost:8081" \ CATALOG_SERVICE_URL="localhost:8083" \ ORDER_SERVICE_URL="localhost:8082" go run .
# Build individual services
docker build -f account/app.dockerfile -t account-service ./account
docker build -f catalog/app.dockerfile -t catalog-service ./catalog
docker build -f order/app.dockerfile -t order-service ./order
docker build -f graphql/app.dockerfile -t graphql-gateway ./graphqlCREATE TABLE accounts(
id CHAR(27) PRIMARY KEY,
name VARCHAR(24) NOT NULL
);CREATE TABLE orders(
id CHAR(27) PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
account_id CHAR(27) NOT NULL,
total_price MONEY NOT NULL
);
CREATE TABLE order_products(
order_id CHAR(27) REFERENCES orders (id) ON DELETE CASCADE,
product_id CHAR(27),
quantity INT NOT NULL,
PRIMARY KEY (order_id, product_id)
);- Index:
catalog - Type:
product - Fields:
name,description,price
- Account Service:
localhost:8081 - Catalog Service:
localhost:8083 - Order Service:
localhost:8082
- GraphQL Endpoint:
http://localhost:8080/graphql - Playground:
http://localhost:8080/playground
DATABASE_URL: PostgreSQL connection string
ELASTICSEARCH_URL: Elasticsearch connection URL
DATABASE_URL: PostgreSQL connection stringACCOUNT_SERVICE_URL: Account service gRPC URLCATALOG_SERVICE_URL: Catalog service gRPC URL
ACCOUNT_SERVICE_URL: Account service gRPC URLCATALOG_SERVICE_URL: Catalog service gRPC URLORDER_SERVICE_URL: Order service gRPC URL
-
Create an account
mutation { createAccount(account: {name: "John Doe"}) { id name } }
-
Create a product
mutation { createProduct(product: { name: "Laptop" description: "High-performance laptop" price: 999.99 }) { id name description price } }
-
Create an order
mutation { createOrder(order: { accountId: "ACCOUNT_ID_HERE" products: [{ id: "PRODUCT_ID_HERE" quantity: 2 }] }) { id createdAt totalPrice products { id name price quantity } } }
-
Database Connection Issues
- Ensure PostgreSQL and Elasticsearch containers are running
- Check connection strings in environment variables
-
gRPC Connection Issues
- Verify service URLs and ports
- Check if services are running and accessible
-
GraphQL Schema Issues
- Regenerate GraphQL code:
go run github.com/99designs/gqlgen generate - Check schema.graphql file for syntax errors
- Regenerate GraphQL code:
View logs for specific services:
docker-compose logs account
docker-compose logs catalog
docker-compose logs order
docker-compose logs graphqlThis project is licensed under the MIT License.