Skip to content

Commit cf426e3

Browse files
committed
fix (next): updated getting started guide after v5 tests
1 parent a3c5034 commit cf426e3

File tree

2 files changed

+63
-52
lines changed

2 files changed

+63
-52
lines changed

src/fragments/start/getting-started/next/api.mdx

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,36 @@ amplify add api
1515
Select the explicit values below to enable **API key** (for public read access) and **Cognito User Pools** (for authenticated access).
1616

1717
```
18-
? Select from one of the below mentioned services: GraphQL
18+
? Select from one of the below mentioned services: (GraphQL)
19+
1920
? Here is the GraphQL API that we will create. Select a setting to edit or continue
20-
Authorization modes: API key (default, expiration time: 7 days from now)
21-
? Choose the default authorization type for the API Amazon Cognito User Pool
22-
Using service: Cognito, provided by: awscloudformation
21+
Authorization modes: (API key (default, expiration time: 7 days from now))
22+
23+
? Choose the default authorization type for the API: (Amazon Cognito User Pool)
2324
25+
Using service: Cognito, provided by: awscloudformation
2426
The current configured provider is Amazon Cognito.
2527
26-
Do you want to use the default authentication and security configuration? Default configuration
28+
? Do you want to use the default authentication and security configuration? Default configuration: (Default configuration)
2729
2830
Warning: you will not be able to edit these selections.
2931
30-
How do you want users to be able to sign in? Username
31-
Do you want to configure advanced settings? No, I am done.
32+
? How do you want users to be able to sign in? (Username)
33+
? Do you want to configure advanced settings? No, I am done. (No, I am done.)
34+
3235
✅ Successfully added auth resource nextamplified locally
3336
3437
✅ Some next steps:
3538
"amplify push" will build all your local backend resources and provision it in the cloud
3639
"amplify publish" will build all your local backend and frontend resources (if you have hosting category added) and provision it in the cloud
3740
38-
? Configure additional auth types? No
39-
? Here is the GraphQL API that we will create. Select a setting to edit or continue Continue
40-
? Choose a schema template: Single object with fields (e.g., “Todo” with ID, name, description)
41+
? Configure additional auth types? (No)
42+
? Here is the GraphQL API that we will create. Select a setting to edit or continue: (Continue)
43+
? Choose a schema template: (Single object with fields (e.g., “Todo” with ID, name, description))
44+
45+
# You should see that the GraphQL schema compiled successfully and the location for the file.
4146
47+
? Do you want to edit the schema now? (Yes)
4248
4349
```
4450

@@ -89,16 +95,18 @@ amplify push
8995

9096

9197
API key configuration
92-
✔ Enter a description for the API key: ·
93-
✔ After how many days from now the API key should expire (1-365): · 7
94-
GraphQL schema compiled successfully.
95-
96-
? Do you want to generate code for your newly created GraphQL API Yes
97-
? Choose the code generation language target javascript
98-
? Enter the file name pattern of graphql queries, mutations and subscriptions src/graphql/**/*.js
99-
? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions Yes
100-
? Enter maximum statement depth [increase from default if your schema is deeply nested] 2
98+
✔ Enter a description for the API key: · (Just hit enter)
99+
✔ After how many days from now the API key should expire (1-365): · 7 (Just hit enter)
100+
101+
# You should see that the GraphQL schema compiled successfully.
102+
103+
? Do you want to generate code for your newly created GraphQL API: (Yes)
104+
? Choose the code generation language target: (javascript)
105+
? Enter the file name pattern of graphql queries, mutations and subscriptions (src/graphql/**/*.js) (Just hit enter)
106+
? Do you want to generate/update all possible GraphQL operations - queries, mutations and subscriptions: (Yes)
107+
? Enter maximum statement depth [increase from default if your schema is deeply nested]: (2)
101108

109+
# Wait a minute or so for the deployment into the dev environment to complete
102110
...
103111

104112
✔ Generated GraphQL operations successfully and saved at src/graphql
@@ -228,13 +236,19 @@ Amplify.configure({ ...awsExports, ssr: true });
228236

229237
export async function getServerSideProps({ req }) {
230238
const SSR = withSSRContext({ req });
231-
const response = await SSR.API.graphql({ query: listPosts });
232-
233-
return {
234-
props: {
235-
posts: response.data.listPosts.items
236-
}
237-
};
239+
try {
240+
const response = await SSR.API.graphql({ query: listPosts });
241+
return {
242+
props: {
243+
posts: response.data.listPosts.items,
244+
},
245+
};
246+
} catch (err) {
247+
console.log(err);
248+
return {
249+
props: {},
250+
};
251+
}
238252
}
239253

