Skip to content

Instantly share code, notes, and snippets.

@kmjones1979
Created August 9, 2023 23:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmjones1979/8907a234cbeaa3efa045aad6234cd34e to your computer and use it in GitHub Desktop.
Save kmjones1979/8907a234cbeaa3efa045aad6234cd34e to your computer and use it in GitHub Desktop.
Example table showcasing the messages coming from The Graph using GraphQL
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