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

main.ts

60 lines1.7 KB

1import { LitElement, html } from 'lit'
2import { QueryClient, QueryClientProvider } from '@tanstack/lit-query'
3import './post-list'
4import './post-detail'
5​
6const queryClient = new QueryClient()
7​
8class BasicGraphqlRequestQueryProvider extends QueryClientProvider {
9 constructor() {
10 super()
11 this.client = queryClient
12 }
14​
15class BasicGraphqlRequestApp extends LitElement {
16 static properties = {
17 postId: { state: true },
18 }
19​
20 private postId: string | null = null
21​
22 protected override createRenderRoot(): HTMLElement | DocumentFragment {
23 return this
24 }
25​
26 private setPostId(event: CustomEvent<string | null>) {
27 this.postId = event.detail
28 }
29​
30 render() {
31 return html`
32 <basic-graphql-request-query-provider>
33 <p>
34 As you visit the posts below, you will notice them in a loading state
35 the first time you load them. However, after you return to this list
36 and click on any posts you have already visited again, you will see
37 them load instantly and background refresh right before your eyes!
38 <strong>
39 (You may need to throttle your network speed to simulate longer
40 loading sequences)
41 </strong>
42 </p>
43 ${
44 this.postId !== null
45 ? html`<post-detail
46 .postId=${this.postId}
47 @set-post-id=${this.setPostId}
48 ></post-detail>`
49 : html`<post-list @set-post-id=${this.setPostId}></post-list>`
50 }
51 </basic-graphql-request-query-provider>
52 `
53 }
55​
56customElements.define(
57 'basic-graphql-request-query-provider',
58 BasicGraphqlRequestQueryProvider,
60customElements.define('basic-graphql-request-app', BasicGraphqlRequestApp)