forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
[id].tsx
48 lines (42 loc) · 1.27 KB
/
[id].tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { API, graphqlOperation } from 'aws-amplify'
import { GetTodoQuery, GetTodoListQuery } from '../../src/API'
import { getTodo, getTodoList } from '../../src/graphql/queries'
import config from '../../src/aws-exports'
API.configure(config)
const TodoPage = (props: { todo: GetTodoQuery['getTodo'] }) => {
return (
<div>
<h2>Individual Todo {props.todo.id}</h2>
<pre>{JSON.stringify(props.todo, null, 2)}</pre>
</div>
)
}
export const getStaticPaths = async () => {
let result = (await API.graphql(
graphqlOperation(getTodoList, { id: 'global' })
)) as { data: GetTodoListQuery; errors: any[] }
if (result.errors) {
console.error('Failed to fetch todo.', result.errors)
throw new Error(result.errors[0].message)
}
const paths = result.data.getTodoList.todos.items.map(({ id }) => ({
params: { id },
}))
return { paths, fallback: false }
}
export const getStaticProps = async ({ params: { id } }) => {
const todo = (await API.graphql({
...graphqlOperation(getTodo),
variables: { id },
})) as { data: GetTodoQuery; errors: any[] }
if (todo.errors) {
console.error(todo.errors)
throw new Error(todo.errors[0].message)
}
return {
props: {
todo: todo.data.getTodo,
},
}
}
export default TodoPage