houdini-react 2.0.0-next.11 → 2.0.0-next.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/bin/houdini-react +88 -0
  2. package/package.json +43 -22
  3. package/postInstall.js +353 -0
  4. package/runtime/client.ts +5 -0
  5. package/runtime/clientPlugin.ts +17 -0
  6. package/runtime/componentFields.ts +79 -0
  7. package/runtime/hooks/index.ts +9 -0
  8. package/runtime/hooks/useDeepCompareEffect.ts +91 -0
  9. package/runtime/hooks/useDocumentHandle.ts +232 -0
  10. package/runtime/hooks/useDocumentStore.ts +76 -0
  11. package/runtime/hooks/useDocumentSubscription.ts +62 -0
  12. package/runtime/hooks/useFragment.ts +93 -0
  13. package/runtime/hooks/useFragmentHandle.ts +46 -0
  14. package/runtime/hooks/useIsMounted.ts +14 -0
  15. package/runtime/hooks/useMutation.ts +70 -0
  16. package/runtime/hooks/useQuery.ts +12 -0
  17. package/runtime/hooks/useQueryHandle.ts +185 -0
  18. package/runtime/hooks/useSubscription.ts +12 -0
  19. package/runtime/hooks/useSubscriptionHandle.ts +33 -0
  20. package/runtime/hydration.tsx +155 -0
  21. package/runtime/index.tsx +49 -0
  22. package/runtime/manifest.ts +6 -0
  23. package/runtime/routing/Router.tsx +885 -0
  24. package/runtime/routing/cache.ts +54 -0
  25. package/runtime/routing/index.ts +2 -0
  26. package/server/index.d.ts +1 -0
  27. package/server/index.js +4 -0
  28. package/server/react-streaming.d.js +0 -0
  29. package/vite/index.d.ts +3 -0
  30. package/vite/index.js +284 -0
  31. package/vite/transform.d.ts +11 -0
  32. package/vite/transform.js +92 -0
  33. package/README.md +0 -36
  34. package/build/plugin/codegen/entries/documentWrappers.d.ts +0 -6
  35. package/build/plugin/codegen/entries/fallbacks.d.ts +0 -5
  36. package/build/plugin/codegen/entries/index.d.ts +0 -16
  37. package/build/plugin/codegen/entries/pages.d.ts +0 -2
  38. package/build/plugin/codegen/index.d.ts +0 -17
  39. package/build/plugin/codegen/manifest.d.ts +0 -5
  40. package/build/plugin/codegen/render.d.ts +0 -7
  41. package/build/plugin/codegen/router.d.ts +0 -7
  42. package/build/plugin/codegen/typeRoot.d.ts +0 -5
  43. package/build/plugin/config.d.ts +0 -4
  44. package/build/plugin/dedent.d.ts +0 -1
  45. package/build/plugin/extract.d.ts +0 -6
  46. package/build/plugin/index.d.ts +0 -5
  47. package/build/plugin/state.d.ts +0 -3
  48. package/build/plugin/transform.d.ts +0 -6
  49. package/build/plugin/vite.d.ts +0 -27
  50. package/build/plugin-cjs/index.js +0 -90122
  51. package/build/plugin-cjs/package.json +0 -1
  52. package/build/plugin-esm/index.js +0 -90118
  53. package/build/runtime/client.d.ts +0 -3
  54. package/build/runtime/clientPlugin.d.ts +0 -3
  55. package/build/runtime/componentFields.d.ts +0 -9
  56. package/build/runtime/hooks/index.d.ts +0 -8
  57. package/build/runtime/hooks/useDeepCompareEffect.d.ts +0 -35
  58. package/build/runtime/hooks/useDocumentHandle.d.ts +0 -36
  59. package/build/runtime/hooks/useDocumentStore.d.ts +0 -11
  60. package/build/runtime/hooks/useDocumentSubscription.d.ts +0 -11
  61. package/build/runtime/hooks/useFragment.d.ts +0 -16
  62. package/build/runtime/hooks/useFragmentHandle.d.ts +0 -8
  63. package/build/runtime/hooks/useIsMounted.d.ts +0 -3
  64. package/build/runtime/hooks/useMutation.d.ts +0 -14
  65. package/build/runtime/hooks/useQuery.d.ts +0 -5
  66. package/build/runtime/hooks/useQueryHandle.d.ts +0 -10
  67. package/build/runtime/hooks/useSubscription.d.ts +0 -4
  68. package/build/runtime/hooks/useSubscriptionHandle.d.ts +0 -25
  69. package/build/runtime/index.d.ts +0 -14
  70. package/build/runtime/manifest.d.ts +0 -3
  71. package/build/runtime/routing/Router.d.ts +0 -62
  72. package/build/runtime/routing/cache.d.ts +0 -7
  73. package/build/runtime/routing/hooks.d.ts +0 -40
  74. package/build/runtime/routing/index.d.ts +0 -3
  75. package/build/runtime-cjs/client.d.ts +0 -3
  76. package/build/runtime-cjs/client.js +0 -25
  77. package/build/runtime-cjs/clientPlugin.d.ts +0 -3
  78. package/build/runtime-cjs/clientPlugin.js +0 -37
  79. package/build/runtime-cjs/componentFields.d.ts +0 -9
  80. package/build/runtime-cjs/componentFields.js +0 -83
  81. package/build/runtime-cjs/hooks/index.d.ts +0 -8
  82. package/build/runtime-cjs/hooks/index.js +0 -45
  83. package/build/runtime-cjs/hooks/useDeepCompareEffect.d.ts +0 -35
  84. package/build/runtime-cjs/hooks/useDeepCompareEffect.js +0 -76
  85. package/build/runtime-cjs/hooks/useDocumentHandle.d.ts +0 -36
  86. package/build/runtime-cjs/hooks/useDocumentHandle.js +0 -177
  87. package/build/runtime-cjs/hooks/useDocumentStore.d.ts +0 -11
  88. package/build/runtime-cjs/hooks/useDocumentStore.js +0 -76
  89. package/build/runtime-cjs/hooks/useDocumentSubscription.d.ts +0 -11
  90. package/build/runtime-cjs/hooks/useDocumentSubscription.js +0 -76
  91. package/build/runtime-cjs/hooks/useFragment.d.ts +0 -16
  92. package/build/runtime-cjs/hooks/useFragment.js +0 -102
  93. package/build/runtime-cjs/hooks/useFragmentHandle.d.ts +0 -8
  94. package/build/runtime-cjs/hooks/useFragmentHandle.js +0 -47
  95. package/build/runtime-cjs/hooks/useIsMounted.d.ts +0 -3
  96. package/build/runtime-cjs/hooks/useIsMounted.js +0 -38
  97. package/build/runtime-cjs/hooks/useMutation.d.ts +0 -14
  98. package/build/runtime-cjs/hooks/useMutation.js +0 -67
  99. package/build/runtime-cjs/hooks/useQuery.d.ts +0 -5
  100. package/build/runtime-cjs/hooks/useQuery.js +0 -32
  101. package/build/runtime-cjs/hooks/useQueryHandle.d.ts +0 -10
  102. package/build/runtime-cjs/hooks/useQueryHandle.js +0 -131
  103. package/build/runtime-cjs/hooks/useSubscription.d.ts +0 -4
  104. package/build/runtime-cjs/hooks/useSubscription.js +0 -32
  105. package/build/runtime-cjs/hooks/useSubscriptionHandle.d.ts +0 -25
  106. package/build/runtime-cjs/hooks/useSubscriptionHandle.js +0 -42
  107. package/build/runtime-cjs/index.d.ts +0 -14
  108. package/build/runtime-cjs/index.js +0 -88
  109. package/build/runtime-cjs/manifest.d.ts +0 -3
  110. package/build/runtime-cjs/manifest.js +0 -25
  111. package/build/runtime-cjs/package.json +0 -1
  112. package/build/runtime-cjs/routing/Router.d.ts +0 -62
  113. package/build/runtime-cjs/routing/Router.js +0 -540
  114. package/build/runtime-cjs/routing/cache.d.ts +0 -7
  115. package/build/runtime-cjs/routing/cache.js +0 -61
  116. package/build/runtime-cjs/routing/hooks.d.ts +0 -40
  117. package/build/runtime-cjs/routing/hooks.js +0 -93
  118. package/build/runtime-cjs/routing/index.d.ts +0 -3
  119. package/build/runtime-cjs/routing/index.js +0 -33
  120. package/build/runtime-esm/client.d.ts +0 -3
  121. package/build/runtime-esm/client.js +0 -5
  122. package/build/runtime-esm/clientPlugin.d.ts +0 -3
  123. package/build/runtime-esm/clientPlugin.js +0 -17
  124. package/build/runtime-esm/componentFields.d.ts +0 -9
  125. package/build/runtime-esm/componentFields.js +0 -59
  126. package/build/runtime-esm/hooks/index.d.ts +0 -8
  127. package/build/runtime-esm/hooks/index.js +0 -15
  128. package/build/runtime-esm/hooks/useDeepCompareEffect.d.ts +0 -35
  129. package/build/runtime-esm/hooks/useDeepCompareEffect.js +0 -41
  130. package/build/runtime-esm/hooks/useDocumentHandle.d.ts +0 -36
  131. package/build/runtime-esm/hooks/useDocumentHandle.js +0 -143
  132. package/build/runtime-esm/hooks/useDocumentStore.d.ts +0 -11
  133. package/build/runtime-esm/hooks/useDocumentStore.js +0 -42
  134. package/build/runtime-esm/hooks/useDocumentSubscription.d.ts +0 -11
  135. package/build/runtime-esm/hooks/useDocumentSubscription.js +0 -42
  136. package/build/runtime-esm/hooks/useFragment.d.ts +0 -16
  137. package/build/runtime-esm/hooks/useFragment.js +0 -67
  138. package/build/runtime-esm/hooks/useFragmentHandle.d.ts +0 -8
  139. package/build/runtime-esm/hooks/useFragmentHandle.js +0 -23
  140. package/build/runtime-esm/hooks/useIsMounted.d.ts +0 -3
  141. package/build/runtime-esm/hooks/useIsMounted.js +0 -14
  142. package/build/runtime-esm/hooks/useMutation.d.ts +0 -14
  143. package/build/runtime-esm/hooks/useMutation.js +0 -42
  144. package/build/runtime-esm/hooks/useQuery.d.ts +0 -5
  145. package/build/runtime-esm/hooks/useQuery.js +0 -8
  146. package/build/runtime-esm/hooks/useQueryHandle.d.ts +0 -10
  147. package/build/runtime-esm/hooks/useQueryHandle.js +0 -97
  148. package/build/runtime-esm/hooks/useSubscription.d.ts +0 -4
  149. package/build/runtime-esm/hooks/useSubscription.js +0 -8
  150. package/build/runtime-esm/hooks/useSubscriptionHandle.d.ts +0 -25
  151. package/build/runtime-esm/hooks/useSubscriptionHandle.js +0 -18
  152. package/build/runtime-esm/index.d.ts +0 -14
  153. package/build/runtime-esm/index.js +0 -48
  154. package/build/runtime-esm/manifest.d.ts +0 -3
  155. package/build/runtime-esm/manifest.js +0 -5
  156. package/build/runtime-esm/routing/Router.d.ts +0 -62
  157. package/build/runtime-esm/routing/Router.js +0 -499
  158. package/build/runtime-esm/routing/cache.d.ts +0 -7
  159. package/build/runtime-esm/routing/cache.js +0 -36
  160. package/build/runtime-esm/routing/hooks.d.ts +0 -40
  161. package/build/runtime-esm/routing/hooks.js +0 -53
  162. package/build/runtime-esm/routing/index.d.ts +0 -3
  163. package/build/runtime-esm/routing/index.js +0 -6
  164. package/build/server/index.d.ts +0 -1
  165. package/build/server-cjs/index.js +0 -28
  166. package/build/server-cjs/package.json +0 -1
  167. package/build/server-esm/index.js +0 -4
  168. /package/{build/plugin-esm → runtime}/package.json +0 -0
  169. /package/{build/runtime-esm → server}/package.json +0 -0
  170. /package/{build/server-esm → vite}/package.json +0 -0
@@ -0,0 +1,91 @@
1
+ import { deepEquals } from 'houdini/runtime'
2
+ import * as React from 'react'
3
+
4
+ // This file is largely a copy and paste from Kent C. Dodd's use-deep-compare-effect (appropriate license at the bottom).
5
+ // It has been copied locally in order to avoid any awkward third party peer dependencies
6
+ // on generated files (which would make the install annoying). The deep equals library has
7
+ // also been changed to use one that was already included in the runtime (avoiding the extra bundle size)
8
+
9
+ type UseEffectParams = Parameters<typeof React.useEffect>
10
+ type EffectCallback = UseEffectParams[0]
11
+ type DependencyList = UseEffectParams[1]
12
+ // yes, I know it's void, but I like what this communicates about
13
+ // the intent of these functions: It's just like useEffect
14
+ type UseEffectReturn = ReturnType<typeof React.useEffect>
15
+
16
+ function checkDeps(deps: DependencyList) {
17
+ if (!deps?.length) {
18
+ throw new Error(
19
+ 'useDeepCompareEffect should not be used with no dependencies. Use React.useEffect instead.'
20
+ )
21
+ }
22
+ if (deps.every(isPrimitive)) {
23
+ throw new Error(
24
+ 'useDeepCompareEffect should not be used with dependencies that are all primitive values. Use React.useEffect instead.'
25
+ )
26
+ }
27
+ }
28
+
29
+ function isPrimitive(val: unknown) {
30
+ return val == null || /^[sbn]/.test(typeof val)
31
+ }
32
+
33
+ /**
34
+ * @param value the value to be memoized (usually a dependency list)
35
+ * @returns a memoized version of the value as long as it remains deeply equal
36
+ */
37
+ export function useDeepCompareMemoize<T>(value: T) {
38
+ const ref = React.useRef<T>(value)
39
+ const signalRef = React.useRef<number>(0)
40
+
41
+ if (!deepEquals(value, ref.current)) {
42
+ ref.current = value
43
+ signalRef.current += 1
44
+ }
45
+
46
+ return React.useMemo(() => ref.current, [])
47
+ }
48
+
49
+ function useDeepCompareEffect(
50
+ callback: EffectCallback,
51
+ dependencies: DependencyList
52
+ ): UseEffectReturn {
53
+ if (process.env.NODE_ENV !== 'production') {
54
+ checkDeps(dependencies)
55
+ }
56
+ // biome-ignore lint/correctness/useExhaustiveDependencies: intentional deep-compare effect
57
+ return React.useEffect(callback, useDeepCompareMemoize(dependencies))
58
+ }
59
+
60
+ export function useDeepCompareEffectNoCheck(
61
+ callback: EffectCallback,
62
+ dependencies: DependencyList
63
+ ): UseEffectReturn {
64
+ // biome-ignore lint/correctness/useExhaustiveDependencies: intentional deep-compare effect
65
+ return React.useEffect(callback, useDeepCompareMemoize(dependencies))
66
+ }
67
+
68
+ export default useDeepCompareEffect
69
+
70
+ /**
71
+ The MIT License (MIT)
72
+ Copyright (c) 2020 Kent C. Dodds
73
+
74
+ Permission is hereby granted, free of charge, to any person obtaining a copy
75
+ of this software and associated documentation files (the "Software"), to deal
76
+ in the Software without restriction, including without limitation the rights
77
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
78
+ copies of the Software, and to permit persons to whom the Software is
79
+ furnished to do so, subject to the following conditions:
80
+
81
+ The above copyright notice and this permission notice shall be included in all
82
+ copies or substantial portions of the Software.
83
+
84
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
85
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
86
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
87
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
88
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
89
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
90
+ SOFTWARE.
91
+ */
@@ -0,0 +1,232 @@
1
+ import { extractPageInfo } from 'houdini/runtime'
2
+ import { cursorHandlers, offsetHandlers } from 'houdini/runtime'
3
+ import { ArtifactKind } from 'houdini/runtime'
4
+ import type {
5
+ GraphQLObject,
6
+ GraphQLVariables,
7
+ CursorHandlers,
8
+ OffsetHandlers,
9
+ PageInfo,
10
+ FetchFn,
11
+ QueryResult,
12
+ DocumentArtifact,
13
+ QueryArtifact,
14
+ } from 'houdini/runtime'
15
+ import type { DocumentStore } from 'houdini/runtime/client'
16
+ import React from 'react'
17
+
18
+ import { useClient, useLocation, useSession } from '../routing/Router'
19
+
20
+ export function useDocumentHandle<
21
+ _Artifact extends QueryArtifact,
22
+ _Data extends GraphQLObject,
23
+ _Input extends GraphQLVariables,
24
+ >({
25
+ artifact,
26
+ observer,
27
+ storeValue,
28
+ }: {
29
+ artifact: DocumentArtifact
30
+ observer: DocumentStore<_Data, _Input>
31
+ storeValue: QueryResult<_Data, _Input>
32
+ }): DocumentHandle<_Artifact, _Data, _Input> & { fetch: FetchFn<_Data, _Input> } {
33
+ const [forwardPending, setForwardPending] = React.useState(false)
34
+ const [backwardPending, setBackwardPending] = React.useState(false)
35
+ const location = useLocation()
36
+
37
+ // grab the current session value
38
+ const [session] = useSession()
39
+
40
+ // we want to use a separate observer for pagination queries
41
+ const client = useClient()
42
+ const paginationObserver = React.useMemo(() => {
43
+ // if the artifact doesn't support pagination, don't do anything
44
+ if (!artifact.refetch?.paginated) {
45
+ return null
46
+ }
47
+
48
+ return client.observe<_Data, _Input>({ artifact })
49
+ }, [artifact.name, artifact, client.observe])
50
+
51
+ // @ts-expect-error: avoiding an as DocumentHandle<_Artifact, _Data, _Input>
52
+ return React.useMemo<DocumentHandle<_Artifact, _Data, _Input>>(() => {
53
+ const wrapLoad = <_Result>(
54
+ setLoading: (val: boolean) => void,
55
+ fn: (value: any) => Promise<_Result>
56
+ ) => {
57
+ return async (value: any) => {
58
+ setLoading(true)
59
+ let result: _Result | null = null
60
+ let err: Error | null = null
61
+ try {
62
+ result = await fn(value)
63
+ } catch (e) {
64
+ err = e as Error
65
+ }
66
+ setLoading(false)
67
+ // ignore abort errors when loading pages
68
+ if (err && err.name !== 'AbortError') {
69
+ throw err
70
+ }
71
+
72
+ // we're done
73
+ return result || observer.state
74
+ }
75
+ }
76
+
77
+ // add the session value to the
78
+ const fetchQuery: FetchFn<_Data, _Input> = (args) => {
79
+ // before we send the query, we need to figure out which variables are
80
+ // actually useful for this document
81
+ const usedVariables = Object.fromEntries(
82
+ Object.keys(observer.artifact.input?.fields ?? {}).reduce<[string, any][]>(
83
+ (entries, fieldName) => {
84
+ // if the field is not a url parameter, skip it
85
+ if (!(fieldName in location.params)) {
86
+ return entries
87
+ }
88
+
89
+ return [...entries, [fieldName, location.params[fieldName]]]
90
+ },
91
+ []
92
+ )
93
+ )
94
+
95
+ return observer.send({
96
+ ...args,
97
+ variables: {
98
+ ...usedVariables,
99
+ ...args?.variables,
100
+ },
101
+ session,
102
+ })
103
+ }
104
+
105
+ const baseFields = {
106
+ artifact,
107
+ data: storeValue.data,
108
+ variables: storeValue.variables,
109
+ partial: storeValue.partial,
110
+ fetching: storeValue.fetching,
111
+ errors: storeValue.errors,
112
+ }
113
+
114
+ // only consider paginated queries
115
+ if (artifact.kind !== ArtifactKind.Query || !artifact.refetch?.paginated) {
116
+ return {
117
+ ...baseFields,
118
+ fetch: fetchQuery,
119
+ }
120
+ }
121
+
122
+ // if the artifact supports cursor pagination, then add the cursor handlers
123
+ if (artifact.refetch.method === 'cursor') {
124
+ const handlers = cursorHandlers<_Data, _Input>({
125
+ artifact,
126
+ getState: () => storeValue.data,
127
+ getVariables: () => storeValue.variables!,
128
+ fetch: fetchQuery,
129
+ fetchUpdate: (args, updates) => {
130
+ return paginationObserver!.send({
131
+ ...args,
132
+ cacheParams: {
133
+ ...args?.cacheParams,
134
+ disableSubscriptions: true,
135
+ applyUpdates: updates,
136
+ },
137
+ session,
138
+ })
139
+ },
140
+ getSession: async () => session,
141
+ })
142
+
143
+ return {
144
+ ...baseFields,
145
+ fetch: handlers.fetch,
146
+ loadNext: wrapLoad(setForwardPending, handlers.loadNextPage),
147
+ loadNextPending: forwardPending,
148
+ loadPrevious: wrapLoad(setBackwardPending, handlers.loadPreviousPage),
149
+ loadPreviousPending: backwardPending,
150
+ pageInfo: extractPageInfo(storeValue.data, artifact.refetch!.path),
151
+ }
152
+ }
153
+
154
+ if (artifact.refetch.method === 'offset') {
155
+ const handlers = offsetHandlers({
156
+ artifact,
157
+ getState: () => storeValue.data,
158
+ getVariables: () => storeValue.variables!,
159
+ storeName: artifact.name,
160
+ fetch: fetchQuery,
161
+ fetchUpdate: async (args, updates = ['append']) => {
162
+ return paginationObserver!.send({
163
+ ...args,
164
+ cacheParams: {
165
+ disableSubscriptions: true,
166
+ applyUpdates: updates,
167
+ ...args?.cacheParams,
168
+ },
169
+ })
170
+ },
171
+ getSession: async () => session,
172
+ })
173
+
174
+ return {
175
+ ...baseFields,
176
+ fetch: handlers.fetch,
177
+ loadNext: wrapLoad(setForwardPending, handlers.loadNextPage),
178
+ loadNextPending: forwardPending,
179
+ }
180
+ }
181
+
182
+ // we don't want to add anything
183
+ return {
184
+ ...baseFields,
185
+ fetch: fetchQuery,
186
+ refetch: fetchQuery,
187
+ }
188
+ }, [
189
+ artifact,
190
+ observer,
191
+ session,
192
+ storeValue,
193
+ backwardPending,
194
+ forwardPending,
195
+ paginationObserver!.send,
196
+ location.params,
197
+ ])
198
+ }
199
+
200
+ export type DocumentHandle<
201
+ _Artifact extends QueryArtifact,
202
+ _Data extends GraphQLObject = GraphQLObject,
203
+ _Input extends GraphQLVariables = GraphQLVariables,
204
+ > = {
205
+ data: _Data
206
+ partial: boolean
207
+ fetching: boolean
208
+ errors: { message: string }[] | null
209
+ fetch: FetchFn<_Data, Partial<_Input>>
210
+ variables: _Input
211
+ } & RefetchHandlers<_Artifact, _Data, _Input>
212
+
213
+ type RefetchHandlers<_Artifact extends QueryArtifact, _Data extends GraphQLObject, _Input> =
214
+ // we need to add different methods if the artifact supports cursor pagination
215
+ _Artifact extends {
216
+ refetch: { paginated: true; method: 'cursor' }
217
+ }
218
+ ? {
219
+ loadNext: CursorHandlers<_Data, _Input>['loadNextPage']
220
+ loadNextPending: boolean
221
+ loadPrevious: CursorHandlers<_Data, _Input>['loadPreviousPage']
222
+ loadPreviousPending: boolean
223
+ pageInfo: PageInfo
224
+ }
225
+ : // offset pagination
226
+ _Artifact extends { refetch: { paginated: true; method: 'offset' } }
227
+ ? {
228
+ loadNext: OffsetHandlers<_Data, _Input>['loadNextPage']
229
+ loadNextPending: boolean
230
+ }
231
+ : // the artifact does not support a known pagination method, don't add anything
232
+ {}
@@ -0,0 +1,76 @@
1
+ import type {
2
+ DocumentArtifact,
3
+ GraphQLVariables,
4
+ QueryResult,
5
+ GraphQLObject,
6
+ } from 'houdini/runtime'
7
+ import type { DocumentStore, ObserveParams } from 'houdini/runtime/client'
8
+ import * as React from 'react'
9
+
10
+ import { useClient } from '../routing'
11
+ import { useIsMountedRef } from './useIsMounted'
12
+
13
+ export type UseDocumentStoreParams<
14
+ _Artifact extends DocumentArtifact,
15
+ _Data extends GraphQLObject,
16
+ _Input extends GraphQLVariables,
17
+ > = {
18
+ artifact: _Artifact
19
+ observer?: DocumentStore<_Data, _Input>
20
+ } & Partial<ObserveParams<_Data, DocumentArtifact, _Input>>
21
+
22
+ export function useDocumentStore<
23
+ _Data extends GraphQLObject = GraphQLObject,
24
+ _Input extends GraphQLVariables = GraphQLVariables,
25
+ _Artifact extends DocumentArtifact = DocumentArtifact,
26
+ >({
27
+ artifact,
28
+ observer: obs,
29
+ ...observeParams
30
+ }: UseDocumentStoreParams<_Artifact, _Data, _Input>): [
31
+ QueryResult<_Data, _Input>,
32
+ DocumentStore<_Data, _Input>,
33
+ ] {
34
+ const client = useClient()
35
+ const isMountedRef = useIsMountedRef()
36
+
37
+ // hold onto an observer we'll use
38
+ const [observer, setObserver] = React.useState(
39
+ () =>
40
+ obs ??
41
+ client.observe<_Data, _Input>({
42
+ artifact,
43
+ ...observeParams,
44
+ })
45
+ )
46
+
47
+ const box = React.useRef(observer.state)
48
+
49
+ // if the observer changes, we need to track the new one
50
+ if (obs && obs !== observer) {
51
+ box.current = obs.state
52
+ setObserver(obs)
53
+ }
54
+
55
+ // the function that registers a new subscription for the observer
56
+ const subscribe: any = React.useCallback(
57
+ (fn: () => void) => {
58
+ return observer.subscribe((val) => {
59
+ box.current = val
60
+ if (isMountedRef.current) {
61
+ fn()
62
+ }
63
+ })
64
+ },
65
+ [observer, isMountedRef.current]
66
+ )
67
+
68
+ // get a safe reference to the cache
69
+ const storeValue = React.useSyncExternalStore(
70
+ subscribe,
71
+ () => box.current,
72
+ () => box.current
73
+ )
74
+
75
+ return [storeValue!, observer]
76
+ }
@@ -0,0 +1,62 @@
1
+ import type {
2
+ DocumentArtifact,
3
+ GraphQLVariables,
4
+ QueryResult,
5
+ GraphQLObject,
6
+ } from 'houdini/runtime'
7
+ import type { DocumentStore, SendParams } from 'houdini/runtime/client'
8
+
9
+ import { useSession } from '../routing/Router'
10
+ import useDeepCompareEffect from './useDeepCompareEffect'
11
+ import { useDocumentStore, type UseDocumentStoreParams } from './useDocumentStore'
12
+
13
+ export function useDocumentSubscription<
14
+ _Artifact extends DocumentArtifact = DocumentArtifact,
15
+ _Data extends GraphQLObject = GraphQLObject,
16
+ _Input extends GraphQLVariables = GraphQLVariables,
17
+ >({
18
+ artifact,
19
+ variables,
20
+ send,
21
+ disabled,
22
+ ...observeParams
23
+ }: UseDocumentStoreParams<_Artifact, _Data, _Input> & {
24
+ variables: _Input
25
+ disabled?: boolean
26
+ send?: Partial<SendParams>
27
+ }): [QueryResult<_Data, _Input> & { parent?: string | null }, DocumentStore<_Data, _Input>] {
28
+ const [storeValue, observer] = useDocumentStore<_Data, _Input>({
29
+ artifact,
30
+ ...observeParams,
31
+ })
32
+
33
+ // grab the current session value
34
+ const [session] = useSession()
35
+
36
+ // whenever the variables change, we need to retrigger the query
37
+ useDeepCompareEffect(() => {
38
+ if (!disabled) {
39
+ observer.send({
40
+ variables,
41
+ session,
42
+ // TODO: metadata
43
+ metadata: {},
44
+ ...send,
45
+ })
46
+ }
47
+
48
+ return () => {
49
+ if (!disabled) {
50
+ observer.cleanup()
51
+ }
52
+ }
53
+ }, [disabled, session, observer, variables ?? {}, send ?? {}])
54
+
55
+ return [
56
+ {
57
+ parent: send?.stuff?.parentID,
58
+ ...storeValue,
59
+ },
60
+ observer,
61
+ ]
62
+ }
@@ -0,0 +1,93 @@
1
+ import { deepEquals } from 'houdini/runtime'
2
+ import { fragmentKey } from 'houdini/runtime'
3
+ import type { GraphQLObject, GraphQLVariables, FragmentArtifact } from 'houdini/runtime'
4
+ import * as React from 'react'
5
+
6
+ import { useRouterContext } from '../routing'
7
+ import { useDocumentSubscription } from './useDocumentSubscription'
8
+
9
+ export function useFragment<
10
+ _Data extends GraphQLObject,
11
+ _ReferenceType extends {},
12
+ _Input extends GraphQLVariables = GraphQLVariables,
13
+ >(
14
+ reference: _Data | { ' $fragments': _ReferenceType } | null,
15
+ document: { artifact: FragmentArtifact }
16
+ ): _Data | null {
17
+ const { cache } = useRouterContext()
18
+
19
+ // get the fragment reference info
20
+ const { parent, variables, loading } = fragmentReference<_Data, _Input, _ReferenceType>(
21
+ reference,
22
+ document
23
+ )
24
+
25
+ // if we got this far then we are safe to use the fields on the object
26
+ let cachedValue = reference as _Data | null
27
+
28
+ // on the client, we want to ensure that we apply masking to the initial value by
29
+ // loading the value from cache
30
+ if (reference && parent) {
31
+ cachedValue = cache.read({
32
+ selection: document.artifact.selection,
33
+ parent,
34
+ variables,
35
+ loading,
36
+ }).data as _Data
37
+ }
38
+
39
+ // we're ready to setup the live document
40
+ const [storeValue] = useDocumentSubscription<FragmentArtifact, _Data, _Input>({
41
+ artifact: document.artifact,
42
+ variables,
43
+ initialValue: cachedValue,
44
+ // dont subscribe to anything if we are loading
45
+ disabled: loading,
46
+ send: {
47
+ stuff: {
48
+ parentID: parent,
49
+ },
50
+ setup: true,
51
+ },
52
+ })
53
+
54
+ // the parent has changed, we need to use initialValue for this render
55
+ // if we don't, then there is a very brief flash where we will show the old data
56
+ // before the store has had a chance to update
57
+ const lastReference = React.useRef<{ parent: string; variables: _Input } | null>(null)
58
+ return React.useMemo(() => {
59
+ // if the parent reference has changed we need to always prefer the cached value
60
+ const parentChange =
61
+ storeValue.parent !== parent ||
62
+ !deepEquals({ parent, variables }, lastReference.current)
63
+ if (parentChange) {
64
+ // make sure we keep track of the last reference we used
65
+ lastReference.current = { parent, variables: { ...variables } }
66
+
67
+ // and use the cached value
68
+ return cachedValue
69
+ }
70
+
71
+ return storeValue.data
72
+ }, [variables, parent, storeValue.parent, storeValue.data, cachedValue])
73
+ }
74
+
75
+ export function fragmentReference<_Data extends GraphQLObject, _Input, _ReferenceType extends {}>(
76
+ reference: _Data | { ' $fragments': _ReferenceType } | null,
77
+ document: { artifact: FragmentArtifact }
78
+ ): { variables: _Input; parent: string; loading: boolean } {
79
+ // @ts-expect-error: typescript can't guarantee that the fragment key is defined
80
+ // but if its not, then the fragment wasn't mixed into the right thing
81
+ // the variables for the fragment live on the initial value's $fragment key
82
+ const { variables, parent } = reference?.[fragmentKey]?.values?.[document.artifact.name] ?? {}
83
+ if (reference && fragmentKey in reference && (!variables || !parent)) {
84
+ console.warn(
85
+ `⚠️ Parent does not contain the information for this fragment. Something is wrong.
86
+ Please ensure that you have passed a record that has ${document.artifact.name} mixed into it.`
87
+ )
88
+ }
89
+ // @ts-expect-error
90
+ const loading = Boolean(reference?.[fragmentKey]?.loading)
91
+
92
+ return { variables, parent, loading }
93
+ }
@@ -0,0 +1,46 @@
1
+ import type {
2
+ GraphQLObject,
3
+ FragmentArtifact,
4
+ QueryArtifact,
5
+ GraphQLVariables,
6
+ } from 'houdini/runtime'
7
+
8
+ import { useDocumentHandle } from './useDocumentHandle'
9
+ import { useDocumentStore } from './useDocumentStore'
10
+ import { fragmentReference, useFragment } from './useFragment'
11
+
12
+ // useFragmentHandle is just like useFragment except it also returns an imperative handle
13
+ // that users can use to interact with the fragment
14
+ export function useFragmentHandle<
15
+ _Artifact extends FragmentArtifact,
16
+ _Data extends GraphQLObject,
17
+ _ReferenceType extends {},
18
+ _PaginationArtifact extends QueryArtifact,
19
+ _Input extends GraphQLVariables = GraphQLVariables,
20
+ >(
21
+ reference: _Data | { ' $fragments': _ReferenceType } | null,
22
+ document: { artifact: FragmentArtifact; refetchArtifact?: QueryArtifact }
23
+ ): any {
24
+ // get the fragment values
25
+ const data = useFragment<_Data, _ReferenceType, _Input>(reference, document)
26
+
27
+ // look at the fragment reference to get the variables
28
+ const { variables } = fragmentReference<_Data, _Input, _ReferenceType>(reference, document)
29
+
30
+ // use the pagination fragment for meta data if it exists.
31
+ // if we pass this a fragment artifact, it won't add any data
32
+ const [handleValue, handleObserver] = useDocumentStore<_Data, _Input>({
33
+ artifact: document.refetchArtifact ?? document.artifact,
34
+ })
35
+ const handle = useDocumentHandle<_PaginationArtifact, _Data, _Input>({
36
+ observer: handleObserver,
37
+ storeValue: handleValue,
38
+ artifact: document.refetchArtifact ?? document.artifact,
39
+ })
40
+
41
+ return {
42
+ ...handle,
43
+ variables,
44
+ data,
45
+ }
46
+ }
@@ -0,0 +1,14 @@
1
+ import { useRef, useEffect } from 'react'
2
+
3
+ export function useIsMountedRef(): { current: boolean } {
4
+ const isMountedRef = useRef(true)
5
+
6
+ useEffect(() => {
7
+ isMountedRef.current = true
8
+ return () => {
9
+ isMountedRef.current = false
10
+ }
11
+ }, [])
12
+
13
+ return isMountedRef
14
+ }
@@ -0,0 +1,70 @@
1
+ import type {
2
+ MutationArtifact,
3
+ GraphQLObject,
4
+ QueryResult,
5
+ GraphQLVariables,
6
+ } from 'houdini/runtime'
7
+
8
+ import { useSession } from '../routing/Router'
9
+ import { useDocumentStore } from './useDocumentStore'
10
+
11
+ export type MutationHandler<_Result, _Input, _Optimistic extends GraphQLObject> = (args: {
12
+ variables: _Input
13
+
14
+ metadata?: App.Metadata
15
+ fetch?: typeof globalThis.fetch
16
+ optimisticResponse?: _Optimistic
17
+ abortController?: AbortController
18
+ }) => Promise<void>
19
+
20
+ export function useMutation<
21
+ _Result extends GraphQLObject,
22
+ _Input extends GraphQLVariables,
23
+ _Optimistic extends GraphQLObject,
24
+ >({
25
+ artifact,
26
+ }: {
27
+ artifact: MutationArtifact
28
+ }): [boolean, MutationHandler<_Result, _Input, _Optimistic>] {
29
+ // build the live document we'll use to send values
30
+ const [storeValue, observer] = useDocumentStore<_Result, _Input>({ artifact })
31
+
32
+ // grab the pending state from the document store
33
+ const pending = storeValue.fetching
34
+
35
+ // grab the current session value
36
+ const [session] = useSession()
37
+
38
+ // sending the mutation just means invoking the observer's send method
39
+ const mutate: MutationHandler<_Result, _Input, _Optimistic> = async ({
40
+ metadata,
41
+ fetch,
42
+ variables,
43
+ abortController,
44
+ ...mutationConfig
45
+ }) => {
46
+ const result = await observer.send({
47
+ variables,
48
+ metadata,
49
+ session,
50
+ abortController,
51
+ stuff: {
52
+ ...mutationConfig,
53
+ },
54
+ })
55
+
56
+ if (result.errors && result.errors.length > 0) {
57
+ const err = new RuntimeGraphQLError(
58
+ result.errors.map((error) => error.message).join('. ')
59
+ )
60
+ err.raw = result.errors
61
+ throw err
62
+ }
63
+ }
64
+
65
+ return [pending, mutate]
66
+ }
67
+
68
+ export class RuntimeGraphQLError extends Error {
69
+ raw: QueryResult['errors'] = []
70
+ }
@@ -0,0 +1,12 @@
1
+ import type { GraphQLObject, QueryArtifact } from 'houdini/runtime'
2
+
3
+ import type { UseQueryConfig } from './useQueryHandle'
4
+ import { useQueryHandle } from './useQueryHandle'
5
+
6
+ export function useQuery<
7
+ _Artifact extends QueryArtifact,
8
+ _Data extends GraphQLObject = GraphQLObject,
9
+ >(document: { artifact: _Artifact }, variables: any = null, config: UseQueryConfig = {}): _Data {
10
+ const { data } = useQueryHandle<_Artifact, _Data>(document, variables, config)
11
+ return data as unknown as _Data
12
+ }