• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

boazdejong/serverless-graphql-api: Serverless GraphQL API using Lambda and Dynam ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

boazdejong/serverless-graphql-api

开源软件地址(OpenSource Url):

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

开源编程语言(OpenSource Language):

JavaScript 100.0%

开源软件介绍(OpenSource Introduction):

banner

Serverless GraphQL API using Lambda and DynamoDB

serverless Build Status

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

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. 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.

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.

mutation {
  createSong(artist: "99a746e0-0734-11e7-b2fd-45ae0a3b9074", title: "Whatever", duration: 120) {
    id
  }
}

updateArtist()

mutation {
  updateArtist(id: "99a746e0-0734-11e7-b2fd-45ae0a3b9074", first_name: "John", last_name: "Ruth") {
    id
    first_name
    last_name
  }
}

updateSong()

mutation {
  updateSong(id: "a8a0a060-071b-11e7-bd09-8562f101f7c2", artist: "99a746e0-0734-11e7-b2fd-45ae0a3b9074", duration: 130, title: "A new title") {
    id
  }
}

Queries

Example query

{
  songs {
    id
    title
    duration
    artist {
      id
      first_name
      last_name
    }
  }
}

This query will return a result similar to this

{
  "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



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap