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

QueryClientProvider.tsx

110 lines2.9 KB

1import {
2 createContext,
3 mergeProps,
4 onCleanup,
5 onMount,
6 useContext,
7} from 'solid-js'
8import type { QueryClient } from '@tanstack/query-core'
9import type { Context, JSX } from 'solid-js'
10import type { ContextOptions } from './types'
11​
12declare global {
13 interface Window {
14 SolidQueryClientContext?: Context<QueryClient | undefined>
15 }
17​
18export const defaultContext = createContext<QueryClient | undefined>(undefined)
19const QueryClientSharingContext = createContext<boolean>(false)
20​
21// If we are given a context, we will use it.
22// Otherwise, if contextSharing is on, we share the first and at least one
23// instance of the context across the window
24// to ensure that if Solid Query is used across
25// different bundles or microfrontends they will
26// all use the same **instance** of context, regardless
27// of module scoping.
28function getQueryClientContext(
29 context: Context<QueryClient | undefined> | undefined,
30 contextSharing: boolean,
31) {
32 if (context) {
33 return context
34 }
35 if (contextSharing && typeof window !== 'undefined') {
36 if (!window.SolidQueryClientContext) {
37 window.SolidQueryClientContext = defaultContext
38 }
39​
40 return window.SolidQueryClientContext
41 }
42​
43 return defaultContext
45​
46export const useQueryClient = ({ context }: ContextOptions = {}) => {
47 const queryClient = useContext(
48 getQueryClientContext(context, useContext(QueryClientSharingContext)),
49 )
50​
51 if (!queryClient) {
52 throw new Error('No QueryClient set, use QueryClientProvider to set one')
53 }
54​
55 return queryClient
57​
58type QueryClientProviderPropsBase = {
59 client: QueryClient
60 children?: JSX.Element
62type QueryClientProviderPropsWithContext = ContextOptions & {
63 contextSharing?: never
64} & QueryClientProviderPropsBase
65type QueryClientProviderPropsWithContextSharing = {
66 context?: never
67 contextSharing?: boolean
68} & QueryClientProviderPropsBase
69​
70export type QueryClientProviderProps =
71 | QueryClientProviderPropsWithContext
72 | QueryClientProviderPropsWithContextSharing
73​
74export const QueryClientProvider = (
75 props: QueryClientProviderProps,
76): JSX.Element => {
77 const mergedProps = mergeProps(
78 {
79 contextSharing: false,
80 },
81 props,
82 )
83 onMount(() => {
84 mergedProps.client.mount()
85​
86 if (process.env.NODE_ENV !== 'production' && mergedProps.contextSharing) {
87 mergedProps.client
88 .getLogger()
89 .error(
90 `The contextSharing option has been deprecated and will be removed in the next major version`,
91 )
92 }
93 })
94 onCleanup(() => mergedProps.client.unmount())
95​
96 const QueryClientContext = getQueryClientContext(
97 mergedProps.context,
98 mergedProps.contextSharing,
99 )
100​
101 return (
102 <QueryClientSharingContext.Provider
103 value={!mergedProps.context && mergedProps.contextSharing}
104 >
105 <QueryClientContext.Provider value={mergedProps.client}>
106 {mergedProps.children}
107 </QueryClientContext.Provider>
108 </QueryClientSharingContext.Provider>
109 )