Note : This is a code sample written in one day for Shopify in order to show some of my coding skills.
This application is deployed on Heroku and can be tested easily thanks to GraphiQL. Test API online
- Ruby 2.5.0
- Rails 5.2
- Postgresql
- Clone this repo
- Run
bundle install
- Run
rails db:create && rails db:migrate && rails db:seed
- Run
rails server
- The GraphiQL interface is now available at
http://localhost:3000
Carts and Orders have very similar logic, the only difference is that Cart model is flexible to product price changes, Order has its own price attributes.
Use the following queries to complete an order. For more queries, follow the generated documentation on GraphiQL right pannel.
query {
products(availableOnly: false) {
id
title
description
price
inventoryCount
}
}
availableOnly
argument is optional
Note : cartId
is optional, if you don't have a cart yet, it will create one for you.
mutation {
addToCart(input:
{
productId: [selected_product_id]
cartId: [your_cart_id]
}
)
{
errors
cart {
id
totalPrice
cartsProducts {
quantity
product {
id
title
}
}
}
}
}
mutation {
removeFromCart(input:
{
productId: [selected_product_id]
cartId: [your_cart_id]
}
)
{
errors
cart {
id
totalPrice
cartsProducts {
quantity
product {
id
title
}
}
}
}
}
query {
cart(id: [your_cart_id]) {
id
totalPrice
cartsProducts {
quantity
product {
title
}
}
}
}
mutation {
placeOrder(input:
{
cartId: [your_cart_id],
email: "[email protected]"
}
)
{
errors
order {
id
totalPrice
ordersProducts {
product {
title
}
}
}
}
}
query {
order(id: [your_order_id]){
id
totalPrice
ordersProducts {
quantity
productPrice
product {
title
}
}
}
}