-
Notifications
You must be signed in to change notification settings - Fork 39
/
[id].tsx
39 lines (33 loc) · 970 Bytes
/
[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
import * as React from "react";
import { API, graphqlOperation } from "aws-amplify";
import { GetTodoQuery } from "../../src/API";
import { getTodo } from "../../src/graphql/queries";
import config from "../../src/aws-exports";
API.configure(config);
type Props = { todo: GetTodoQuery["getTodo"] };
const TodoPage = (props: Props) => {
return (
<div>
<h2>Individual Todo {props.todo.id}</h2>
<pre>{JSON.stringify(props.todo, null, 2)}</pre>
</div>
);
};
TodoPage.getInitialProps = async context => {
const { id } = context.query;
try {
const todo = (await API.graphql({
...graphqlOperation(getTodo),
variables: { id }
})) as { data: GetTodoQuery; errors?: {}[] };
if (todo.errors) {
console.log("Failed to fetch todo. ", todo.errors);
return { todo: {} };
}
return { todo: todo.data.getTodo };
} catch (err) {
console.warn(err);
return { todo: {} };
}
};
export default TodoPage;