https://github.com/lk321/sls-graphql
https://github.com/lk321/sls-graphql
aws dynamodb serverless
Last synced: 12 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/lk321/sls-graphql
- Owner: lk321
- Created: 2021-09-20T22:26:28.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-09-20T22:29:43.000Z (over 4 years ago)
- Last Synced: 2025-02-10T05:28:28.251Z (about 1 year ago)
- Topics: aws, dynamodb, serverless
- Language: JavaScript
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

# Serverless GraphQL API using Lambda and DynamoDB
[](http://www.serverless.com)
[](https://travis-ci.org/boazdejong/serverless-graphql-api)
GraphQL Lambda Server using [graphql-server-lambda](https://github.com/apollographql/graphql-server/tree/master/packages/graphql-server-lambda) from [Apollo](http://dev.apollodata.com/).
[graphql-tools](https://github.com/apollographql/graphql-tools) and [merge-graphql-schemas](https://github.com/okgrow/merge-graphql-schemas) are used to generate the schema.
[serverless-webpack](https://github.com/elastic-coders/serverless-webpack) is used to transform ES6 with [Babel](https://babeljs.io/) and build the lambda.
## Setup
Clone the repository and install the packages.
```
git clone https://github.com/boazdejong/serverless-graphql-api
cd serverless-graphql-api
npm install
```
## Deploy
Run the `deploy` script to create the Lambda Function and API Gateway for GraphQL. This also creates two DynamoDB tables named `artists` and `songs`
```
npm run deploy
```
## Queries and Mutations
Query the GraphQL server using the [GraphiQL.app](https://github.com/skevy/graphiql-app). If you have Homebrew installed on OSX run
```
brew cask install graphiql
```
### Mutations
The following mutations are available in this example.
#### createArtist()
Create an artist providing the first and last name as arguments. The id will be a generated uuid.
```graphql
mutation {
createArtist(first_name: "Billy", last_name: "Crash") {
id
}
}
```
#### createSong()
Using the generated id from the artist you can create a song with the following mutation. Also provide a title and duration.
```graphql
mutation {
createSong(artist: "99a746e0-0734-11e7-b2fd-45ae0a3b9074", title: "Whatever", duration: 120) {
id
}
}
```
#### updateArtist()
```graphql
mutation {
updateArtist(id: "99a746e0-0734-11e7-b2fd-45ae0a3b9074", first_name: "John", last_name: "Ruth") {
id
first_name
last_name
}
}
```
#### updateSong()
```graphql
mutation {
updateSong(id: "a8a0a060-071b-11e7-bd09-8562f101f7c2", artist: "99a746e0-0734-11e7-b2fd-45ae0a3b9074", duration: 130, title: "A new title") {
id
}
}
```
### Queries
#### Example query
```graphql
{
songs {
id
title
duration
artist {
id
first_name
last_name
}
}
}
```
This query will return a result similar to this
```json
{
"data": {
"songs": [
{
"id": "a8a0a060-071b-11e7-bd09-8562f101f7c2",
"title": "Whatever",
"duration": 120,
"artist": {
"id": "99a746e0-0734-11e7-b2fd-45ae0a3b9074",
"first_name": "Billy",
"last_name": "Crash"
}
}
]
}
}
```
## DynamoDB Streams
This project also includes an example of capturing table activity with DynamoDB Streams.
The `record` lambda function is triggered by two stream events. One for each table.
In `serverless.yml`:
```
record:
handler: lib/handler.record
events:
- stream:
type: dynamodb
arn:
Fn::GetAtt:
- ArtistsDynamoDbTable
- StreamArn
batchSize: 1
- stream:
type: dynamodb
arn:
Fn::GetAtt:
- SongsDynamoDbTable
- StreamArn
batchSize: 1
```
The stream is enabled when defining the DynamoDB table in the `serverless.yml` resources.
```
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
```