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

index.tsx

184 lines4.8 KB

1/* @refresh reload */
2import {
3 QueryClient,
4 QueryClientProvider,
5 useQuery,
6} from '@tanstack/solid-query'
7import { SolidQueryDevtools } from '@tanstack/solid-query-devtools'
8import { For, Loading, Match, Switch, createSignal } from 'solid-js'
9import { render } from '@solidjs/web'
10import { gql, request } from 'graphql-request'
11import type { Accessor, Setter } from 'solid-js'
12​
13const endpoint = 'https://graphqlzero.almansi.me/api'
14​
15const queryClient = new QueryClient()
16​
17type Post = {
18 id: number
19 title: string
20 body: string
22​
23function App() {
24 const [postId, setPostId] = createSignal(-1)
25​
26 return (
27 <QueryClientProvider client={queryClient}>
28 <SolidQueryDevtools />
29 <p>
30 As you visit the posts below, you will notice them in a loading state
31 the first time you load them. However, after you return to this list and
32 click on any posts you have already visited again, you will see them
33 load instantly and background refresh right before your eyes!{' '}
34 <strong>
35 (You may need to throttle your network speed to simulate longer
36 loading sequences)
37 </strong>
38 </p>
39 <Loading fallback={<div>Loading...</div>}>
40 {postId() > -1 ? (
41 <Post postId={postId()} setPostId={setPostId} />
42 ) : (
43 <Posts setPostId={setPostId} />
44 )}
45 </Loading>
46 </QueryClientProvider>
47 )
49​
50function createPosts() {
51 return useQuery(() => ({
52 queryKey: ['posts'],
53 queryFn: async () => {
54 const {
55 posts: { data },
56 } = await request<{ posts: { data: Array<Post> } }>(
57 endpoint,
58 gql`
59 query {
60 posts {
61 data {
62 id
63 title
64 }
65 }
66 }
67 `,
68 )
69 return data
70 },
71 }))
73​
74function Posts(props: { setPostId: Setter<number> }) {
75 const state = createPosts()
76​
77 return (
78 <div>
79 <h1>Posts</h1>
80 <div>
81 <Switch>
82 <Match when={state.status === 'pending'}>Loading...</Match>
83 <Match when={state.status === 'error'}>
84 <span>Error: {(state.error as Error).message}</span>
85 </Match>
86 <Match when={state.data !== undefined}>
87 <>
88 <div>
89 <For each={state.data}>
90 {(postAccessor: any) => {
91 const post =
92 typeof postAccessor === 'function'
93 ? postAccessor()
94 : postAccessor
95 return (
96 <p>
97 <a
98 onClick={() => props.setPostId(post.id)}
99 href="#"
100 style={
101 // We can find the existing query data here to show bold links for
102 // ones that are cached
103 queryClient.getQueryData(['post', post.id])
104 ? {
105 'font-weight': 'bold',
106 color: 'green',
107 }
108 : {}
109 }
110 >
111 {post.title}
112 </a>
113 </p>
114 )
115 }}
116 </For>
117 </div>
118 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
119 </>
120 </Match>
121 </Switch>
122 </div>
123 </div>
124 )
126​
127function createPost(postId: Accessor<number>) {
128 return useQuery(() => ({
129 queryKey: ['post', postId()],
130 queryFn: async () => {
131 const { post } = await request<{ post: Post }>(
132 endpoint,
133 gql`
134 query {
135 post(id: ${postId()}) {
136 id
137 title
138 body
139 }
140 }
141 `,
142 )
143​
144 return post
145 },
146 enabled: !!postId(),
147 }))
149​
150function Post(props: { postId: number; setPostId: Setter<number> }) {
151 const state = createPost(() => props.postId)
152​
153 return (
154 <div>
155 <div>
156 <a onClick={() => props.setPostId(-1)} href="#">
157 Back
158 </a>
159 </div>
160 <Switch>
161 <Match when={!props.postId || state.status === 'pending'}>
162 Loading...
163 </Match>
164 <Match when={state.status === 'error'}>
165 <span>Error: {(state.error as Error).message}</span>
166 </Match>
167 <Match when={state.data !== undefined}>
168 <>
169 <h1>{state.data?.title}</h1>
170 <div>
171 <p>{state.data?.body}</p>
172 </div>
173 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
174 </>
175 </Match>
176 </Switch>
177 </div>
178 )
180​
181const root = document.getElementById('root')
182if (!root) throw new Error('Missing #root element')
183​
184render(() => <App />, root)