Warehouse Management System
In this "Warehouse management system" example, you will learn how to configure common access patterns for your app. This example has the following types:
- Warehouse
- Product
- Inventory
- Employee
- AccountRepresentative
- Customer
These types have the following common access patterns:
- Look up employee details by employee ID
- Query employee details by employee name
- Find an employee's phone number(s)
- Find a customer's phone number(s)
- Get orders for a given customer within a given date range
- Show all open orders within a given date range across all customers
- See all employees recently hired
- Find all employees working in a given warehouse
- Get all items on order for a given product
- Get current inventories for a given product at all warehouses
- Get customers by account representative
- Get orders by account representative and date
- Get all items on order for a given product
- Get all employees with a given job title
- Get inventory by product and warehouse
- Get total product inventory
- Get account representatives ranked by order total and sales period
The following schema introduces the required indexes and relationships so that you can support these access patterns:
1# This "input" configures a global authorization rule to enable public access to2# all models in this schema. Learn more about authorization rules here: https://docs.amplify.aws/cli/graphql/auth3input AMPLIFY { globalAuthRule: AuthRule = { allow: public } } # FOR TESTING ONLY!4
5type Order @model {6 id: ID!7 customerID: ID! @index(name: "byCustomerByStatusByDate", sortKeyFields: ["status", "date"]) @index(name: "byCustomerByDate", sortKeyFields: ["date"])8 accountRepresentativeID: ID! @index(name: "byRepresentativebyDate", sortKeyFields: ["date"])9 productID: ID! @index(name: "byProduct", sortKeyFields: ["id"])10 status: String!11 amount: Int!12 date: String!13}14
15type Customer @model {16 id: ID!17 name: String!18 phoneNumber: String19 accountRepresentativeID: ID! @index(name: "byRepresentative", sortKeyFields: ["id"])20 ordersByDate: [Order] @hasMany(indexName: "byCustomerByDate", fields: ["id"])21 ordersByStatusDate: [Order] @hasMany(indexName: "byCustomerByStatusByDate", fields: ["id"])22}23
24type Employee @model {25 id: ID!26 name: String! @index(name: "byName", queryField: "employeeByName", sortKeyFields: ["id"])27 startDate: String!28 phoneNumber: String!29 warehouseID: ID! @index(name: "byWarehouse", sortKeyFields: ["id"])30 jobTitle: String! @index(name: "byTitle", queryField: "employeesByJobTitle", sortKeyFields: ["id"])31 newHire: String! @index(name: "newHire", queryField: "employeesNewHire", sortKeyFields: ["id"]) @index(name: "newHireByStartDate", queryField: "employeesNewHireByStartDate", sortKeyFields: ["startDate"])32}33
34type Warehouse @model {35 id: ID!36 employees: [Employee] @hasMany(indexName: "byWarehouse", fields: ["id"])37}38
39type AccountRepresentative @model {40 id: ID!41 customers: [Customer] @hasMany(indexName: "byRepresentative", fields: ["id"])42 orders: [Order] @hasMany(indexName: "byRepresentativebyDate", fields: ["id"])43 orderTotal: Int44 salesPeriod: String @index(name: "bySalesPeriodByOrderTotal", queryField: "repsByPeriodAndTotal", sortKeyFields: ["orderTotal"])45}46
47type Inventory @model {48 productID: ID! @primaryKey(sortKeyFields: ["warehouseID"])49 warehouseID: ID! @index(name: "byWarehouseID", queryField: "itemsByWarehouseID")50 inventoryAmount: Int!51}52
53type Product @model {54 id: ID!55 name: String!56 orders: [Order] @hasMany(indexName: "byProduct", fields: ["id"])57 inventories: [Inventory] @hasMany(fields: ["id"])58}
Now that you have the schema created, let's create the items in the database that you will be operating against:
1# first2mutation createWarehouse {3 createWarehouse(input: {id: "1"}) {4 id5 }6}7
8# second9mutation createEmployee {10 createEmployee(input: {11 id: "amanda"12 name: "Amanda",13 startDate: "2018-05-22",14 phoneNumber: "6015555555",15 warehouseID: "1",16 jobTitle: "Manager",17 newHire: "true"}18 ) {19 id20 jobTitle21 name22 newHire23 phoneNumber24 startDate25 warehouseID26 }27}28
29# third30mutation createAccountRepresentative {31 createAccountRepresentative(input: {32 id: "dabit"33 orderTotal: 40000034 salesPeriod: "January 2019"35 }) {36 id37 orderTotal38 salesPeriod39 }40}41
42# fourth43mutation createCustomer {44 createCustomer(input: {45 id: "jennifer_thomas"46 accountRepresentativeID: "dabit"47 name: "Jennifer Thomas"48 phoneNumber: "+16015555555"49 }) {50 id51 name52 accountRepresentativeID53 phoneNumber54 }55}56
57# fifth58mutation createProduct {59 createProduct(input: {60 id: "yeezyboost"61 name: "Yeezy Boost"62 }) {63 id64 name65 }66}67
68# sixth69mutation createInventory {70 createInventory(input: {71 productID: "yeezyboost"72 warehouseID: "1"73 inventoryAmount: 30074 }) {75 productID76 inventoryAmount77 warehouseID78 }79}80
81# seventh82mutation createOrder {83 createOrder(input: {84 amount: 30085 date: "2018-07-12"86 status: "pending"87 accountRepresentativeID: "dabit"88 customerID: "jennifer_thomas"89 productID: "yeezyboost"90 }) {91 id92 customerID93 accountRepresentativeID94 amount95 date96 customerID97 productID98 }99}
1. Look up employee details by employee ID
This can simply be done by querying the employee model with an employee ID, no @primaryKey
or @index
need to be explicitly specified to make this work.
1query getEmployee($id: ID!) {2 getEmployee(id: $id) {3 id4 name5 phoneNumber6 startDate7 jobTitle8 }9}
2. Query employee details by employee name
The @index
byName
on the Employee
type makes this access-pattern feasible because under the hood an index is created and a query is used to match against the name field. You can use this query:
1query employeeByName($name: String!) {2 employeeByName(name: $name) {3 items {4 id5 name6 phoneNumber7 startDate8 jobTitle9 }10 }11}
3. Find an Employee’s phone number
Either one of the previous queries would work to find an employee’s phone number as long as one has their ID or name.
4. Find a customer’s phone number
A similar query to those given above but on the Customer model would give you a customer’s phone number.
1query getCustomer($customerID: ID!) {2 getCustomer(id: $customerID) {3 phoneNumber4 }5}
5. Get orders for a given customer within a given date range
There is a one-to-many relation that lets all the orders of a customer be queried.
This relationship is created by having the @index
name byCustomerByDate
on the Order model that is queried by the @hasMany
relationship on the orders field of the Customer model.
A sort key with the date is used. What this means is that the GraphQL resolver can use predicates like Between
to efficiently search the date range rather than scanning all records in the database and then filtering them out.
The query one would need to get the orders to a customer within a date range would be:
1query getCustomerWithOrdersByDate($customerID: ID!) {2 getCustomer(id: $customerID) {3 ordersByDate(date: {4 between: [ "2018-01-22", "2020-10-11" ]5 }) {6 items {7 id8 amount9 productID10 }11 }12 }13}
6. Show all open orders within a given date range across all customers
The @index
byCustomerByStatusByDate
enables you to run a query that would work for this access pattern.
In this example, a composite sort key (combination of two or more keys) with the status
and date
is used. What this means is that the unique identifier of a record in the database is created by concatenating these two fields (status and date) together, and then the GraphQL resolver can use predicates like between
or contains
to efficiently search the unique identifier for matches rather than scanning all records in the database and then filtering them out.
1query listCustomersWithOrdersByStatusDate {2 listCustomers {3 items {4 ordersByStatusDate(statusDate: {5 between: [6 { status: "pending", date: "2018-01-22" },7 { status: "pending", date: "2020-10-11" }8 ]}) {9 items {10 id11 amount12 date13 }14 }15 }16 }17}
7. See all employees hired recently
Having @index(name: "newHire", fields: ["newHire", "id"])
on the Employee
model allows one to query by whether an employee has been hired recently.
1query employeesNewHire {2 employeesNewHire(newHire: "true") {3 items {4 id5 name6 phoneNumber7 startDate8 jobTitle9 }10 }11}
You can also query and have the results returned by start date by using the employeesNewHireByStartDate
query:
1query employeesNewHireByDate {2 employeesNewHireByStartDate(newHire: "true") {3 items {4 id5 name6 phoneNumber7 startDate8 jobTitle9 }10 }11}
8. Find all employees working in a given warehouse
This needs a one to many relationship from warehouses to employees. As can be seen from the @hasMany
relationship in the Warehouse
model, this relationship uses the byWarehouse
index on the Employee
model. The relevant query would look like this:
1query getWarehouse($warehouseID: ID!) {2 getWarehouse(id: $warehouseID) {3 id4 employees{5 items {6 id7 name8 startDate9 phoneNumber10 jobTitle11 }12 }13 }14}
9. Get all items on order for a given product
This access-pattern would use a one-to-many relation from products to orders. With this query you can get all orders of a given product:
1query getProductOrders($productID: ID!) {2 getProduct(id: $productID) {3 id4 orders {5 items {6 id7 status8 amount9 date10 }11 }12 }13}
10. Get current inventories for a product at all warehouses
The query needed to get the inventories of a product in all warehouses would be:
1query getProductInventoryInfo($productID: ID!) {2 getProduct(id: $productID) {3 id4 inventories {5 items {6 warehouseID7 inventoryAmount8 }9 }10 }11}
11. Get customers by account representative
This uses a has-many relationship between account representatives and customers:
The query needed would look like this:
1query getCustomersForAccountRepresentative($representativeId: ID!) {2 getAccountRepresentative(id: $representativeId) {3 customers {4 items {5 id6 name7 phoneNumber8 }9 }10 }11}
12. Get orders by account representative and date
As can be seen in the AccountRepresentative model this relationship uses the byRepresentativebyDate
field on the Order
model to create the connection needed. The query needed would look like this:
1query getOrdersForAccountRepresentative($representativeId: ID!) {2 getAccountRepresentative(id: $representativeId) {3 id4 orders(date: {5 between: [6 "2010-01-22", "2020-10-11"7 ]8 }) {9 items {10 id11 status12 amount13 date14 }15 }16 }17}
13. Get all items on order for a given product
This is the same as number 9.
14. Get all employees with a given job title
Using the byTitle
@index
makes this access pattern quite easy.
1query employeesByJobTitle {2 employeesByJobTitle(jobTitle: "Manager") {3 items {4 id5 name6 phoneNumber7 jobTitle8 }9 }10}
15. Get inventory by product by warehouse
Here having the inventories be held in a separate model is particularly useful since this model can have its own partition key and sort key such that the inventories themselves can be queried as is needed for this access-pattern.
A query on this model would look like this:
1query inventoryByProductAndWarehouse($productID: ID!, $warehouseID: ID!) {2 getInventory(productID: $productID, warehouseID: $warehouseID) {3 productID4 warehouseID5 inventoryAmount6 }7}
You can also get all inventory from an individual warehouse by using the itemsByWarehouseID
query created by the byWarehouseID
key:
1query byWarehouseId($warehouseID: ID!) {2 itemsByWarehouseID(warehouseID: $warehouseID) {3 items {4 inventoryAmount5 productID6 }7 }8}
16. Get total product inventory
How this would be done depends on the use case. If one just wants a list of all inventories in all warehouses, one could just run a list inventories on the Inventory model:
1query listInventorys {2 listInventorys {3 items {4 productID5 warehouseID6 inventoryAmount7 }8 }9}
17. Get sales representatives ranked by order total and sales period
The sales period is either a date range or maybe even a month or week. Therefore you can set the sales period as a string and query using the combination of salesPeriod
and orderTotal
. You can also set the sortDirection
in order to get the return values from largest to smallest:
1query repsByPeriodAndTotal {2 repsByPeriodAndTotal(3 sortDirection: DESC,4 salesPeriod: "January 2019",5 orderTotal: {6 ge: 10007 }) {8 items {9 id10 orderTotal11 }12 }13}