Download next.js/examples/api-routes-graphql/pages/index.tsx from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 738 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/api-routes-graphql/pages/index.tsx
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/next.js/examples/api-routes-graphql/pages/index.tsx
-
curl -L -o index.tsx https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/next.js/examples/api-routes-graphql/pages/index.tsx
738 Bytes
| 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> | |
| ); | |
| } | |