A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.amplify.aws/gen1/react/build-a-backend/functions/graphql-from-lambda/ below:

Website Navigation


Call a GraphQL API from a Lambda function - React

Call a GraphQL API from a Lambda function

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:

Query

Using an API Key for authenticating your requests, you can query the GraphQL API to get a list of all Todos. To paginate over the list queries, you need to pass in a limit and nextToken on the listTodos query. See more at GraphQL pagination .

Mutation

In this example you will create a mutation showing how to pass in variables as arguments to create a Todo record.

IAM Authorization

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.

CommonJS

When 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