Congratulations! Welcome to Lambda Sharp, LLC! A fictitious company that you just started working at! AppSync will be the focus of your work.
SUBJECT: Data project that must be completed!
FROM: Dave aka DBA <mailto:[email protected]>
We just acquirered Northwind Traders! We need an API ASAP and your the only developer available right now. I've created a partial GraphQL schema and a (not so pretty) mock datasource lambda function to start querying the data. We need this in production TONIGHT!
Thanks,
Dave aka "The DBA"
- Create a new lambda function from this repo. northwindTradersMockDb.js
- Navigate to your Lambda Console and click Create function.
- Keep the default Author from scratch selected.
- Name your function.
- Use the Runtime (NodeJs 10.x).
- Set a timeout of 10 seconds.
- Select the role you created from the Existing Role dropdown.
- Click Create function to create function
- Create a new api in AppSync
- Click Build from Scratch option with the name HackathonApp.
- Click Edit Schema. Copy/paste schema.graphql contents in to the Schema window. Save.
- Create new Data Source in AppSync
- Data source name: NorthwindTraders.
- Data source type: AWS Lambda Function.
- Region: sames as the lambda function created previously (likely US-EAST-1)
- Data source: Reference the lambda function created previously.
- Use New role option.
Helpful hint: Be sure you can access the CloudWatch trail events for this lambda function.
AWS Resources
- Add a resolver to get all customers. In Schema, right pane (no scroll bar but it will scroll) >
Query>allCustomers. Use the lambda functionNorthwindTraders> Click Attach. Add the request mapping template.
{
"version": "2017-02-28",
"operation": "Invoke",
"payload": {
"field": "allCustomers"
}
}- Add a resolver to get a single customer. In Schema, right pane (no scroll bar but it will scroll) >
Query>getCustomer. Use the lambda functionNorthwindTraders> Click Attach. Add the request mapping template.
{
"version": "2017-02-28",
"operation": "Invoke",
"payload": {
"field": "getCustomer",
"arguments": $util.toJson($context.arguments)
}
}- In
Queries, query for customers.
query {
allCustomers {
CustomerID
ContactName
}
}- Get a single customer order using
getCustomer
query {
getCustomer(id: "ADD A CUSTOMER ID HERE") {
__typename
ContactName
ContactTitle
Country
}
}- Add a resolver to get orders of a single customer. In Schema, right pane (no scroll bar but it will scroll) >
Customer>getCustomerOrders. Use the lambda functionNorthwindTraders> Click Attach. Add the request mapping template.
{
"version": "2017-02-28",
"operation": "Invoke",
"payload": {
"field": "getCustomerOrders",
"source": $utils.toJson($context.source)
}
}- Get a single customer's orders
query {
getCustomer(id: "ANTON") {
ContactName
getCustomerOrders {
# lambda invocation
__typename
OrderID
}
}
}Based on the previous levels, build the following:
- Add Employee type in the schema.
- Add a resolver. data source field
getEmployeecan be resolved. - An Employee type is connected to an Order type.
- Construct a query that will show the employee's name on a customer's order.
Example of employee data
{
"EmployeeID": 9,
"LastName": "Dodsworth",
"FirstName": "Anne",
"Title": "Sales Representative",
"TitleOfCourtesy": "Ms.",
"BirthDate": "1966-01-27 00:00:00.000",
"HireDate": "1994-11-15 00:00:00.000",
"Address": "7 Houndstooth Rd.",
"City": "London",
"Region": "NULL",
"PostalCode": "WG2 7LT",
"Country": "UK",
"HomePhone": "(71) 555-4444",
"Extension": 452,
"Photo": "",
"Notes": "Anne has a BA degree in English from St. Lawrence College. She is fluent in French and German.",
"ReportsTo": 5,
"PhotoPath": "http://accweb/emmployees/davolio.bmp"
}Helpful Resources
* [AWS Resource](https://docs.aws.amazon.com/appsync/latest/devguide/designing-your-schema.html#adding-a-mutation-type) * [Quick start writing queries](https://docs.aws.amazon.com/appsync/latest/devguide/quickstart-write-queries.html)- Add a mutation type for updating an order.
- Resolve to lambda function method
updateOrder - Write a mutation in the query window to update the
ShippedDatefield.
type Mutation {
updateOrder(OrderID: Int, ShippedDate: String): Order
}Add mutation: Mutation to the type Schema. Construct an update order in the Queries. This resource is helpful: AWS Run Queries and Mutations
You know you did it right when you send a new ShippedDate and returns with the order object as expected.
- Add another data source for AppSync (DynamoDB is a good option).
- Extend a type in the schema to use the new data source.
- Build a query and show us customer data with new information!
Suggested Data Sources
Additional Resources
Show us your Schema and run some queries in front of us!
