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/graphqlapi/connect-from-server-runtime/ below:

Website Navigation


Connect to data from server-side runtimes - React

Connect to data from server-side runtimes

This guide walks through how you can connect to the Amplify GraphQL API from any server-side runtimes. For Next.js applications, Amplify provides first-class support for App Router (React Server Components, Route Handlers, and Server Actions) and Pages Router (Components, API Routes), and Middleware. Review Connect to GraphQL API from AWS Lambda if need to call the Amplify GraphQL API from a Node.js AWS Lambda function.

Connect to GraphQL API from a Next.js server runtime

Before you begin, you will need:

Step 1 - Choose the correct GraphQL API client for Next.js server runtimes

Amplify offers two specialized GraphQL API clients for Next.js server runtimes (from @aws-amplify/adapter-nextjs/api) that you should use depending whether you retrieve the user tokens using cookies or NextRequest and NextResponse:

Choose the correct GraphQL API client based on your Next.js Router (App or Pages) and then their use case:

Step 2 - Generate the GraphQL API client for Next.js server runtimes

To generate a GraphQL API client for the Next.js server runtime using cookies, you need to provide both your Amplify configuration and the cookies function from Next.js.

We recommend to generate the server API client in a utility file. Then, import the generated client in your Next.js React Server Components, Server Actions, or Route Handlers.

To generate a GraphQL API client for the Next.js server runtime using NextRequest and NextResponse, you need to only provide your Amplify configuration. When making the individual API requests, you'll need to pass it into an runWithAmplifyServerContext function to pass in the cookies from request and response variables.

We recommend to generate the server API client in a utility file. Then, import the generated client in your Next.js Middleware, component's server runtime code, and API Routes.

Step 3 - Call GraphQL API using generated server API clients

With the generated server API clients, you can make any GraphQL query or mutation request. Subscriptions are not available within server runtimes.

Import the cookie-based GraphQL API's server client in your Next.js React Server Component code and make your GraphQL.

Import the NextRequest/NextResponse-based GraphQL API's server client in your Next.js server runtime code and make your GraphQL within the runWithAmplifyServerContext function. Review Server-side Rendering to learn more about creating an Amplify server context.

For example, in a Next.js Pages Router API route, use the req and res parameters from the handler function with runWithAmplifyServerContext:

Connect to GraphQL API from AWS Lambda

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

(TK authorization rules from Lambda)

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