Skip to content

Commit 2c31dd3

Browse files
committed
feat: add data fetching to
1 parent 1afc501 commit 2c31dd3

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { gql } from "urql";
2+
import { useRouteData } from "@/lib/context";
3+
4+
export default function RecentPosts() {
5+
const { graphqlData } = useRouteData();
6+
7+
const posts = graphqlData?.RecentPosts?.data?.posts?.nodes || [];
8+
9+
if (graphqlData?.RecentPosts?.error) {
10+
console.error("Error fetching RecentPosts:", graphqlData.RecentPosts.error);
11+
return <div>Error loading recent posts.</div>;
12+
}
13+
14+
return (
15+
<div className="recent-posts">
16+
<h2>Recent Posts</h2>
17+
<ul>
18+
{posts.map((post) => (
19+
<li key={post.id}>
20+
<a href={post.uri}>{post.title}</a>
21+
</li>
22+
))}
23+
</ul>
24+
</div>
25+
);
26+
}
27+
28+
RecentPosts.query = {
29+
query: gql`
30+
query RecentPosts {
31+
posts(first: 5) {
32+
nodes {
33+
id
34+
title
35+
uri
36+
}
37+
}
38+
}
39+
`,
40+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import Layout from "@/components/Layout";
2+
import RecentPosts from "@/components/RecentPosts";
23

34
export default function Home() {
45
return (
56
<Layout>
67
<h2>Home Template</h2>
78
<p>This is the home page of the template hierarchy example app.</p>
9+
<RecentPosts />
810
</Layout>
911
);
1012
}
13+
14+
Home.queries = [
15+
RecentPosts.query, // Ensure RecentPosts query is included
16+
];
Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
import Layout from "@/components/Layout";
22

3-
export default function SingleTemplate() {
3+
export default function SingleTemplate({ graphqlData }) {
4+
const { SinglePostQuery } = graphqlData;
45
return (
56
<Layout>
6-
<h2>Single Template</h2>
7-
<p>This is the single template for the template hierarchy example app.</p>
7+
<h2>{SinglePostQuery.response.data.post.title}</h2>
8+
<div
9+
dangerouslySetInnerHTML={{
10+
__html: SinglePostQuery.response.data.post.content,
11+
}}
12+
/>
813
</Layout>
914
);
1015
}
16+
17+
export const queries = "test";
18+
19+
SingleTemplate.queries = [
20+
{
21+
name: "SinglePostQuery",
22+
query: /* GraphQL */ `
23+
query SinglePostQuery($id: ID!) {
24+
post(id: $id, idType: URI) {
25+
id
26+
title
27+
content
28+
date
29+
}
30+
}
31+
`,
32+
variables: (event, { uri }) => ({
33+
id: uri,
34+
}),
35+
},
36+
];

0 commit comments

Comments
 (0)