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

index.tsx

173 lines4.4 KB

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