next.js/examples/api-routes-graphql/pages/index.tsx
index.tsx40 lines738 B
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>
  );
}
Quest for Codev2.0.0
/
SIGN IN