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

index.tsx

172 lines4.5 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, Match, Show, Switch, createSignal } from 'solid-js'
9import { render } from 'solid-js/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: string
19 title: string
20 body: string
22​
23function App() {
24 const [postId, setPostId] = createSignal<string | null>(null)
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 <Show when={postId()} fallback={<Posts setPostId={setPostId} />}>
40 {(id) => <Post postId={id()} setPostId={setPostId} />}
41 </Show>
42 </QueryClientProvider>
43 )
45​
46function createPosts() {
47 return useQuery(() => ({
48 queryKey: ['posts'],
49 queryFn: async () => {
50 const {
51 posts: { data },
52 } = await request<{ posts: { data: Array<Post> } }>(
53 endpoint,
54 gql`
55 query {
56 posts {
57 data {
58 id
59 title
60 }
61 }
62 }
63 `,
64 )
65 return data
66 },
67 }))
69​
70function Posts(props: { setPostId: Setter<string | null> }) {
71 const state = createPosts()
72​
73 return (
74 <div>
75 <h1>Posts</h1>
76 <div>
77 <Switch>
78 <Match when={state.status === 'pending'}>Loading...</Match>
79 <Match when={state.status === 'error'}>
80 <span>Error: {(state.error as Error).message}</span>
81 </Match>
82 <Match when={state.data !== undefined}>
83 <>
84 <div>
85 <For each={state.data}>
86 {(post) => (
87 <p>
88 <a
89 onClick={() => props.setPostId(post.id)}
90 href="#"
91 style={
92 // We can find the existing query data here to show bold links for
93 // ones that are cached
94 queryClient.getQueryData(['post', post.id])
95 ? {
96 'font-weight': 'bold',
97 color: 'green',
98 }
99 : {}
100 }
101 >
102 {post.title}
103 </a>
104 </p>
105 )}
106 </For>
107 </div>
108 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
109 </>
110 </Match>
111 </Switch>
112 </div>
113 </div>
114 )
116​
117function createPost(postId: Accessor<string>) {
118 return useQuery(() => ({
119 queryKey: ['post', postId()],
120 queryFn: async () => {
121 const { post } = await request<{ post: Post }>(
122 endpoint,
123 gql`
124 query Post($id: ID!) {
125 post(id: $id) {
126 id
127 title
128 body
129 }
130 }
131 `,
132 { id: postId() },
133 )
134​
135 return post
136 },
137 }))
139​
140function Post(props: { postId: string; setPostId: Setter<string | null> }) {
141 const state = createPost(() => props.postId)
142​
143 return (
144 <div>
145 <div>
146 <a onClick={() => props.setPostId(null)} href="#">
147 Back
148 </a>
149 </div>
150 <Switch>
151 <Match when={state.status === 'pending'}>Loading...</Match>
152 <Match when={state.status === 'error'}>
153 <span>Error: {(state.error as Error).message}</span>
154 </Match>
155 <Match when={state.data !== undefined}>
156 <>
157 <h1>{state.data?.title}</h1>
158 <div>
159 <p>{state.data?.body}</p>
160 </div>
161 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
162 </>
163 </Match>
164 </Switch>
165 </div>
166 )
168​
169const root = document.getElementById('root')
170if (!root) throw new Error('Missing #root element')
171​
172render(() => <App />, root)