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( "{ users { name } }", fetcher, ); if (error) return
Failed to load
; if (isLoading) return
Loading...
; if (!data) return null; const { users } = data; return (
{users.map((user, index) => (
{user.name}
))}
); }