You can call an AppSync GraphQL API from a Node.js app or a Lambda function. Take a basic Todo
app as an example:
This API will have operations available for Query
, Mutation
, and Subscription
. Let's take a look at how to perform both a query as well as a mutation from a Lambda function using Node.js.
First, create a Lambda function with amplify add function
and choose the AppSync - GraphQL API request (with IAM)
to get started. Be sure to grant access to your GraphQL API when prompted by the CLI to grant access to other resources in the project. Alternatively, you can create the function from scratch.
? Select which capability you want to add: Lambda function (serverless function)
? Provide an AWS Lambda function name: myfunction
? Choose the runtime that you want to use: NodeJS
? Choose the function template that you want to use: AppSync - GraphQL API request (with IAM)
Available advanced settings:
- Resource access permissions
- Scheduled recurring invocation
- Lambda layers configuration
- Environment variables configuration
- Secret values configuration
? Do you want to configure advanced settings? Yes
? Do you want to access other resources in this project from your Lambda function? Yes
? Select the categories you want this function to have access to. api
? Select the operations you want to permit on <YOUR_API_NAME> Query, Mutation, Subscription
You can access the following resource attributes as environment variables from your Lambda function
API_<YOUR_API_NAME>_GRAPHQLAPIENDPOINTOUTPUT
API_<YOUR_API_NAME>_GRAPHQLAPIIDOUTPUT
API_<YOUR_API_NAME>_GRAPHQLAPIKEYOUTPUT
The function can only be added when the GraphQL API with IAM authorization exists.
Create from scratch? Select which capability you want to add: Lambda function (serverless function)
? Provide an AWS Lambda function name: myfunction
? Choose the runtime that you want to use: NodeJS
? Choose the function template that you want to use: Hello World
Available advanced settings:
- Resource access permissions
- Scheduled recurring invocation
- Lambda layers configuration
- Environment variables configuration
- Secret values configuration
? Do you want to configure advanced settings? Yes
? Do you want to access other resources in this project from your Lambda function? Yes
? Select the categories you want this function to have access to. api
? Select the operations you want to permit on <YOUR_API_NAME> Query, Mutation, Subscription
You can access the following resource attributes as environment variables from your Lambda function
API_<YOUR_API_NAME>_GRAPHQLAPIENDPOINTOUTPUT
API_<YOUR_API_NAME>_GRAPHQLAPIIDOUTPUT
API_<YOUR_API_NAME>_GRAPHQLAPIKEYOUTPUT
The examples on this page use node-fetch
to make a HTTP request to your GraphQL API. When the Node.js v18 runtime is released for Lambda this dependency can be removed in favor of native fetch
To get started, add the node-fetch
module as a dependency:
CommonJS:
For functions written using CommonJS, you will need to install version 2 of node-fetch
ESM:
QueryUsing an API Key for authenticating your requests, you can query the GraphQL API to get a list of all Todo
s. To paginate over the list queries, you need to pass in a limit
and nextToken
on the listTodos
query. See more at GraphQL pagination .
In this example you will create a mutation showing how to pass in variables as arguments to create a Todo
record.
Let's take a look at another example schema that uses iam
authorization.
The CLI will automatically configure the Lambda execution IAM role to call the GraphQL API. Before writing your Lambda function you will first need to install the appropriate AWS SDK v3 dependencies:
Then, the following example will sign the request to call the GraphQL API using IAM authorization.
CommonJSWhen writing functions with CommonJS, you will need to install version 2 of node-fetch
:
Similar to the example above you can now write your handler. The difference here is the use of require()
rather than import ... from
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4