Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

index.js

160 lines3.7 KB

1/* eslint-disable jsx-a11y/anchor-is-valid */
2import React from "react";
3import ReactDOM from "react-dom";
4import {
5 useQuery,
6 useQueryClient,
7 QueryClient,
8 QueryClientProvider,
9} from "react-query";
10import { ReactQueryDevtools } from "react-query/devtools";
11import { request, gql } from "graphql-request";
12​
13const endpoint = "https://graphqlzero.almansi.me/api";
14​
15const queryClient = new QueryClient();
16​
17function App() {
18 const [postId, setPostId] = React.useState(-1);
19​
20 return (
21 <QueryClientProvider client={queryClient}>
22 <p>
23 As you visit the posts below, you will notice them in a loading state
24 the first time you load them. However, after you return to this list and
25 click on any posts you have already visited again, you will see them
26 load instantly and background refresh right before your eyes!{" "}
27 <strong>
28 (You may need to throttle your network speed to simulate longer
29 loading sequences)
30 </strong>
31 </p>
32 {postId > -1 ? (
33 <Post postId={postId} setPostId={setPostId} />
34 ) : (
35 <Posts setPostId={setPostId} />
36 )}
37 <ReactQueryDevtools initialIsOpen />
38 </QueryClientProvider>
39 );
41​
42function usePosts() {
43 return useQuery("posts", async () => {
44 const {
45 posts: { data },
46 } = await request(
47 endpoint,
48 gql`
49 query {
50 posts {
51 data {
52 id
53 title
54 }
55 }
56 }
57 `
58 );
59 return data;
60 });
62​
63function Posts({ setPostId }) {
64 const queryClient = useQueryClient();
65 const { status, data, error, isFetching } = usePosts();
66​
67 return (
68 <div>
69 <h1>Posts</h1>
70 <div>
71 {status === "loading" ? (
72 "Loading..."
73 ) : status === "error" ? (
74 <span>Error: {error.message}</span>
75 ) : (
76 <>
77 <div>
78 {data.map((post) => (
79 <p key={post.id}>
80 <a
81 onClick={() => setPostId(post.id)}
82 href="#"
83 style={
84 // We can find the existing query data here to show bold links for
85 // ones that are cached
86 queryClient.getQueryData(["post", post.id])
87 ? {
88 fontWeight: "bold",
89 color: "green",
90 }
91 : {}
92 }
93 >
94 {post.title}
95 </a>
96 </p>
97 ))}
98 </div>
99 <div>{isFetching ? "Background Updating..." : " "}</div>
100 </>
101 )}
102 </div>
103 </div>
104 );
106​
107function usePost(postId) {
108 return useQuery(
109 ["post", postId],
110 async () => {
111 const { post } = await request(
112 endpoint,
113 gql`
114 query {
115 post(id: ${postId}) {
116 id
117 title
118 body
119 }
120 }
121 `
122 );
123​
124 return post;
125 },
126 {
127 enabled: !!postId,
128 }
129 );
131​
132function Post({ postId, setPostId }) {
133 const { status, data, error, isFetching } = usePost(postId);
134​
135 return (
136 <div>
137 <div>
138 <a onClick={() => setPostId(-1)} href="#">
139 Back
140 </a>
141 </div>
142 {!postId || status === "loading" ? (
143 "Loading..."
144 ) : status === "error" ? (
145 <span>Error: {error.message}</span>
146 ) : (
147 <>
148 <h1>{data.title}</h1>
149 <div>
150 <p>{data.body}</p>
151 </div>
152 <div>{isFetching ? "Background Updating..." : " "}</div>
153 </>
154 )}
155 </div>
156 );
158​
159const rootElement = document.getElementById("root");
160ReactDOM.render(<App />, rootElement);