A sample AWS AppSync GraphQL API leveraging lambda functions as resolvers.
GraphQL (when implemented correctly) is a fantastic way to design highly expressive API's.
With a bit of additional configuration, AWS AppSync allows us to define Lambda functions as resolvers, unlocking a new level of customization and scalability.
Using this approach we can independently develop, deploy, and scale any resolver in our API, giving us a huge amount of flexibility in how we design and maintain our system.
And AWS AppSync allows us to perform regular and batch invocation of lambda resolvers, allowing us to write lambda functions that avoid the N + 1 query problem.
This sample GraphQL API has the following schema.
input CreateItemInput {
name: String!
}
type Item {
name: String!
}
type CreateItemPayload {
item: Item!
}
type Query {
item(id: ID!): Item
items: [Item!]!
}
type Mutation {
createItem(input: CreateItemInput!): CreateItemPayload!
}
type | field | lambda | mode |
---|---|---|---|
Mutation | createItem |
CreateItemFunction | invoke |
Query | item |
ReadItemFunction | invoke |
Query | items |
ListItemsFunction | batch invoke |
Resolver for createItem
mutation. Returns responses.CreateItem
using the Name
from requests.CreateItem
.
meta | value |
---|---|
runtime | go1.x |
uri | cmd/create-item/main.go |
handler | internal/resolvers/create_item.go |
Resolver for item
query. Returns responses.Item
.
meta | value |
---|---|
runtime | go1.x |
uri | cmd/read-item/main.go |
handler | internal/resolvers/read_item.go |
Resolver for items
query. Returns responses.Item[]
. This handler is invoked as a batch invoke and operates in a similar fashion to a data loader.
meta | value |
---|---|
runtime | go1.x |
uri | cmd/list-items/main.go |
handler | internal/resolvers/list_items.go |
sam build
Initial: sam deploy --guided
Subsequent: sam deploy