-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
90 lines (80 loc) · 2.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { useQuery, gql, NetworkStatus } from "@apollo/client";
import React, { useEffect, useState } from "react";
const QUERY = gql`
query Jobs {
jobs {
id
title
}
}
`;
export default function IndexPage() {
const { data, loading, networkStatus, error, refetch } = useQuery(QUERY, {
notifyOnNetworkStatusChange: true
});
console.log({ loading, data, error, refetch });
// On page load, the `networkStatus` should be NetworkStatus.ready ( `7` ) if the data is in the cache, and the page should not need to re-render.
const [cached, setCached] = useState(true);
useEffect(() => {
if (networkStatus !== NetworkStatus.ready) setCached(false);
}, [networkStatus]);
if (loading) return "Loading...";
return (
<div>
<h1>Next.js Server-Side Rendering with Apollo GraphQL</h1>
<p>
<strong>
A simple approach to server-side rendering in Next.js with Apollo
GraphQL, featuring no duplicate queries or complicated client/server
logic, cache hydration and live queries for the client.
</strong>
</p>
<p>
Data provided by{" "}
<a
href="https://api.graphql.jobs/"
target="_blank"
rel="noopener noreferrer"
>
https://api.graphql.jobs/
</a>
.
</p>
<p>
This page's data was fetched on the{" "}
<strong>{cached ? "Next.js server" : "client"}</strong>.<br />
Network Status: <strong>{networkStatus}</strong>{" "}
<button
onClick={() =>
refetch({
fetchPolicy: "network-only"
})
}
>
Refetch
</button>
</p>
<div
style={{
display: "grid",
grid: "auto / repeat(auto-fit, minmax(15em,1fr))"
}}
>
{[...data?.jobs].map((item) => {
return (
<ul key={item.id}>
{Object.entries(item).map(([key, value]) => {
if (typeof value !== typeof "") return null;
return (
<li key={key}>
<strong>{key}</strong>: {value}
</li>
);
})}
</ul>
);
})}
</div>
</div>
);
}