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

dabit3/lambda-graphql-resolver-examples: Examples of using AWS Lambda Functions ...

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

开源软件名称(OpenSource Name):

dabit3/lambda-graphql-resolver-examples

开源软件地址(OpenSource Url):

https://github.com/dabit3/lambda-graphql-resolver-examples

开源编程语言(OpenSource Language):

JavaScript 78.6%

开源软件介绍(OpenSource Introduction):

AWS AppSync - Lambda GraphQL Resolver Examples

Resources in this project

~ amplify status

Current Environment: local

| Category | Resource name    | Operation | Provider plugin   |
| -------- | ---------------- | --------- | ----------------- |
| Storage  | currencytable    | No Change | awscloudformation |
| Function | currencyfunction | No Change | awscloudformation |
| Api      | gqllambdacrypto  | No Change | awscloudformation |

API - AWS AppSync (GraphQL)

Schema

This schema has 1 main type (Coin) as well as Query and a Mutation operation to interact with the type. The resolver for these operations is the Lambda function (currencyfunction).

type Coin {
  id: String!
  name: String!
  symbol: String!
  price_usd: String!
}

type Query {
  getCoins(limit: Int start: Int): [Coin] @function(name: "currencyfunction-${env}")
}

type Mutation {
  createCoin(name: String! symbol: String! price_usd: String!): Coin @function(name: "currencyfunction-${env}")
}

Function - AWS Lambda

The Function has two main features:

  1. Fetch from a REST API and return the results.

  2. Interact with a DynamoDB Table (putItem and Scan)

index.js

// index.js
const axios = require('axios')

const getCoins = require('./getCoins')
const createCoin = require('./createCoin')

exports.handler = function (event, _, callback) {
  // uncomment to invoke DynamoDB with putItem or Scan
  // if (event.typeName === 'Mutation') {
  //   createCoin(event, callback)
  // }
  // if (event.typeName === 'Query') {
  //   getCoins(callback)
  // }
  
  // call another API and return the response (query only)
  let apiUrl = `https://api.coinlore.com/api/tickers/?start=1&limit=10`

  if (event.arguments) { 
    const { start = 0, limit = 10 } = event.arguments
    apiUrl = `https://api.coinlore.com/api/tickers/?start=${start}&limit=${limit}`
  }
  
  axios.get(apiUrl)
    .then(response => callback(null, response.data.data))
    .catch(err => callback(err))
}

getCoins.js

// getCoins.js
const AWS = require('aws-sdk')
const region = process.env.REGION
const storageCurrencytableName = process.env.STORAGE_CURRENCYTABLE_NAME
const docClient = new AWS.DynamoDB.DocumentClient({region})

const params = {
  TableName: storageCurrencytableName
}

function getCoins(callback) {
  docClient.scan(params, function(err, data) {
    if (err) {
      callback(err)
    } else {
      callback(null, data.Items)
    }
  });
}

module.exports = getCoins

createCoin.js

// createCoin.js
const AWS = require('aws-sdk')
const uuid = require('uuid/v4')
const region = process.env.REGION
const ddb_table_name = process.env.STORAGE_CURRENCYTABLE_NAME
const docClient = new AWS.DynamoDB.DocumentClient({region})

function write(params, event, callback){
  docClient.put(params, function(err, data) {
    if (err) {
      callback(err)
    } else {
      callback(null, event.arguments)
    }
  })
}

function createCoin(event, callback) {
  const args = { ...event.arguments, id: uuid() }
  var params = {
    TableName: ddb_table_name,
    Item: args
  };
  
  if (Object.keys(event.arguments).length > 0) {
    write(params, event, callback)
  } 
}

module.exports = createCoin

Storage - Amazon DynamoDB

This table has the following properties:

  • id
  • name
  • symbol
  • price_usd

Deploy this app

To deploy this project, you can do one of the following:

1. Use the AWS Amplify 1-click deploy button

amplifybutton

2. Deploy from your local machine

  1. Clone the repo
git clone https://github.com/dabit3/lambda-graphql-resolver-examples.git

cd lambda-graphql-resolver-examples
  1. Install dependencies
npm install
  1. Initialize new Amplify repository
amplify init
  1. Deploy
amplify push



鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
flatlogic/react-dashboard: 发布时间:2022-06-22
下一篇:
juunegreiros/agenda-petshop: Projeto para curso de nodeJS e GraphQL发布时间:2022-06-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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