Skip to content

Instantly share code, notes, and snippets.

@dabit3
Last active October 22, 2020 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dabit3/3639c45cf5b47d361333b2c35b4f0a02 to your computer and use it in GitHub Desktop.
Save dabit3/3639c45cf5b47d361333b2c35b4f0a02 to your computer and use it in GitHub Desktop.
Next.js + Amplify - pages/posts/[id].js part 1
// pages/posts/[id].js
import { API } from 'aws-amplify';
import { getPost, listPosts } from '../../src/graphql/queries'
import '../../configureAmplify';
import { useRouter } from 'next/router';
export default function Post({ post }) {
const router = useRouter();
if (router.isFallback) return <div>Loading...</div>
return (
<div>
<h2>{post.name}</h2>
<p>{post.content}</p>
<span>By: {post.username}</span>
</div>
)
}
export async function getStaticPaths() {
const postData = await API.graphql({ query: listPosts });
const postIds = postData.data.listPosts.items.map(post => ({ params: { id: post.id } }));
return {
paths: postIds, fallback: true
};
}
export async function getStaticProps(context) {
const id = context.params.id;
const post = await API.graphql({ query: getPost, variables: { id } });
return {
props: {
post: post.data.getPost
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment