/* @refresh reload */ import { QueryClient, QueryClientProvider, useQuery, } from '@tanstack/solid-query' import { SolidQueryDevtools } from '@tanstack/solid-query-devtools' import { For, Match, Switch, createSignal } from 'solid-js' import { render } from 'solid-js/web' import { gql, request } from 'graphql-request' import type { Accessor, Setter } from 'solid-js' const endpoint = 'https://graphqlzero.almansi.me/api' const queryClient = new QueryClient() type Post = { id: number title: string body: string } function App() { const [postId, setPostId] = createSignal(-1) return (

As you visit the posts below, you will notice them in a loading state the first time you load them. However, after you return to this list and click on any posts you have already visited again, you will see them load instantly and background refresh right before your eyes!{' '} (You may need to throttle your network speed to simulate longer loading sequences)

{postId() > -1 ? ( ) : ( )}
) } function createPosts() { return useQuery(() => ({ queryKey: ['posts'], queryFn: async () => { const { posts: { data }, } = await request<{ posts: { data: Array } }>( endpoint, gql` query { posts { data { id title } } } `, ) return data }, })) } function Posts(props: { setPostId: Setter }) { const state = createPosts() return (

Posts

Loading... Error: {(state.error as Error).message} <>
{state.isFetching ? 'Background Updating...' : ' '}
) } function createPost(postId: Accessor) { return useQuery(() => ({ queryKey: ['post', postId()], queryFn: async () => { const { post } = await request<{ post: Post }>( endpoint, gql` query { post(id: ${postId()}) { id title body } } `, ) return post }, enabled: !!postId(), })) } function Post(props: { postId: number; setPostId: Setter }) { const state = createPost(() => props.postId) return (
Loading... Error: {(state.error as Error).message} <>

{state.data?.title}

{state.data?.body}

{state.isFetching ? 'Background Updating...' : ' '}
) } render(() => , document.getElementById('root') as HTMLElement)