1
- import { OperationVariables } from '@apollo/client'
1
+ import type {
2
+ OperationVariables ,
3
+ useBackgroundQuery as apolloUseBackgroundQuery ,
4
+ useReadQuery as apolloUseReadQuery ,
5
+ } from '@apollo/client'
2
6
import type { DocumentNode } from 'graphql'
3
7
8
+ /**
9
+ * @NOTE
10
+ * The types QueryOperationResult, MutationOperationResult, SubscriptionOperationResult, and SuspenseQueryOperationResult
11
+ * are overridden in packages/web/src/apollo/typeOverride.ts. This was originally so that you could bring your own gql client.
12
+ *
13
+ * The default (empty) types are defined in packages/web/src/global.web-auto-imports.ts
14
+ *
15
+ * Do not import types for hooks directly from Apollo here, unless it is an Apollo specific hook.
16
+ */
17
+
4
18
type DefaultUseQueryType = <
5
19
TData = any ,
6
20
TVariables extends OperationVariables = GraphQLOperationVariables
@@ -24,14 +38,29 @@ type DefaultUseSubscriptionType = <
24
38
subscription : DocumentNode ,
25
39
options ?: GraphQLSubscriptionHookOptions < TData , TVariables >
26
40
) => SubscriptionOperationResult < TData , TVariables >
41
+
42
+ type DefaultUseSuspenseType = <
43
+ TData = any ,
44
+ TVariables extends OperationVariables = GraphQLOperationVariables
45
+ > (
46
+ query : DocumentNode ,
47
+ options ?: GraphQLSuspenseQueryHookOptions < TData , TVariables >
48
+ ) => SuspenseQueryOperationResult < TData , TVariables >
49
+
27
50
export interface GraphQLHooks <
28
51
TuseQuery = DefaultUseQueryType ,
29
52
TuseMutation = DefaultUseMutationType ,
30
- TuseSubscription = DefaultUseSubscriptionType
53
+ TuseSubscription = DefaultUseSubscriptionType ,
54
+ TuseSuspenseQuery = DefaultUseSuspenseType
31
55
> {
32
56
useQuery : TuseQuery
33
57
useMutation : TuseMutation
34
58
useSubscription : TuseSubscription
59
+ useSuspenseQuery : TuseSuspenseQuery
60
+ // @NOTE note that we aren't using typeoverride here.
61
+ // This is because useBackgroundQuery and useReadQuery are apollo specific hooks.
62
+ useBackgroundQuery : typeof apolloUseBackgroundQuery
63
+ useReadQuery : typeof apolloUseReadQuery
35
64
}
36
65
37
66
export const GraphQLHooksContext = React . createContext < GraphQLHooks > ( {
@@ -50,13 +79,37 @@ export const GraphQLHooksContext = React.createContext<GraphQLHooks>({
50
79
'You must register a useSubscription hook via the `GraphQLHooksProvider`'
51
80
)
52
81
} ,
82
+ useSuspenseQuery : ( ) => {
83
+ throw new Error (
84
+ 'You must register a useSuspenseQuery hook via the `GraphQLHooksProvider`.'
85
+ )
86
+ } ,
87
+
88
+ // These are apollo specific hooks!
89
+ useBackgroundQuery : ( ) => {
90
+ throw new Error (
91
+ 'You must register a useBackgroundQuery hook via the `GraphQLHooksProvider`.'
92
+ )
93
+ } ,
94
+
95
+ useReadQuery : ( ) => {
96
+ throw new Error (
97
+ 'You must register a useReadQuery hook via the `GraphQLHooksProvider`.'
98
+ )
99
+ } ,
53
100
} )
54
101
55
102
interface GraphQlHooksProviderProps <
56
103
TuseQuery = DefaultUseQueryType ,
57
104
TuseMutation = DefaultUseMutationType ,
58
- TuseSubscription = DefaultUseSubscriptionType
59
- > extends GraphQLHooks < TuseQuery , TuseMutation , TuseSubscription > {
105
+ TuseSubscription = DefaultUseSubscriptionType ,
106
+ TuseSuspenseQuery = DefaultUseSuspenseType
107
+ > extends GraphQLHooks <
108
+ TuseQuery ,
109
+ TuseMutation ,
110
+ TuseSubscription ,
111
+ TuseSuspenseQuery
112
+ > {
60
113
children : React . ReactNode
61
114
}
62
115
@@ -74,6 +127,9 @@ export const GraphQLHooksProvider = <
74
127
useQuery,
75
128
useMutation,
76
129
useSubscription,
130
+ useSuspenseQuery,
131
+ useBackgroundQuery,
132
+ useReadQuery,
77
133
children,
78
134
} : GraphQlHooksProviderProps < TuseQuery , TuseMutation > ) => {
79
135
return (
@@ -82,6 +138,9 @@ export const GraphQLHooksProvider = <
82
138
useQuery,
83
139
useMutation,
84
140
useSubscription,
141
+ useSuspenseQuery,
142
+ useBackgroundQuery,
143
+ useReadQuery,
85
144
} }
86
145
>
87
146
{ children }
@@ -127,3 +186,29 @@ export function useSubscription<
127
186
TVariables
128
187
> ( query , options )
129
188
}
189
+
190
+ export function useSuspenseQuery <
191
+ TData = any ,
192
+ TVariables extends OperationVariables = GraphQLOperationVariables
193
+ > (
194
+ query : DocumentNode ,
195
+ options ?: GraphQLSuspenseQueryHookOptions < TData , TVariables >
196
+ ) : SuspenseQueryOperationResult < TData , TVariables > {
197
+ return React . useContext ( GraphQLHooksContext ) . useSuspenseQuery <
198
+ TData ,
199
+ TVariables
200
+ > ( query , options )
201
+ }
202
+
203
+ export const useBackgroundQuery : typeof apolloUseBackgroundQuery < any > = (
204
+ ...args
205
+ ) => {
206
+ // @TODO something about the apollo types here mean I need to override the return type
207
+ return React . useContext ( GraphQLHooksContext ) . useBackgroundQuery (
208
+ ...args
209
+ ) as any
210
+ }
211
+
212
+ export const useReadQuery : typeof apolloUseReadQuery = ( ...args ) => {
213
+ return React . useContext ( GraphQLHooksContext ) . useReadQuery ( ...args )
214
+ }
0 commit comments