/appsync-lambda-golang

A sample AWS AppSync GraphQL API leveraging lambda functions as resolvers.

Primary LanguageGo

appsync-lambda-golang

A sample AWS AppSync GraphQL API leveraging lambda functions as resolvers.

Why?

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.

GraphQL Schema

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!
}

Resolvers

type field lambda mode
Mutation createItem CreateItemFunction invoke
Query item ReadItemFunction invoke
Query items ListItemsFunction batch invoke

Lambda Functions

CreateItemFunction

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

ReadItemFunction

Resolver for item query. Returns responses.Item.

⛔️ Note: Currently returns a not implemented error.

meta value
runtime go1.x
uri cmd/read-item/main.go
handler internal/resolvers/read_item.go

ListItemsFunction

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.

⛔️ Note: Currently returns a not implemented error.

meta value
runtime go1.x
uri cmd/list-items/main.go
handler internal/resolvers/list_items.go

Building

sam build

Deploying

Initial: sam deploy --guided

Subsequent: sam deploy