240254
async function handleCreatePost(event) {
@@ -346,7 +360,10 @@ Submit that form to create a new post, and you'll build that page next!
346360

347361
Statically generating pages during the build process improves performance. But, dynamically created posts still need to not `404`.
348362

349-
To solve this, create **pages/posts/[id].js** and paste in the following content:
363+
To solve this, create a new folder within **/pages** named "posts". Next, create a new file within this new **/posts** named **[id].js** to create the structure:
364+
**pages/posts/[id].js**.
365+
366+
Paste the following content into the newly made **[id].js** file:
350367

351368
```jsx
352369
import { Amplify, API, withSSRContext } from 'aws-amplify';
@@ -359,29 +376,16 @@ import styles from '../../styles/Home.module.css';
359376

360377
Amplify.configure({ ...awsExports, ssr: true });
361378

362-
export async function getStaticPaths() {
363-
const SSR = withSSRContext();
364-
const { data } = await SSR.API.graphql({ query: listPosts });
365-
const paths = data.listPosts.items.map((post) => ({
366-
params: { id: post.id }
367-
}));
368-
369-
return {
370-
fallback: true,
371-
paths
372-
};
373-
}
374-
375-
export async function getStaticProps({ params }) {
376-
const SSR = withSSRContext();
379+
export async function getServerSideProps({req, params}) {
380+
const SSR = withSSRContext({req});
377381
const { data } = await SSR.API.graphql({
378382
query: getPost,
379383
variables: {
380384
id: params.id
381385
}
382386
});
383-
384-
return {
387+
console.log(data)
388+
return {
385389
props: {
386390
post: data.getPost
387391
}
@@ -441,9 +445,7 @@ Let's walk through some of this file:
441445

442446
- **Amplify.configure** – For authenticated requests to work on the server, your client has to be configured with `ssr: true` to make credentials available on subsequent requests.
443447

444-
- **getStaticPaths** – Because the value of `[id]` isn't known at build-time, you need to provide all possible `paths` for Next.js to render pages for. You do this by querying all posts with `API.graphql`, and mapping each `post` into `{ params: { id: post.id }}`. These `params` are passed to `getStaticProps` next. Note that you return `fallback: true`. If this value were `false`, then any _new_ posts would return a `404` because they didn't exist at build-time. With `true`, static pages are returned quickly, while any new `id`s are looked up once.
445-
446-
- **getStaticProps** – Because the `Post` components props aren't dynamic per-request, they're provided statically from `getStaticPaths` as `params`. Here, you use `params.id` to query for that specific post with `API.graphql`.
448+
- **getServerSideProps** - This will only run on the server-side and allows for fetching of data as we make the request. Here, you use params.id to query for that specific post with API.graphql and await the response.
447449

448450
- **Post**`Post` is a functional component that renders the provided prop `post`. Because `fallback: true` was specified above, you have to account for a "loading" state while a new post may be fetched.
449451

src/fragments/start/getting-started/next/setup.mdx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ npx create-next-app next-amplified
77
cd next-amplified
88
```
99

10-
This creates a new Next.js app in a directory called `next-amplified` and then switches us into that new directory.
10+
You'll need to select if you'd like to build this project with TypeScript (Yes) or JavaScript (No), and then if you'd like ESLint to be configured as well. Note that if you select Typescript, there may be additional changes you'll need to make throughout this guide to files and configurations.
11+
12+
Once these have been selected, a new Next.js app will be created in a directory called `next-amplified` and then switches us into that new directory.
1113

1214
Now that you're in the root of the project, you can run the app by using the following command:
1315

@@ -48,12 +50,19 @@ Source directory path (src)
4850

4951
Distribution directory path (build)
5052

51-
Build command (npm run build)
53+
Build command (npm run-script build)
54+
55+
Start command (npm run-script start)
56+
57+
? Initialize the project with the above configuration? (Y/n)
58+
59+
# If you completed the introduction to Amplify steps, the default below is the profile you created with the `amplify configure` command.
60+
Using default provider awscloudformation
61+
? Select the authentication method you want to use: (AWS profile)
5262

53-
Start command (npm start)
63+
? Initialize the project with the above configuration? (Y/n)
64+
? Do you want to use an AWS profile? (default)
5465

55-
# This is the profile you created with the `amplify configure` command in the introduction step.
56-
Do you want to use an AWS profile?
5766
```
5867

5968
> Where possible the CLI will infer the proper configuration based on the type of project Amplify is being initialized in. In this case it knew you are using Create Next App and provided the proper configuration for type of app, framework, source, distribution, build, and start options.

0 commit comments

Comments
 (0)