项目作者: abachuk

项目描述 :
GraphQL API Boilerplate using serverless, AWS Lambda, Cognito and DynamoDB
高级语言: JavaScript
项目地址: git://github.com/abachuk/serverless-graphql-api.git
创建时间: 2017-10-31T19:11:59Z
项目社区:https://github.com/abachuk/serverless-graphql-api

开源协议:

下载


serverless-graphql-api

Inspired by https://github.com/boazdejong/serverless-graphql-api

Serverless GraphQL API using Lambda and DynamoDB

serverless

GraphQL Lambda Server using graphql-server-lambda from Apollo.

graphql-tools and merge-graphql-schemas are used to generate the schema.

serverless-webpack is used to transform ES6 with Babel and build the lambda.

Setup

  1. Install serverless npm install -g serverless
  2. Configure AWS provider
    a. Login to AWS console
    b. Navigate to IAM
    c. Create new user (or use existing one). Get access key and secret key
    d. Navigate back to your local terminal serverless config credentials --provider aws --key 1234 --secret 5678 --profile serverless-demo
  3. Clone the repository and install the packages.
    1. git clone https://github.com/abachuk/serverless-graphql-api
    2. cd serverless-graphql-api
    3. npm install

Deploy

Run the deploy script to create the Lambda Function and API Gateway for GraphQL. This also creates two DynamoDB tables named authors and posts

  1. npm run deploy

Queries and Mutations

Query the GraphQL server using the GraphiQL.app. If you have Homebrew installed on OSX run

  1. brew cask install graphiql

Mutations

The following mutations are available in this example.

createAuthor()

Create an author providing the first and last name as arguments. The id will be a generated uuid.

  1. mutation {
  2. createAuthor(first_name: "Agent", last_name: "Smith") {
  3. id
  4. }
  5. }

createPost()

Using the generated id from the author you can create a post with the following mutation. Also provide a title and duration.

  1. mutation {
  2. createPost(post: "34b236e0-0734-54e7-b2cd-45ue6a3b9071", title: "Whatever", body: "my long and very interesting post") {
  3. id
  4. }
  5. }

updateAuthor()

  1. mutation {
  2. updateAuthor(id: "34b236e0-0734-54e7-b2cd-45ue6a3b9071", first_name: "Agent", last_name: "Jones") {
  3. id
  4. first_name
  5. last_name
  6. }
  7. }

updatePost()

  1. mutation {
  2. updatePost(id: "w1a0a060-091b-11e7-bd09-1092f101s7c0", author: "34b236e0-0734-54e7-b2cd-45ue6a3b9071", body:
  3. "even more interesting post", title: "A new title") {
  4. id
  5. }
  6. }

Queries

Example query

  1. {
  2. posts {
  3. id
  4. title
  5. body
  6. author {
  7. id
  8. first_name
  9. last_name
  10. }
  11. }
  12. }

This query will return a result similar to this

  1. {
  2. "data": {
  3. "posts": [
  4. {
  5. "id": "w1a0a055-091b-11e7-bd09-1092f101s7c1",
  6. "title": "Whatever",
  7. "body": "super duper cool blog post",
  8. "author": {
  9. "id": "99a746e0-0734-11e7-b2fd-45ae0a3b9074",
  10. "first_name": "Agent",
  11. "last_name": "Brown"
  12. }
  13. }
  14. ]
  15. }
  16. }

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:

  1. record:
  2. handler: lib/handler.record
  3. events:
  4. - stream:
  5. type: dynamodb
  6. arn:
  7. Fn::GetAtt:
  8. - PostsDynamoDbTable
  9. - StreamArn
  10. batchSize: 1
  11. - stream:
  12. type: dynamodb
  13. arn:
  14. Fn::GetAtt:
  15. - PostsDynamoDbTable
  16. - StreamArn
  17. batchSize: 1

The stream is enabled when defining the DynamoDB table in the serverless.yml resources.

  1. StreamSpecification:
  2. StreamViewType: NEW_AND_OLD_IMAGES