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

index.tsx

165 lines4.3 KB

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