houdini-react 2.0.0-go.1 → 2.0.0-go.10

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.
@@ -0,0 +1,79 @@
1
+ import { getFieldsForType } from 'houdini/runtime'
2
+ import type { DocumentArtifact, GraphQLObject, GraphQLValue } from 'houdini/runtime'
3
+ import { defaultComponentField, type Cache } from 'houdini/runtime/cache'
4
+
5
+ export function injectComponents({
6
+ cache,
7
+ selection,
8
+ data,
9
+ variables,
10
+ parentType = 'Query',
11
+ }: {
12
+ cache: Cache
13
+ selection: DocumentArtifact['selection']
14
+ data: GraphQLValue | null
15
+ variables: Record<string, GraphQLValue> | undefined | null
16
+ parentType?: string
17
+ }) {
18
+ // if the value is null, we're done
19
+ if (data === null) {
20
+ return
21
+ }
22
+
23
+ // if the value is not an object (ie its a scalar) we're done
24
+ if (typeof data !== 'object') {
25
+ return
26
+ }
27
+ data = data as GraphQLObject
28
+
29
+ // if the value is an array we need to instantiate each item
30
+ if (Array.isArray(data)) {
31
+ data.forEach((item) =>
32
+ injectComponents({
33
+ cache,
34
+ selection,
35
+ data: item,
36
+ variables,
37
+ parentType,
38
+ })
39
+ )
40
+ return
41
+ }
42
+
43
+ // if the object has a subselection we need to walk down
44
+ const typename = '__typename' in data ? (data['__typename'] as string) : ''
45
+ const fields = getFieldsForType(selection, typename, false)
46
+ if (!fields) {
47
+ return
48
+ }
49
+
50
+ // walk down each field
51
+ for (const [field, subSelection] of Object.entries(fields)) {
52
+ // if the field is a component then we need to assign the value to the target
53
+ if (subSelection.component) {
54
+ // if the component is already in the cache, we're done
55
+ if (!cache._internal_unstable.componentCache[subSelection.component.key]) {
56
+ continue
57
+ }
58
+
59
+ data[field] = defaultComponentField({
60
+ variables,
61
+ parent: cache._internal_unstable.id(parentType, data) ?? '',
62
+ cache,
63
+ component: subSelection.component,
64
+ }) as any as GraphQLValue
65
+ }
66
+
67
+ // if there is a selection, we need to walk down
68
+ const dataValue = data[field]
69
+ if (subSelection.selection) {
70
+ injectComponents({
71
+ cache,
72
+ selection: subSelection.selection,
73
+ data: dataValue,
74
+ variables,
75
+ parentType: subSelection.type,
76
+ })
77
+ }
78
+ }
79
+ }
@@ -0,0 +1,9 @@
1
+ export { useQuery } from './useQuery'
2
+ export { useQueryHandle } from './useQueryHandle'
3
+ export { useFragment } from './useFragment'
4
+ export { useFragmentHandle } from './useFragmentHandle'
5
+ export { useMutation } from './useMutation'
6
+ export { useSubscription } from './useSubscription'
7
+
8
+ export { type DocumentHandle } from './useDocumentHandle'
9
+ export { type UseQueryConfig } from './useQueryHandle'
@@ -0,0 +1,89 @@
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 || !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, [signalRef.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
+ return React.useEffect(callback, useDeepCompareMemoize(dependencies))
57
+ }
58
+
59
+ export function useDeepCompareEffectNoCheck(
60
+ callback: EffectCallback,
61
+ dependencies: DependencyList
62
+ ): UseEffectReturn {
63
+ return React.useEffect(callback, useDeepCompareMemoize(dependencies))
64
+ }
65
+
66
+ export default useDeepCompareEffect
67
+
68
+ /**
69
+ The MIT License (MIT)
70
+ Copyright (c) 2020 Kent C. Dodds
71
+
72
+ Permission is hereby granted, free of charge, to any person obtaining a copy
73
+ of this software and associated documentation files (the "Software"), to deal
74
+ in the Software without restriction, including without limitation the rights
75
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
76
+ copies of the Software, and to permit persons to whom the Software is
77
+ furnished to do so, subject to the following conditions:
78
+
79
+ The above copyright notice and this permission notice shall be included in all
80
+ copies or substantial portions of the Software.
81
+
82
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
83
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
84
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
85
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
86
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
87
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
88
+ SOFTWARE.
89
+ */
@@ -0,0 +1,224 @@
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])
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
+ // only consider paginated queries
106
+ if (artifact.kind !== ArtifactKind.Query || !artifact.refetch?.paginated) {
107
+ return {
108
+ artifact,
109
+ data: storeValue.data,
110
+ variables: storeValue.variables,
111
+ fetch: fetchQuery,
112
+ partial: storeValue.partial,
113
+ }
114
+ }
115
+
116
+ // if the artifact supports cursor pagination, then add the cursor handlers
117
+ if (artifact.refetch.method === 'cursor') {
118
+ const handlers = cursorHandlers<_Data, _Input>({
119
+ artifact,
120
+ getState: () => storeValue.data,
121
+ getVariables: () => storeValue.variables!,
122
+ fetch: fetchQuery,
123
+ fetchUpdate: (args, updates) => {
124
+ return paginationObserver!.send({
125
+ ...args,
126
+ cacheParams: {
127
+ ...args?.cacheParams,
128
+ disableSubscriptions: true,
129
+ applyUpdates: updates,
130
+ },
131
+ session,
132
+ })
133
+ },
134
+ getSession: async () => session,
135
+ })
136
+
137
+ return {
138
+ artifact,
139
+ data: storeValue.data,
140
+ variables: storeValue.variables,
141
+ fetch: handlers.fetch,
142
+ partial: storeValue.partial,
143
+ loadNext: wrapLoad(setForwardPending, handlers.loadNextPage),
144
+ loadNextPending: forwardPending,
145
+ loadPrevious: wrapLoad(setBackwardPending, handlers.loadPreviousPage),
146
+ loadPreviousPending: backwardPending,
147
+ pageInfo: extractPageInfo(storeValue.data, artifact.refetch!.path),
148
+ }
149
+ }
150
+
151
+ if (artifact.refetch.method === 'offset') {
152
+ const handlers = offsetHandlers({
153
+ artifact,
154
+ getState: () => storeValue.data,
155
+ getVariables: () => storeValue.variables!,
156
+ storeName: artifact.name,
157
+ fetch: fetchQuery,
158
+ fetchUpdate: async (args, updates = ['append']) => {
159
+ return paginationObserver!.send({
160
+ ...args,
161
+ cacheParams: {
162
+ disableSubscriptions: true,
163
+ applyUpdates: updates,
164
+ ...args?.cacheParams,
165
+ },
166
+ })
167
+ },
168
+ getSession: async () => session,
169
+ })
170
+
171
+ return {
172
+ artifact,
173
+ data: storeValue.data,
174
+ variables: storeValue.variables,
175
+ fetch: handlers.fetch,
176
+ partial: storeValue.partial,
177
+ loadNext: wrapLoad(setForwardPending, handlers.loadNextPage),
178
+ loadNextPending: forwardPending,
179
+ }
180
+ }
181
+
182
+ // we don't want to add anything
183
+ return {
184
+ artifact,
185
+ data: storeValue.data,
186
+ variables: storeValue.variables,
187
+ fetch: fetchQuery,
188
+ refetch: fetchQuery,
189
+ partial: storeValue.partial,
190
+ }
191
+ }, [artifact, observer, session, storeValue])
192
+ }
193
+
194
+ export type DocumentHandle<
195
+ _Artifact extends QueryArtifact,
196
+ _Data extends GraphQLObject = GraphQLObject,
197
+ _Input extends GraphQLVariables = GraphQLVariables
198
+ > = {
199
+ data: _Data
200
+ partial: boolean
201
+ fetch: FetchFn<_Data, Partial<_Input>>
202
+ variables: _Input
203
+ } & RefetchHandlers<_Artifact, _Data, _Input>
204
+
205
+ type RefetchHandlers<_Artifact extends QueryArtifact, _Data extends GraphQLObject, _Input> =
206
+ // we need to add different methods if the artifact supports cursor pagination
207
+ _Artifact extends {
208
+ refetch: { paginated: true; method: 'cursor' }
209
+ }
210
+ ? {
211
+ loadNext: CursorHandlers<_Data, _Input>['loadNextPage']
212
+ loadNextPending: boolean
213
+ loadPrevious: CursorHandlers<_Data, _Input>['loadPreviousPage']
214
+ loadPreviousPending: boolean
215
+ pageInfo: PageInfo
216
+ }
217
+ : // offset pagination
218
+ _Artifact extends { refetch: { paginated: true; method: 'offset' } }
219
+ ? {
220
+ loadNext: OffsetHandlers<_Data, _Input>['loadNextPage']
221
+ loadNextPending: boolean
222
+ }
223
+ : // the artifact does not support a known pagination method, don't add anything
224
+ {}
@@ -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
+ let [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]
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,102 @@
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 { useDeepCompareMemoize } from './useDeepCompareEffect'
8
+ import { useDocumentSubscription } from './useDocumentSubscription'
9
+
10
+ export function useFragment<
11
+ _Data extends GraphQLObject,
12
+ _ReferenceType extends {},
13
+ _Input extends GraphQLVariables = GraphQLVariables
14
+ >(
15
+ reference: _Data | { [fragmentKey]: _ReferenceType } | null,
16
+ document: { artifact: FragmentArtifact }
17
+ ) {
18
+ const { cache } = useRouterContext()
19
+
20
+ // get the fragment reference info
21
+ const { parent, variables, loading } = fragmentReference<_Data, _Input, _ReferenceType>(
22
+ reference,
23
+ document
24
+ )
25
+
26
+ // if we got this far then we are safe to use the fields on the object
27
+ let cachedValue = reference as _Data | null
28
+
29
+ // on the client, we want to ensure that we apply masking to the initial value by
30
+ // loading the value from cache
31
+ if (reference && parent) {
32
+ cachedValue = cache.read({
33
+ selection: document.artifact.selection,
34
+ parent,
35
+ variables,
36
+ loading,
37
+ }).data as _Data
38
+ }
39
+
40
+ // we're ready to setup the live document
41
+ const [storeValue] = useDocumentSubscription<FragmentArtifact, _Data, _Input>({
42
+ artifact: document.artifact,
43
+ variables,
44
+ initialValue: cachedValue,
45
+ // dont subscribe to anything if we are loading
46
+ disabled: loading,
47
+ send: {
48
+ stuff: {
49
+ parentID: parent,
50
+ },
51
+ setup: true,
52
+ },
53
+ })
54
+
55
+ // the parent has changed, we need to use initialValue for this render
56
+ // if we don't, then there is a very brief flash where we will show the old data
57
+ // before the store has had a chance to update
58
+ const lastReference = React.useRef<{ parent: string; variables: _Input } | null>(null)
59
+ return React.useMemo(() => {
60
+ // if the parent reference has changed we need to always prefer the cached value
61
+ const parentChange =
62
+ storeValue.parent !== parent ||
63
+ !deepEquals({ parent, variables }, lastReference.current)
64
+ if (parentChange) {
65
+ // make sure we keep track of the last reference we used
66
+ lastReference.current = { parent, variables: { ...variables } }
67
+
68
+ // and use the cached value
69
+ return cachedValue
70
+ }
71
+
72
+ return storeValue.data
73
+ }, [
74
+ useDeepCompareMemoize({
75
+ parent,
76
+ variables,
77
+ cachedValue,
78
+ storeValue: storeValue.data,
79
+ storeParent: storeValue.parent,
80
+ }),
81
+ ])
82
+ }
83
+
84
+ export function fragmentReference<_Data extends GraphQLObject, _Input, _ReferenceType extends {}>(
85
+ reference: _Data | { [fragmentKey]: _ReferenceType } | null,
86
+ document: { artifact: FragmentArtifact }
87
+ ): { variables: _Input; parent: string; loading: boolean } {
88
+ // @ts-expect-error: typescript can't guarantee that the fragment key is defined
89
+ // but if its not, then the fragment wasn't mixed into the right thing
90
+ // the variables for the fragment live on the initial value's $fragment key
91
+ const { variables, parent } = reference?.[fragmentKey]?.values?.[document.artifact.name] ?? {}
92
+ if (reference && fragmentKey in reference && (!variables || !parent)) {
93
+ console.warn(
94
+ `⚠️ Parent does not contain the information for this fragment. Something is wrong.
95
+ Please ensure that you have passed a record that has ${document.artifact.name} mixed into it.`
96
+ )
97
+ }
98
+ // @ts-expect-error
99
+ const loading = Boolean(reference?.[fragmentKey]?.loading)
100
+
101
+ return { variables, parent, loading }
102
+ }
@@ -0,0 +1,47 @@
1
+ import type {
2
+ GraphQLObject,
3
+ FragmentArtifact,
4
+ QueryArtifact,
5
+ fragmentKey,
6
+ GraphQLVariables,
7
+ } from 'houdini/runtime'
8
+
9
+ import { useDocumentHandle, type DocumentHandle } from './useDocumentHandle'
10
+ import { useDocumentStore } from './useDocumentStore'
11
+ import { fragmentReference, useFragment } from './useFragment'
12
+
13
+ // useFragmentHandle is just like useFragment except it also returns an imperative handle
14
+ // that users can use to interact with the fragment
15
+ export function useFragmentHandle<
16
+ _Artifact extends FragmentArtifact,
17
+ _Data extends GraphQLObject,
18
+ _ReferenceType extends {},
19
+ _PaginationArtifact extends QueryArtifact,
20
+ _Input extends GraphQLVariables = GraphQLVariables
21
+ >(
22
+ reference: _Data | { [fragmentKey]: _ReferenceType } | null,
23
+ document: { artifact: FragmentArtifact; refetchArtifact?: QueryArtifact }
24
+ ): DocumentHandle<_PaginationArtifact, _Data, _Input> {
25
+ // get the fragment values
26
+ const data = useFragment<_Data, _ReferenceType, _Input>(reference, document)
27
+
28
+ // look at the fragment reference to get the variables
29
+ const { variables } = fragmentReference<_Data, _Input, _ReferenceType>(reference, document)
30
+
31
+ // use the pagination fragment for meta data if it exists.
32
+ // if we pass this a fragment artifact, it won't add any data
33
+ const [handleValue, handleObserver] = useDocumentStore<_Data, _Input>({
34
+ artifact: document.refetchArtifact ?? document.artifact,
35
+ })
36
+ const handle = useDocumentHandle<_PaginationArtifact, _Data, _Input>({
37
+ observer: handleObserver,
38
+ storeValue: handleValue,
39
+ artifact: document.refetchArtifact ?? document.artifact,
40
+ })
41
+
42
+ return {
43
+ ...handle,
44
+ variables,
45
+ data,
46
+ }
47
+ }