在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(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 ExamplesResources 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)SchemaThis schema has 1 main type ( 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 LambdaThe Function has two main features:
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 DynamoDBThis table has the following properties:
Deploy this appTo deploy this project, you can do one of the following: 1. Use the AWS Amplify 1-click deploy button2. Deploy from your local machine
git clone https://github.com/dabit3/lambda-graphql-resolver-examples.git
cd lambda-graphql-resolver-examples
npm install
amplify init
amplify push |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论