File size: 738 Bytes
1e92f2d |
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 |
import useSWR from "swr";
const fetcher = (query: string) =>
fetch("/api/graphql", {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({ query }),
})
.then((res) => res.json())
.then((json) => json.data);
type Data = {
users: {
name: string;
}[];
};
export default function Index() {
const { data, error, isLoading } = useSWR<Data>(
"{ users { name } }",
fetcher,
);
if (error) return <div>Failed to load</div>;
if (isLoading) return <div>Loading...</div>;
if (!data) return null;
const { users } = data;
return (
<div>
{users.map((user, index) => (
<div key={index}>{user.name}</div>
))}
</div>
);
}
|