Open History to see the last commit
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, 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
21}
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 )
46}
47β
48function createPosts() {
49 return useQuery(() => ({
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 }))
70}
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 )
117}
118β
119function createPost(postId: Accessor<number>) {
120 return useQuery(() => ({
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 }))
140}
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 )
171}
172β
173const root = document.getElementById('root')
174if (!root) throw new Error('Missing #root element')
175β
176render(() => <App />, root)