Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dabit3/lambda-graphql-resolver-examples
Examples of using AWS Lambda Functions as GraphQL Resolvers with the Amplify CLI
https://github.com/dabit3/lambda-graphql-resolver-examples
aws-amplify aws-appsync dynamodb graphql react serverless
Last synced: 6 days ago
JSON representation
Examples of using AWS Lambda Functions as GraphQL Resolvers with the Amplify CLI
- Host: GitHub
- URL: https://github.com/dabit3/lambda-graphql-resolver-examples
- Owner: dabit3
- Created: 2019-06-10T16:46:35.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-16T00:59:52.000Z (over 3 years ago)
- Last Synced: 2023-08-03T20:13:02.419Z (over 1 year ago)
- Topics: aws-amplify, aws-appsync, dynamodb, graphql, react, serverless
- Language: JavaScript
- Size: 1.07 MB
- Stars: 33
- Watchers: 1
- Forks: 25
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AWS AppSync - Lambda GraphQL Resolver Examples
## Resources in this project
```sh
~ amplify statusCurrent 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`).
```graphql
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
```javascript
// 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
```javascript
// 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
```javascript
// 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](https://oneclick.amplifyapp.com/button.svg)](https://console.aws.amazon.com/amplify/home#/deploy?repo=https://github.com/dabit3/lambda-graphql-resolver-examples)
### 2. Deploy from your local machine
1. Clone the repo
```sh
git clone https://github.com/dabit3/lambda-graphql-resolver-examples.gitcd lambda-graphql-resolver-examples
```2. Install dependencies
```sh
npm install
```3. Initialize new Amplify repository
```sh
amplify init
```4. Deploy
```sh
amplify push
```