Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/iwatakeshi/aws-lambda-appsync
Utilities to simplify GraphQL operations in a lambda function for Appsync
https://github.com/iwatakeshi/aws-lambda-appsync
Last synced: 6 days ago
JSON representation
Utilities to simplify GraphQL operations in a lambda function for Appsync
- Host: GitHub
- URL: https://github.com/iwatakeshi/aws-lambda-appsync
- Owner: iwatakeshi
- License: apache-2.0
- Created: 2020-02-19T23:50:43.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:33:58.000Z (about 2 years ago)
- Last Synced: 2024-12-22T00:36:08.656Z (16 days ago)
- Language: TypeScript
- Size: 551 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# aws-lambda-appsync
Utilities to simplify GraphQL operations in a Lambda function for AppsyncCurrently, there are two ways provided that will allow you to use AppSync in a Lambda function.
The implementations are based on two blog posts:
* [Using Cognito User Pool](https://blog.floom.app/post/aws-appsync-with-lambda)
* [Using IAM](https://docs.aws.amazon.com/appsync/latest/devguide/building-a-client-app-node.html)**Note**:
There will be tests that need to be done. However, this library simply wraps existing libraries to reduce the boilerplate required to perform GraphQL operations. Thus, please use this library with caution if you plan to use in production (until I can get the tests done).
## Usage
```bash
# Yarn
yarn add @iwatakeshi/aws-lambda-appsync# NPM
npm install --save @iwatakeshi/aws-lambda-appsync
``````ts
import { createIAMAppSyncClient, createCognitoUserPoolAppSyncClient } from '@iwatakeshi/aws-lambda-appsync'// Create a client using IAM
const client = createIAMAppSyncClient({
url: '...',
region: process.env.REGION,
credentials: {
accessKeyId: process.env.ACCESS_KEY_ID,
secretAccessKey: process.env.SECRET_ACCESS_KEY
}
})// Create a client using Cognito User Pool
const client = createCognitoUserPoolAppSyncClient({
url: '...',
region: process.env.REGION,
clientId: process.env.CLIENT_ID,
userPoolId: process.env.USER_POOL_ID,
authParameters: {
username: process.env.ADMIN_USERNAME,
password: process.env.ADMIN_PASSWORD
},
auth: {
// Optional, but can be overridden,
jwtToken: (provider, options) => {
let credentials, expired = new Date('01-01-1970')
return async () => {
// Check if we already have credentials or if credentials are expired
if (!credentials || expired < new Date()) {
// Get new credentials (import 'getCredentials' from @iwatakeshi/aws-lambda-appsync/lib/utils/cognito)
credentials = await getCredentials(provider, options)
// ...
}return credentials?.AuthenticationResult?.IdToken
}
}
}
})```