rsnext/examples/api-routes-graphql/pages/index.js
2020-05-18 15:24:37 -04:00

29 lines
601 B
JavaScript

import useSWR from 'swr'
const fetcher = (query) =>
fetch('/api/graphql', {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({ query }),
})
.then((res) => res.json())
.then((json) => json.data)
export default function Index() {
const { data, error } = useSWR('{ users { name } }', fetcher)
if (error) return <div>Failed to load</div>
if (!data) return <div>Loading...</div>
const { users } = data
return (
<div>
{users.map((user, i) => (
<div key={i}>{user.name}</div>
))}
</div>
)
}