houdini-react 2.0.0-go.0 → 2.0.0-go.2
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.
- package/package.json +89 -73
- package/{build/houdini-react/shim.cjs → shim.cjs} +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -36
- package/build/houdini-react/package.json +0 -92
- package/build/houdini-react/postInstall.js +0 -117
- package/build/houdini-react/runtime/client.ts +0 -5
- package/build/houdini-react/runtime/clientPlugin.ts +0 -17
- package/build/houdini-react/runtime/componentFields.ts +0 -79
- package/build/houdini-react/runtime/hooks/index.ts +0 -9
- package/build/houdini-react/runtime/hooks/useDeepCompareEffect.ts +0 -89
- package/build/houdini-react/runtime/hooks/useDocumentHandle.ts +0 -224
- package/build/houdini-react/runtime/hooks/useDocumentStore.ts +0 -76
- package/build/houdini-react/runtime/hooks/useDocumentSubscription.ts +0 -62
- package/build/houdini-react/runtime/hooks/useFragment.ts +0 -102
- package/build/houdini-react/runtime/hooks/useFragmentHandle.ts +0 -47
- package/build/houdini-react/runtime/hooks/useIsMounted.ts +0 -14
- package/build/houdini-react/runtime/hooks/useMutation.ts +0 -54
- package/build/houdini-react/runtime/hooks/useQuery.ts +0 -17
- package/build/houdini-react/runtime/hooks/useQueryHandle.ts +0 -184
- package/build/houdini-react/runtime/hooks/useSubscription.ts +0 -12
- package/build/houdini-react/runtime/hooks/useSubscriptionHandle.ts +0 -33
- package/build/houdini-react/runtime/index.tsx +0 -49
- package/build/houdini-react/runtime/manifest.ts +0 -6
- package/build/houdini-react/runtime/package.json +0 -1
- package/build/houdini-react/runtime/routing/Router.tsx +0 -887
- package/build/houdini-react/runtime/routing/cache.ts +0 -52
- package/build/houdini-react/runtime/routing/index.ts +0 -2
- package/build/houdini-react/server/index.d.ts +0 -1
- package/build/houdini-react/server/index.js +0 -4
- package/build/houdini-react/server/package.json +0 -1
- package/build/houdini-react/vite/index.d.ts +0 -3
- package/build/houdini-react/vite/index.js +0 -11
- package/build/houdini-react/vite/package.json +0 -1
- package/build/houdini-react-darwin-arm64/bin/houdini-react +0 -0
- package/build/houdini-react-darwin-arm64/package.json +0 -11
- package/build/houdini-react-darwin-x64/bin/houdini-react +0 -0
- package/build/houdini-react-darwin-x64/package.json +0 -11
- package/build/houdini-react-linux-arm64/bin/houdini-react +0 -0
- package/build/houdini-react-linux-arm64/package.json +0 -11
- package/build/houdini-react-linux-x64/bin/houdini-react +0 -0
- package/build/houdini-react-linux-x64/package.json +0 -11
- package/build/houdini-react-windows-arm64/bin/houdini-react.exe +0 -0
- package/build/houdini-react-windows-arm64/package.json +0 -11
- package/build/houdini-react-windows-x64/bin/houdini-react.exe +0 -0
- package/build/houdini-react-windows-x64/package.json +0 -11
- package/build/package.json +0 -91
|
@@ -1,224 +0,0 @@
|
|
|
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
|
-
{}
|
|
@@ -1,76 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,62 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,102 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,47 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
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
|
-
}) => Promise<QueryResult<_Result, _Input>>
|
|
18
|
-
|
|
19
|
-
export function useMutation<
|
|
20
|
-
_Result extends GraphQLObject,
|
|
21
|
-
_Input extends GraphQLVariables,
|
|
22
|
-
_Optimistic extends GraphQLObject
|
|
23
|
-
>({
|
|
24
|
-
artifact,
|
|
25
|
-
}: {
|
|
26
|
-
artifact: MutationArtifact
|
|
27
|
-
}): [boolean, MutationHandler<_Result, _Input, _Optimistic>] {
|
|
28
|
-
// build the live document we'll use to send values
|
|
29
|
-
const [storeValue, observer] = useDocumentStore<_Result, _Input>({ artifact })
|
|
30
|
-
|
|
31
|
-
// grab the pending state from the document store
|
|
32
|
-
const pending = storeValue.fetching
|
|
33
|
-
|
|
34
|
-
// grab the current session value
|
|
35
|
-
const [session] = useSession()
|
|
36
|
-
|
|
37
|
-
// sending the mutation just means invoking the observer's send method
|
|
38
|
-
const mutate: MutationHandler<_Result, _Input, _Optimistic> = ({
|
|
39
|
-
metadata,
|
|
40
|
-
fetch,
|
|
41
|
-
variables,
|
|
42
|
-
...mutationConfig
|
|
43
|
-
}) =>
|
|
44
|
-
observer.send({
|
|
45
|
-
variables,
|
|
46
|
-
metadata,
|
|
47
|
-
session,
|
|
48
|
-
stuff: {
|
|
49
|
-
...mutationConfig,
|
|
50
|
-
},
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
return [pending, mutate]
|
|
54
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import type { GraphQLVariables, 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
|
-
_Input extends GraphQLVariables = GraphQLVariables
|
|
10
|
-
>(
|
|
11
|
-
document: { artifact: QueryArtifact },
|
|
12
|
-
variables: any = null,
|
|
13
|
-
config: UseQueryConfig = {}
|
|
14
|
-
): _Data {
|
|
15
|
-
const { data } = useQueryHandle<_Artifact, _Data, _Input>(document, variables, config)
|
|
16
|
-
return data
|
|
17
|
-
}
|