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

index.jsx

163 lines3.8 KB

1/* eslint-disable jsx-a11y/anchor-is-valid */
2import React from 'react'
3import ReactDOM from 'react-dom/client'
4import {
5 useQuery,
6 useQueryClient,
7 QueryClient,
8 QueryClientProvider,
9} from '@tanstack/react-query'
10import { ReactQueryDevtools } from '@tanstack/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({
44 queryKey: ['posts'],
45 queryFn: async () => {
46 const {
47 posts: { data },
48 } = await request(
49 endpoint,
50 gql`
51 query {
52 posts {
53 data {
54 id
55 title
56 }
57 }
58 }
59 `,
60 )
61 return data
62 },
63 })
65​
66function Posts({ setPostId }) {
67 const queryClient = useQueryClient()
68 const { status, data, error, isFetching } = usePosts()
69​
70 return (
71 <div>
72 <h1>Posts</h1>
73 <div>
74 {status === 'pending' ? (
75 'Loading...'
76 ) : status === 'error' ? (
77 <span>Error: {error.message}</span>
78 ) : (
79 <>
80 <div>
81 {data.map((post) => (
82 <p key={post.id}>
83 <a
84 onClick={() => setPostId(post.id)}
85 href="#"
86 style={
87 // We can find the existing query data here to show bold links for
88 // ones that are cached
89 queryClient.getQueryData(['post', post.id])
90 ? {
91 fontWeight: 'bold',
92 color: 'green',
93 }
94 : {}
95 }
96 >
97 {post.title}
98 </a>
99 </p>
100 ))}
101 </div>
102 <div>{isFetching ? 'Background Updating...' : ' '}</div>
103 </>
104 )}
105 </div>
106 </div>
107 )
109​
110function usePost(postId) {
111 return useQuery(
112 ['post', postId],
113 async () => {
114 const { post } = await request(
115 endpoint,
116 gql`
117 query {
118 post(id: ${postId}) {
119 id
120 title
121 body
122 }
123 }
124 `,
125 )
126​
127 return post
128 },
129 {
130 enabled: !!postId,
131 },
132 )
134​
135function Post({ postId, setPostId }) {
136 const { status, data, error, isFetching } = usePost(postId)
137​
138 return (
139 <div>
140 <div>
141 <a onClick={() => setPostId(-1)} href="#">
142 Back
143 </a>
144 </div>
145 {!postId || status === 'pending' ? (
146 'Loading...'
147 ) : status === 'error' ? (
148 <span>Error: {error.message}</span>
149 ) : (
150 <>
151 <h1>{data.title}</h1>
152 <div>
153 <p>{data.body}</p>
154 </div>
155 <div>{isFetching ? 'Background Updating...' : ' '}</div>
156 </>
157 )}
158 </div>
159 )
161​
162const rootElement = document.getElementById('root')
163ReactDOM.createRoot(rootElement).render(<App />)