The GraphQL Gateway provides a unified API for client interactions, aggregating data from all microservices.
- Unified GraphQL API for users, products, and orders
- JWT authentication verification
- Data aggregation from multiple microservices
- Node.js
- Apollo Server
- GraphQL
-
Navigate to the graphql-gateway directory:
cd graphql-gateway
-
Install dependencies:
npm install
-
Set up environment variables: Create a
.env
file in the graphql-gateway directory and add:PORT=4000 USER_SERVICE_URL=http://localhost:5000 PRODUCT_SERVICE_URL=http://localhost:5002 ORDER_SERVICE_URL=http://localhost:5001
-
Start the gateway:
npm start
The gateway is automatically deployed when you run docker-compose up
from the root directory. It will be available at http://localhost:4000/graphql
.
-
users
: Retrieve a list of all usersquery { users { _id name email userId createdAt updatedAt } }
-
user(id: ID!)
: Get details of a specific user by their IDquery ($id: ID!) { user(id: $id) { _id name email userId createdAt updatedAt } }
-
products
: Retrieve a list of all productsquery { products { _id name price quantity productId createdAt updatedAt } }
-
product(id: ID!)
: Get details of a specific product by its IDquery ($id: ID!) { product(id: $id) { _id name price quantity productId createdAt updatedAt } }
-
orders
: Retrieve a list of all ordersquery { orders { _id user_Id { _id name email userId address { street city state zip } createdAt updatedAt } product_Id quantity order_Id createdAt updatedAt } }
-
order(id: ID!)
: Get details of a specific order by its IDquery ($id: ID!) { order(id: $id) { _id user_Id { _id name email userId address { street city state zip } createdAt updatedAt } product_Id quantity order_Id createdAt updatedAt } }
-
registerUser(input: RegisterInput!): User
Register a new user by providing their details:mutation ($input: RegisterInput!) { registerUser(input: $input) { _id name email userId address { street city state zip } createdAt updatedAt } }
-
loginUser(input: LoginInput!): LoginResponse
Login a user with their email and password:mutation ($input: LoginInput!) { loginUser(input: $input) { user { name email userId } token } }
-
createProduct(input: ProductInput!): Product
Create a new product by providing its details:mutation ($input: ProductInput!) { createProduct(input: $input) { _id name price quantity productId createdAt updatedAt } }
-
placeOrder(input: OrderInput!): Order
Place an order by specifying the product ID and quantity:mutation ($input: OrderInput!) { placeOrder(input: $input) { _id user_Id { _id name email userId address { street city state zip } createdAt updatedAt } product_Id quantity order_Id createdAt updatedAt } }
-
User
Represents a user in the system.type User { _id: ID! name: String! email: String! userId: String! createdAt: String! updatedAt: String! }
-
LoginUser
Represents a logged-in user.type LoginUser { name: String! email: String! userId: ID! }
-
LoginResponse
Represents the response after a successful login.type LoginResponse { user: LoginUser! token: String! }
-
Product
Represents a product in the catalog.type Product { _id: ID! name: String! price: Float! quantity: Int! productId: String! createdAt: String! updatedAt: String! }
-
Order
Represents an order placed by a user.type Order { _id: ID! user_Id: OrderUser! product_Id: ID! quantity: Int! order_Id: String! createdAt: String! updatedAt: String! }
-
OrderUser
Represents the user associated with an order.type OrderUser { _id: ID! name: String! email: String! userId: String! address: Address! createdAt: String! updatedAt: String! }
-
Address
Represents the user's address.type Address { street: String city: String state: String zip: Int }
-
RegisterInput
Input for registering a new user.input RegisterInput { name: String! email: String! password: String! street: String city: String state: String zip: Int }
-
LoginInput
Input for user login.input LoginInput { email: String! password: String! }
-
ProductInput
Input for creating a new product.input ProductInput { name: String! price: Float! quantity: Int! }
-
OrderInput
Input for placing an order.input OrderInput { productId: ID! quantity: Int! }
The gateway verifies JWT tokens for protected queries and mutations. Include the JWT in the Authorization header of your requests.