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

index.tsx

167 lines4.4 KB

1/* @refresh reload */
2import {
3 createQuery,
4 QueryClient,
5 QueryClientProvider,
6} from '@tanstack/solid-query'
7import { SolidQueryDevtools } from '@tanstack/solid-query-devtools'
8import type { Accessor, Setter } from 'solid-js'
9import { createSignal, For, Match, Switch } from 'solid-js'
10import { render } from 'solid-js/web'
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] = createSignal(-1)
19​
20 return (
21 <QueryClientProvider client={queryClient}>
22 <SolidQueryDevtools />
23 <p>
24 As you visit the posts below, you will notice them in a loading state
25 the first time you load them. However, after you return to this list and
26 click on any posts you have already visited again, you will see them
27 load instantly and background refresh right before your eyes!{' '}
28 <strong>
29 (You may need to throttle your network speed to simulate longer
30 loading sequences)
31 </strong>
32 </p>
33 {postId() > -1 ? (
34 <Post postId={postId()} setPostId={setPostId} />
35 ) : (
36 <Posts setPostId={setPostId} />
37 )}
38 </QueryClientProvider>
39 )
41​
42function createPosts() {
43 return createQuery(() => ({
44 queryKey: ['posts'],
45 queryFn: async () => {
46 const {
47 posts: { data },
48 } = await request<any>(
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(props: { setPostId: Setter<number> }) {
67 const state = createPosts()
68​
69 return (
70 <div>
71 <h1>Posts</h1>
72 <div>
73 <Switch>
74 <Match when={state.status === 'pending'}>Loading...</Match>
75 <Match when={state.status === 'error'}>
76 <span>Error: {(state.error as Error).message}</span>
77 </Match>
78 <Match when={state.data !== undefined}>
79 <>
80 <div>
81 <For each={state.data}>
82 {(post: any) => (
83 <p>
84 <a
85 onClick={() => props.setPostId(post.id)}
86 href="#"
87 style={
88 // We can find the existing query data here to show bold links for
89 // ones that are cached
90 queryClient.getQueryData(['post', post.id])
91 ? {
92 'font-weight': 'bold',
93 color: 'green',
94 }
95 : {}
96 }
97 >
98 {post.title}
99 </a>
100 </p>
101 )}
102 </For>
103 </div>
104 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
105 </>
106 </Match>
107 </Switch>
108 </div>
109 </div>
110 )
112​
113function createPost(postId: Accessor<number>) {
114 return createQuery(() => ({
115 queryKey: ['post', postId()],
116 queryFn: async (context) => {
117 const { post } = await request<any>(
118 endpoint,
119 gql`
120 query {
121 post(id: ${context.queryKey[1]}) {
122 id
123 title
124 body
125 }
126 }
127 `,
128 )
129​
130 return post
131 },
132 enabled: !!postId,
133 }))
135​
136function Post(props: { postId: number; setPostId: Setter<number> }) {
137 const state = createPost(() => props.postId)
138​
139 return (
140 <div>
141 <div>
142 <a onClick={() => props.setPostId(-1)} href="#">
143 Back
144 </a>
145 </div>
146 <Switch>
147 <Match when={!props.postId || state.status === 'pending'}>
148 Loading...
149 </Match>
150 <Match when={state.status === 'error'}>
151 <span>Error: {(state.error as Error).message}</span>
152 </Match>
153 <Match when={state.data !== undefined}>
154 <>
155 <h1>{state.data.title}</h1>
156 <div>
157 <p>{state.data.body}</p>
158 </div>
159 <div>{state.isFetching ? 'Background Updating...' : ' '}</div>
160 </>
161 </Match>
162 </Switch>
163 </div>
164 )
166​
167render(() => <App />, document.getElementById('root') as HTMLElement)