Created
August 9, 2023 23:01
-
-
Save kmjones1979/8907a234cbeaa3efa045aad6234cd34e to your computer and use it in GitHub Desktop.
Example table showcasing the messages coming from The Graph using GraphQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Link from "next/link"; | |
import type { NextPage } from "next"; | |
import { BugAntIcon, MagnifyingGlassIcon, SparklesIcon } from "@heroicons/react/24/outline"; | |
import { MetaHeader } from "~~/components/MetaHeader"; | |
import { gql } from '@apollo/client'; | |
import { useQuery } from '@apollo/client'; | |
export const GET_MESSAGES = gql` | |
{ | |
messages(first: 10, orderBy: createdAt, orderDirection: asc) { | |
id | |
_from { | |
messages(first: 1) { | |
message | |
} | |
} | |
} | |
} | |
`; | |
const Home: NextPage = () => { | |
const { loading, error, data } = useQuery(GET_MESSAGES); | |
if (loading) return <p>Loading...</p>; | |
if (error) return <p>Error: {error.message}</p>; | |
const messages = data.messages; | |
return ( | |
<> | |
<MetaHeader /> | |
<div className="flex items-center flex-col flex-grow pt-10"> | |
<h1>Messages</h1> | |
<table> | |
<thead> | |
<tr> | |
<th>ID</th> | |
<th>Message</th> | |
</tr> | |
</thead> | |
<tbody> | |
{messages.map((message) => ( | |
<tr key={message.id}> | |
<td>{message.id}</td> | |
<td>{message._from.messages[0]?.message || 'No message available'}</td> | |
</tr> | |
))} | |
</tbody> | |
</table> | |
</div> | |
</> | |
); | |
}; | |
export default Home; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment