diff --git a/src/pages/[platform]/start/getting-started/data-model/index.mdx b/src/pages/[platform]/start/getting-started/data-model/index.mdx index b0ab0a5dbbf..2514b878f7c 100644 --- a/src/pages/[platform]/start/getting-started/data-model/index.mdx +++ b/src/pages/[platform]/start/getting-started/data-model/index.mdx @@ -1158,28 +1158,130 @@ Now if you wish to subscribe to data, import the `onCreateTodo` subscription and In this section you will create a way to list and create todos from the Next.js application. To do this, you will fetch & render the latest todos from the server as well as create a new todo. -Open **app/page.tsx** and replace it with the following code: +### Generate the Amplify GraphQL API client -```jsx -// app/page.tsx +To make any GraphQL API requests service-side, we need to first generate an API client that we can use on server-side. To generate a new API client, import `generateServerClientUsingCookies` from `@aws-amplify/adapter-nextjs/api` and use it to generate a cookiesClient using the **amplifyconfiguration.json** in our project as `config` and `cookies` from `next/headers`. + + + +Amplify offers two API clients for Next.js server-side runtimes. Use `generateServerClientUsingCookies` primarily for use cases where `cookies` from `next/headers` is available, such as in App Router's React Server Components, Server Actions. Use `generateServerClientUsingReqRes` for use cases where a `NextRequest`/`NextResponse` are available, such as in the Pages Router or Middleware. Review [Connect to data from server-side runtimes](/[platform]/build-a-backend/graphqlapi/connect-from-server-runtime/) to review in-depth which API client to use for which use cases. + + + +Open **src/app/page.tsx** and replace it with the following code: + +```tsx import { generateServerClientUsingCookies } from '@aws-amplify/adapter-nextjs/api'; -import { revalidatePath } from 'next/cache'; import { cookies } from 'next/headers'; -import * as mutations from '@/src/graphql/mutations'; -import * as queries from '@/src/graphql/queries'; +import config from '@/amplifyconfiguration.json'; -import config from '@/src/amplifyconfiguration.json'; +export const cookiesClient = generateServerClientUsingCookies({ + config, + cookies +}); + +export default async function Home() { + return ( +
+
+ + +
+
+ ); +} +``` + +### Create a form for submitting the todos + +In Next.js you can use a [Server Action](https://nextjs.org/docs/app/api-reference/functions/server-actions) to handle form submission server-side. Let's add a Server Action which submits its data to the `createTodo` function. When called, `createTodo` should send a GraphQL mutation via `cookiesClient.graphql(...)` to the GraphQL API, then call `revalidatePath` from the Next.js cache to invalidate the page cache and fetch the latest todos. + +Update the **src/app/page.tsx** with the following code: + +```tsx +import { generateServerClientUsingCookies } from '@aws-amplify/adapter-nextjs/api'; +import { cookies } from 'next/headers'; +// 1. Add the following two imports +import { revalidatePath } from 'next/cache'; +import * as mutations from '@/graphql/mutations'; + +import config from '@/amplifyconfiguration.json'; export const cookiesClient = generateServerClientUsingCookies({ config, cookies }); +// 2. Create a new Server Action async function createTodo(formData: FormData) { - 'use server'; + "use server" + const { data } = await cookiesClient.graphql({ + query: mutations.createTodo, + variables: { + input: { + name: formData.get('name')?.toString() ?? '' + } + } + }); - await cookiesClient.graphql({ + console.log("Created Todo: ", data?.createTodo ) + + revalidatePath('/'); +} + +export default async function Home() { + return ( +
+ {/* 3. Update the form's action to use the + new create Todo Server Action*/} +
+ + +
+
+ ); +} +``` + +### List todos + +Using `cookiesClient.graphql(...)` we make GraphQL queries as well. Pass in the `listTodos` query and assign the items returned to `todos` then iterate over them to display in a `