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

index.tsx

168 lines4.3 KB

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