Open History to see the last commit
1/* @refresh reload */
2import {
3 createQuery,
4 QueryClient,
5 QueryClientProvider,
6 useQueryClient,
7} from '@tanstack/solid-query'
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 <p>
23 As you visit the posts below, you will notice them in a loading state
24 the first time you load them. However, after you return to this list and
25 click on any posts you have already visited again, you will see them
26 load instantly and background refresh right before your eyes!{' '}
27 <strong>
28 (You may need to throttle your network speed to simulate longer
29 loading sequences)
30 </strong>
31 </p>
32 {postId() > -1 ? (
33 <Post postId={postId()} setPostId={setPostId} />
34 ) : (
35 <Posts setPostId={setPostId} />
36 )}
37 </QueryClientProvider>
38 )
39}
40β
41function createPosts() {
42 return createQuery({
43 queryKey: () => ['posts'],
44 queryFn: async () => {
45 const {
46 posts: { data },
47 } = await request(
48 endpoint,
49 gql`
50 query {
51 posts {
52 data {
53 id
54 title
55 }
56 }
57 }
58 `,
59 )
60 return data
61 },
62 })
63}
64β
65function Posts(props: { setPostId: Setter<number> }) {
66 const queryClient = useQueryClient()
67 const state = createPosts()
68β
69 return (
70 <div>
71 <h1>Posts</h1>
72 <div>
73 <Switch>
74 <Match when={state.status === 'loading'}>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 )
111}
112β
113function createPost(postId: Accessor<number>) {
114 return createQuery({
115 queryKey: () => ['post', postId()],
116 queryFn: async (context) => {
117 const { post } = await request(
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 })
134}
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 === 'loading'}>
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 )
165}
166β
167render(() => <App />, document.getElementById('root') as HTMLElement)