houdini-react 2.0.0-next.3 → 2.0.0-next.31
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/bin/houdini-react +173 -0
- package/package.json +63 -39
- package/postInstall.js +360 -0
- package/runtime/client.ts +5 -0
- package/runtime/clientPlugin.ts +17 -0
- package/runtime/componentFields.ts +79 -0
- package/runtime/hooks/index.ts +9 -0
- package/runtime/hooks/useDeepCompareEffect.ts +91 -0
- package/runtime/hooks/useDocumentHandle.ts +232 -0
- package/runtime/hooks/useDocumentStore.ts +76 -0
- package/runtime/hooks/useDocumentSubscription.ts +62 -0
- package/runtime/hooks/useFragment.ts +93 -0
- package/runtime/hooks/useFragmentHandle.ts +46 -0
- package/runtime/hooks/useIsMounted.ts +14 -0
- package/runtime/hooks/useMutation.ts +70 -0
- package/runtime/hooks/useQuery.ts +12 -0
- package/runtime/hooks/useQueryHandle.ts +186 -0
- package/runtime/hooks/useSubscription.ts +12 -0
- package/runtime/hooks/useSubscriptionHandle.ts +33 -0
- package/runtime/hydration.tsx +155 -0
- package/runtime/index.tsx +49 -0
- package/runtime/manifest.ts +6 -0
- package/runtime/package.json +1 -0
- package/runtime/routing/Router.tsx +885 -0
- package/runtime/routing/cache.ts +54 -0
- package/runtime/routing/index.ts +2 -0
- package/server/index.d.ts +1 -0
- package/server/index.js +4 -0
- package/server/react-streaming.d.js +0 -0
- package/vite/index.d.ts +3 -0
- package/vite/index.js +286 -0
- package/vite/transform.d.ts +11 -0
- package/vite/transform.js +90 -0
- package/README.md +0 -36
- package/build/plugin/codegen/entries/documentWrappers.d.ts +0 -6
- package/build/plugin/codegen/entries/fallbacks.d.ts +0 -5
- package/build/plugin/codegen/entries/index.d.ts +0 -16
- package/build/plugin/codegen/entries/pages.d.ts +0 -2
- package/build/plugin/codegen/index.d.ts +0 -17
- package/build/plugin/codegen/manifest.d.ts +0 -5
- package/build/plugin/codegen/render.d.ts +0 -7
- package/build/plugin/codegen/router.d.ts +0 -7
- package/build/plugin/codegen/typeRoot.d.ts +0 -5
- package/build/plugin/config.d.ts +0 -4
- package/build/plugin/dedent.d.ts +0 -1
- package/build/plugin/extract.d.ts +0 -6
- package/build/plugin/index.d.ts +0 -5
- package/build/plugin/state.d.ts +0 -3
- package/build/plugin/transform.d.ts +0 -6
- package/build/plugin/vite.d.ts +0 -27
- package/build/plugin-cjs/index.js +0 -90119
- package/build/plugin-cjs/package.json +0 -1
- package/build/plugin-esm/index.js +0 -90115
- package/build/runtime/client.d.ts +0 -3
- package/build/runtime/clientPlugin.d.ts +0 -3
- package/build/runtime/componentFields.d.ts +0 -9
- package/build/runtime/hooks/index.d.ts +0 -8
- package/build/runtime/hooks/useDeepCompareEffect.d.ts +0 -35
- package/build/runtime/hooks/useDocumentHandle.d.ts +0 -36
- package/build/runtime/hooks/useDocumentStore.d.ts +0 -11
- package/build/runtime/hooks/useDocumentSubscription.d.ts +0 -11
- package/build/runtime/hooks/useFragment.d.ts +0 -16
- package/build/runtime/hooks/useFragmentHandle.d.ts +0 -8
- package/build/runtime/hooks/useIsMounted.d.ts +0 -3
- package/build/runtime/hooks/useMutation.d.ts +0 -14
- package/build/runtime/hooks/useQuery.d.ts +0 -5
- package/build/runtime/hooks/useQueryHandle.d.ts +0 -10
- package/build/runtime/hooks/useSubscription.d.ts +0 -4
- package/build/runtime/hooks/useSubscriptionHandle.d.ts +0 -25
- package/build/runtime/index.d.ts +0 -14
- package/build/runtime/manifest.d.ts +0 -3
- package/build/runtime/routing/Router.d.ts +0 -62
- package/build/runtime/routing/cache.d.ts +0 -7
- package/build/runtime/routing/hooks.d.ts +0 -40
- package/build/runtime/routing/index.d.ts +0 -3
- package/build/runtime-cjs/client.d.ts +0 -3
- package/build/runtime-cjs/client.js +0 -25
- package/build/runtime-cjs/clientPlugin.d.ts +0 -3
- package/build/runtime-cjs/clientPlugin.js +0 -37
- package/build/runtime-cjs/componentFields.d.ts +0 -9
- package/build/runtime-cjs/componentFields.js +0 -83
- package/build/runtime-cjs/hooks/index.d.ts +0 -8
- package/build/runtime-cjs/hooks/index.js +0 -45
- package/build/runtime-cjs/hooks/useDeepCompareEffect.d.ts +0 -35
- package/build/runtime-cjs/hooks/useDeepCompareEffect.js +0 -76
- package/build/runtime-cjs/hooks/useDocumentHandle.d.ts +0 -36
- package/build/runtime-cjs/hooks/useDocumentHandle.js +0 -177
- package/build/runtime-cjs/hooks/useDocumentStore.d.ts +0 -11
- package/build/runtime-cjs/hooks/useDocumentStore.js +0 -76
- package/build/runtime-cjs/hooks/useDocumentSubscription.d.ts +0 -11
- package/build/runtime-cjs/hooks/useDocumentSubscription.js +0 -76
- package/build/runtime-cjs/hooks/useFragment.d.ts +0 -16
- package/build/runtime-cjs/hooks/useFragment.js +0 -102
- package/build/runtime-cjs/hooks/useFragmentHandle.d.ts +0 -8
- package/build/runtime-cjs/hooks/useFragmentHandle.js +0 -47
- package/build/runtime-cjs/hooks/useIsMounted.d.ts +0 -3
- package/build/runtime-cjs/hooks/useIsMounted.js +0 -38
- package/build/runtime-cjs/hooks/useMutation.d.ts +0 -14
- package/build/runtime-cjs/hooks/useMutation.js +0 -67
- package/build/runtime-cjs/hooks/useQuery.d.ts +0 -5
- package/build/runtime-cjs/hooks/useQuery.js +0 -32
- package/build/runtime-cjs/hooks/useQueryHandle.d.ts +0 -10
- package/build/runtime-cjs/hooks/useQueryHandle.js +0 -131
- package/build/runtime-cjs/hooks/useSubscription.d.ts +0 -4
- package/build/runtime-cjs/hooks/useSubscription.js +0 -32
- package/build/runtime-cjs/hooks/useSubscriptionHandle.d.ts +0 -25
- package/build/runtime-cjs/hooks/useSubscriptionHandle.js +0 -42
- package/build/runtime-cjs/index.d.ts +0 -14
- package/build/runtime-cjs/index.js +0 -88
- package/build/runtime-cjs/manifest.d.ts +0 -3
- package/build/runtime-cjs/manifest.js +0 -25
- package/build/runtime-cjs/package.json +0 -1
- package/build/runtime-cjs/routing/Router.d.ts +0 -62
- package/build/runtime-cjs/routing/Router.js +0 -540
- package/build/runtime-cjs/routing/cache.d.ts +0 -7
- package/build/runtime-cjs/routing/cache.js +0 -61
- package/build/runtime-cjs/routing/hooks.d.ts +0 -40
- package/build/runtime-cjs/routing/hooks.js +0 -93
- package/build/runtime-cjs/routing/index.d.ts +0 -3
- package/build/runtime-cjs/routing/index.js +0 -33
- package/build/runtime-esm/client.d.ts +0 -3
- package/build/runtime-esm/client.js +0 -5
- package/build/runtime-esm/clientPlugin.d.ts +0 -3
- package/build/runtime-esm/clientPlugin.js +0 -17
- package/build/runtime-esm/componentFields.d.ts +0 -9
- package/build/runtime-esm/componentFields.js +0 -59
- package/build/runtime-esm/hooks/index.d.ts +0 -8
- package/build/runtime-esm/hooks/index.js +0 -15
- package/build/runtime-esm/hooks/useDeepCompareEffect.d.ts +0 -35
- package/build/runtime-esm/hooks/useDeepCompareEffect.js +0 -41
- package/build/runtime-esm/hooks/useDocumentHandle.d.ts +0 -36
- package/build/runtime-esm/hooks/useDocumentHandle.js +0 -143
- package/build/runtime-esm/hooks/useDocumentStore.d.ts +0 -11
- package/build/runtime-esm/hooks/useDocumentStore.js +0 -42
- package/build/runtime-esm/hooks/useDocumentSubscription.d.ts +0 -11
- package/build/runtime-esm/hooks/useDocumentSubscription.js +0 -42
- package/build/runtime-esm/hooks/useFragment.d.ts +0 -16
- package/build/runtime-esm/hooks/useFragment.js +0 -67
- package/build/runtime-esm/hooks/useFragmentHandle.d.ts +0 -8
- package/build/runtime-esm/hooks/useFragmentHandle.js +0 -23
- package/build/runtime-esm/hooks/useIsMounted.d.ts +0 -3
- package/build/runtime-esm/hooks/useIsMounted.js +0 -14
- package/build/runtime-esm/hooks/useMutation.d.ts +0 -14
- package/build/runtime-esm/hooks/useMutation.js +0 -42
- package/build/runtime-esm/hooks/useQuery.d.ts +0 -5
- package/build/runtime-esm/hooks/useQuery.js +0 -8
- package/build/runtime-esm/hooks/useQueryHandle.d.ts +0 -10
- package/build/runtime-esm/hooks/useQueryHandle.js +0 -97
- package/build/runtime-esm/hooks/useSubscription.d.ts +0 -4
- package/build/runtime-esm/hooks/useSubscription.js +0 -8
- package/build/runtime-esm/hooks/useSubscriptionHandle.d.ts +0 -25
- package/build/runtime-esm/hooks/useSubscriptionHandle.js +0 -18
- package/build/runtime-esm/index.d.ts +0 -14
- package/build/runtime-esm/index.js +0 -48
- package/build/runtime-esm/manifest.d.ts +0 -3
- package/build/runtime-esm/manifest.js +0 -5
- package/build/runtime-esm/routing/Router.d.ts +0 -62
- package/build/runtime-esm/routing/Router.js +0 -499
- package/build/runtime-esm/routing/cache.d.ts +0 -7
- package/build/runtime-esm/routing/cache.js +0 -36
- package/build/runtime-esm/routing/hooks.d.ts +0 -40
- package/build/runtime-esm/routing/hooks.js +0 -53
- package/build/runtime-esm/routing/index.d.ts +0 -3
- package/build/runtime-esm/routing/index.js +0 -6
- package/build/server/index.d.ts +0 -1
- package/build/server-cjs/index.js +0 -28
- package/build/server-cjs/package.json +0 -1
- package/build/server-esm/index.js +0 -4
- package/build/server-esm/package.json +0 -1
- /package/{build/plugin-esm → server}/package.json +0 -0
- /package/{build/runtime-esm → vite}/package.json +0 -0
|
@@ -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.js'
|
|
9
|
+
import { useDocumentStore } from './useDocumentStore.js'
|
|
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.js'
|
|
4
|
+
import { useQueryHandle } from './useQueryHandle.js'
|
|
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
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { createLRUCache } from 'houdini/runtime'
|
|
2
|
+
import type { GraphQLObject, CachePolicies, QueryArtifact, GraphQLVariables } from 'houdini/runtime'
|
|
3
|
+
import React from 'react'
|
|
4
|
+
|
|
5
|
+
import { useClient } from '../routing/index.js'
|
|
6
|
+
import type { DocumentHandle } from './useDocumentHandle.js'
|
|
7
|
+
import { useDocumentHandle } from './useDocumentHandle.js'
|
|
8
|
+
import { useIsMountedRef } from './useIsMounted.js'
|
|
9
|
+
|
|
10
|
+
// Suspense requires a way to throw a promise that resolves to a place
|
|
11
|
+
// we can put when we go back on a susequent render. This means that we have to have
|
|
12
|
+
// a stable way to identify _this_ useQueryHandle called.
|
|
13
|
+
// For now, we're going to compute an identifier based on the name of the artifact
|
|
14
|
+
// and the variables that we were given.
|
|
15
|
+
//
|
|
16
|
+
// - If we have a cached promise that's pending, we should just throw that promise
|
|
17
|
+
// - If we have a cached promise that's been resolved, we should return that value
|
|
18
|
+
//
|
|
19
|
+
// When the Component unmounts, we need to remove the entry from the cache (so we can load again)
|
|
20
|
+
|
|
21
|
+
const promiseCache = createLRUCache<QuerySuspenseUnit>()
|
|
22
|
+
type QuerySuspenseUnit<
|
|
23
|
+
_Data extends GraphQLObject = GraphQLObject,
|
|
24
|
+
_Input extends GraphQLVariables = GraphQLVariables,
|
|
25
|
+
> = {
|
|
26
|
+
resolve: () => void
|
|
27
|
+
resolved?: DocumentHandle<QueryArtifact, _Data, _Input>
|
|
28
|
+
then: (val: any) => any
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function useQueryHandle<
|
|
32
|
+
_Artifact extends QueryArtifact,
|
|
33
|
+
_Data extends GraphQLObject = GraphQLObject,
|
|
34
|
+
_Input extends GraphQLVariables = GraphQLVariables,
|
|
35
|
+
>(
|
|
36
|
+
{ artifact }: { artifact: QueryArtifact },
|
|
37
|
+
variables: any = null,
|
|
38
|
+
config: UseQueryConfig = {}
|
|
39
|
+
): any {
|
|
40
|
+
// figure out the identifier so we know what to look for
|
|
41
|
+
const identifier = queryIdentifier({ artifact, variables, config })
|
|
42
|
+
|
|
43
|
+
// see if we have an entry in the cache for the identifier
|
|
44
|
+
const suspenseValue = promiseCache.get(identifier)
|
|
45
|
+
|
|
46
|
+
const client = useClient()
|
|
47
|
+
|
|
48
|
+
const isMountedRef = useIsMountedRef()
|
|
49
|
+
|
|
50
|
+
// hold onto an observer we'll use
|
|
51
|
+
const [observer] = React.useState(
|
|
52
|
+
client.observe<_Data, _Input>({
|
|
53
|
+
artifact,
|
|
54
|
+
initialValue: (suspenseValue?.resolved?.data ?? {}) as _Data,
|
|
55
|
+
})
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
// a ref flag we'll enable before throwing so that we don't update while suspend
|
|
59
|
+
const suspenseTracker = React.useRef(false)
|
|
60
|
+
|
|
61
|
+
// a stable box to put the store's value
|
|
62
|
+
const box = React.useRef(observer.state)
|
|
63
|
+
|
|
64
|
+
// a stable subscribe function for the document store
|
|
65
|
+
const subscribe: any = React.useCallback(
|
|
66
|
+
(fn: () => void) => {
|
|
67
|
+
return observer.subscribe((val) => {
|
|
68
|
+
box.current = val
|
|
69
|
+
if (isMountedRef.current && !suspenseTracker.current) {
|
|
70
|
+
fn()
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
},
|
|
74
|
+
[observer, isMountedRef.current]
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
// get a safe reference to the cache
|
|
78
|
+
const storeValue = React.useSyncExternalStore(subscribe, () => box.current)
|
|
79
|
+
|
|
80
|
+
// compute the imperative handle for this artifact
|
|
81
|
+
const handle = useDocumentHandle<_Artifact, _Data, _Input>({
|
|
82
|
+
artifact,
|
|
83
|
+
observer,
|
|
84
|
+
storeValue,
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
// if the identifier changes, we need to remove the old identifier from the
|
|
88
|
+
// suspense cache
|
|
89
|
+
React.useEffect(() => {
|
|
90
|
+
return () => {
|
|
91
|
+
promiseCache.delete(identifier)
|
|
92
|
+
}
|
|
93
|
+
}, [identifier])
|
|
94
|
+
|
|
95
|
+
// when we unmount, we need to clean up
|
|
96
|
+
React.useEffect(() => {
|
|
97
|
+
return () => {
|
|
98
|
+
observer.cleanup()
|
|
99
|
+
}
|
|
100
|
+
}, [observer])
|
|
101
|
+
|
|
102
|
+
// if the promise has resolved, let's use that for our first render
|
|
103
|
+
const result = storeValue.data
|
|
104
|
+
|
|
105
|
+
if (!suspenseValue) {
|
|
106
|
+
// we are going to cache the promise and then throw it
|
|
107
|
+
// when it resolves the cached value will be updated
|
|
108
|
+
// and it will be picked up in the next render
|
|
109
|
+
let resolve: () => void = () => {}
|
|
110
|
+
const loadPromise = new Promise<void>((r) => (resolve = r))
|
|
111
|
+
|
|
112
|
+
const suspenseUnit: QuerySuspenseUnit<_Data, _Input> = {
|
|
113
|
+
// biome-ignore lint/suspicious/noThenProperty: suspense protocol requires a thenable
|
|
114
|
+
then: loadPromise.then.bind(loadPromise),
|
|
115
|
+
resolve,
|
|
116
|
+
// @ts-expect-error
|
|
117
|
+
variables,
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
promiseCache.set(identifier, suspenseUnit as QuerySuspenseUnit)
|
|
121
|
+
|
|
122
|
+
// the suspense unit gives react something to hold onto
|
|
123
|
+
// and it acts as a place for us to register a callback on
|
|
124
|
+
// send to update the cache before resolving the suspense
|
|
125
|
+
handle
|
|
126
|
+
.fetch({
|
|
127
|
+
variables,
|
|
128
|
+
// @ts-expect-error: this is actually allowed... 🤫
|
|
129
|
+
stuff: {
|
|
130
|
+
silenceLoading: true,
|
|
131
|
+
},
|
|
132
|
+
})
|
|
133
|
+
.then((value) => {
|
|
134
|
+
// the final value
|
|
135
|
+
suspenseUnit.resolved = {
|
|
136
|
+
...handle,
|
|
137
|
+
data: value.data,
|
|
138
|
+
partia: value.partial,
|
|
139
|
+
artifact,
|
|
140
|
+
} as unknown as DocumentHandle<QueryArtifact, _Data, _Input>
|
|
141
|
+
|
|
142
|
+
suspenseUnit.resolve()
|
|
143
|
+
})
|
|
144
|
+
suspenseTracker.current = true
|
|
145
|
+
throw suspenseUnit
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// if the promise is still pending, we're still waiting
|
|
149
|
+
if (!result && suspenseValue && !suspenseValue.resolved) {
|
|
150
|
+
suspenseTracker.current = true
|
|
151
|
+
throw suspenseValue
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// make sure we prefer the latest store value instead of the initial version we loaded on mount
|
|
155
|
+
if (!result && suspenseValue?.resolved) {
|
|
156
|
+
return suspenseValue.resolved as unknown as DocumentHandle<_Artifact, _Data, _Input>
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
...handle,
|
|
161
|
+
variables: storeValue.variables,
|
|
162
|
+
data: result,
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export type UseQueryConfig = {
|
|
167
|
+
policy?: CachePolicies
|
|
168
|
+
metadata?: App.Metadata
|
|
169
|
+
fetchKey?: any
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function queryIdentifier(args: {
|
|
173
|
+
artifact: QueryArtifact
|
|
174
|
+
fetchKey?: number
|
|
175
|
+
variables: {}
|
|
176
|
+
config: UseQueryConfig
|
|
177
|
+
}): string {
|
|
178
|
+
// make sure there is always a fetchKey
|
|
179
|
+
args.fetchKey ??= 0
|
|
180
|
+
|
|
181
|
+
// pull the common stuff out
|
|
182
|
+
const { artifact, variables, fetchKey } = args
|
|
183
|
+
|
|
184
|
+
// a query identifier is a mix of its name, arguments, and the fetch key
|
|
185
|
+
return [artifact.name, JSON.stringify(variables), fetchKey].join('@@')
|
|
186
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { SubscriptionArtifact, GraphQLObject, GraphQLVariables } from 'houdini/runtime'
|
|
2
|
+
|
|
3
|
+
import { useSubscriptionHandle } from './useSubscriptionHandle.js'
|
|
4
|
+
|
|
5
|
+
// a hook to subscribe to a subscription artifact
|
|
6
|
+
export function useSubscription<_Result extends GraphQLObject, _Input extends GraphQLVariables>(
|
|
7
|
+
document: { artifact: SubscriptionArtifact },
|
|
8
|
+
variables: _Input
|
|
9
|
+
) {
|
|
10
|
+
const { data } = useSubscriptionHandle(document, variables)
|
|
11
|
+
return data
|
|
12
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { SubscriptionArtifact, GraphQLObject, GraphQLVariables } from 'houdini/runtime'
|
|
2
|
+
|
|
3
|
+
import { useDocumentSubscription } from './useDocumentSubscription.js'
|
|
4
|
+
|
|
5
|
+
export type SubscriptionHandle<_Result extends GraphQLObject, _Input extends GraphQLVariables> = {
|
|
6
|
+
data: _Result | null
|
|
7
|
+
errors: { message: string }[] | null
|
|
8
|
+
variables: _Input
|
|
9
|
+
listen: (args: { variables?: _Input }) => void
|
|
10
|
+
unlisten: () => void
|
|
11
|
+
fetching: boolean
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// a hook to subscribe to a subscription artifact
|
|
15
|
+
export function useSubscriptionHandle<
|
|
16
|
+
_Result extends GraphQLObject,
|
|
17
|
+
_Input extends GraphQLVariables,
|
|
18
|
+
>({ artifact }: { artifact: SubscriptionArtifact }, variables: _Input) {
|
|
19
|
+
// a subscription is basically just a live document
|
|
20
|
+
const [storeValue, observer] = useDocumentSubscription({
|
|
21
|
+
artifact,
|
|
22
|
+
variables,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
data: storeValue.data,
|
|
27
|
+
errors: storeValue.errors,
|
|
28
|
+
fetching: storeValue.fetching,
|
|
29
|
+
variables,
|
|
30
|
+
unlisten: observer.cleanup,
|
|
31
|
+
listen: observer.send,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { hydrateRoot, createRoot } from 'react-dom/client'
|
|
3
|
+
import type { QueryArtifact, GraphQLVariables } from '$houdini/runtime'
|
|
4
|
+
import type { Cache } from '$houdini/runtime/cache'
|
|
5
|
+
import type { HoudiniClient } from '$houdini/runtime/client'
|
|
6
|
+
import cacheRef from '$houdini/runtime/cache'
|
|
7
|
+
|
|
8
|
+
import { injectComponents } from './componentFields.js'
|
|
9
|
+
import { router_cache, type RouterCache } from './routing/index.js'
|
|
10
|
+
import clientFactory from './client.js'
|
|
11
|
+
|
|
12
|
+
declare global {
|
|
13
|
+
interface Window {
|
|
14
|
+
__houdini__client__?: HoudiniClient
|
|
15
|
+
__houdini__pending_components__?: Record<string, any>
|
|
16
|
+
__houdini__cache__?: Cache
|
|
17
|
+
__houdini__hydration__layer__?: any
|
|
18
|
+
__houdini__initial__cache__?: any
|
|
19
|
+
__houdini__initial__session__?: any
|
|
20
|
+
__houdini__pending_artifacts__?: Record<string, QueryArtifact>
|
|
21
|
+
__houdini__pending_data__?: Record<string, any>
|
|
22
|
+
__houdini__pending_variables__?: Record<string, GraphQLVariables>
|
|
23
|
+
__houdini__nav_caches__?: RouterCache
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function hydrate_page(
|
|
28
|
+
App: React.ComponentType<any>,
|
|
29
|
+
Component: React.ComponentType<any>,
|
|
30
|
+
pageName: string,
|
|
31
|
+
pendingQueries: string[]
|
|
32
|
+
) {
|
|
33
|
+
// set up the client using its internally-managed singleton cache (cacheRef).
|
|
34
|
+
// the client's cachePolicy and queryPlugin are closure-bound to cacheRef at
|
|
35
|
+
// construction time, so we must use that same instance as window.__houdini__cache__
|
|
36
|
+
// rather than creating a second cache and trying to wire them together afterward.
|
|
37
|
+
window.__houdini__client__ ??= clientFactory()
|
|
38
|
+
window.__houdini__cache__ ??= cacheRef
|
|
39
|
+
|
|
40
|
+
// configure the singleton for React component field support
|
|
41
|
+
if (window.__houdini__pending_components__) {
|
|
42
|
+
window.__houdini__client__.componentCache = window.__houdini__pending_components__
|
|
43
|
+
}
|
|
44
|
+
window.__houdini__cache__._internal_unstable.componentCache =
|
|
45
|
+
window.__houdini__client__.componentCache
|
|
46
|
+
window.__houdini__cache__._internal_unstable.createComponent = (
|
|
47
|
+
fn: React.ComponentType<any>,
|
|
48
|
+
props: any
|
|
49
|
+
) => React.createElement(fn, props)
|
|
50
|
+
|
|
51
|
+
window.__houdini__hydration__layer__ ??=
|
|
52
|
+
window.__houdini__cache__._internal_unstable.storage.createLayer()
|
|
53
|
+
|
|
54
|
+
// rehydrate the cache from the server-serialized snapshot
|
|
55
|
+
window.__houdini__cache__?.hydrate(
|
|
56
|
+
window.__houdini__initial__cache__,
|
|
57
|
+
window.__houdini__hydration__layer__
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
// prime the data/artifact caches from anything the server streamed
|
|
61
|
+
const initialData: Record<string, any> = {}
|
|
62
|
+
const initialArtifacts: Record<string, QueryArtifact> = {}
|
|
63
|
+
|
|
64
|
+
for (const [artifactName, artifact] of Object.entries(
|
|
65
|
+
window.__houdini__pending_artifacts__ ?? {}
|
|
66
|
+
)) {
|
|
67
|
+
initialArtifacts[artifactName] = artifact
|
|
68
|
+
|
|
69
|
+
if (window.__houdini__pending_data__?.[artifactName]) {
|
|
70
|
+
const variables = window.__houdini__pending_variables__![artifactName]
|
|
71
|
+
|
|
72
|
+
if ((artifact as any).hasComponents) {
|
|
73
|
+
injectComponents({
|
|
74
|
+
cache: window.__houdini__cache__!,
|
|
75
|
+
selection: (artifact as any).selection,
|
|
76
|
+
data: window.__houdini__pending_data__[artifactName],
|
|
77
|
+
variables,
|
|
78
|
+
})
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const observer = window.__houdini__client__!.observe({
|
|
82
|
+
artifact,
|
|
83
|
+
cache: window.__houdini__cache__,
|
|
84
|
+
initialValue: window.__houdini__cache__!.read({
|
|
85
|
+
selection: (artifact as any).selection,
|
|
86
|
+
variables,
|
|
87
|
+
}).data,
|
|
88
|
+
initialVariables: variables,
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
observer.send({
|
|
92
|
+
setup: true,
|
|
93
|
+
variables,
|
|
94
|
+
session: window.__houdini__initial__session__,
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
initialData[artifactName] = observer
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!window.__houdini__nav_caches__) {
|
|
102
|
+
window.__houdini__nav_caches__ = router_cache({
|
|
103
|
+
pending_queries: pendingQueries,
|
|
104
|
+
initialData,
|
|
105
|
+
initialVariables: window.__houdini__pending_variables__,
|
|
106
|
+
initialArtifacts,
|
|
107
|
+
components: { [pageName]: Component },
|
|
108
|
+
})
|
|
109
|
+
_flush_pending_artifacts()
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
hydrateRoot(
|
|
113
|
+
document,
|
|
114
|
+
<App
|
|
115
|
+
initialURL={window.location.pathname}
|
|
116
|
+
cache={window.__houdini__cache__}
|
|
117
|
+
session={window.__houdini__initial__session__}
|
|
118
|
+
{...window.__houdini__nav_caches__}
|
|
119
|
+
/>
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const _pendingArtifacts: Array<[string, QueryArtifact]> = []
|
|
124
|
+
|
|
125
|
+
export function register_artifact(name: string, artifact: QueryArtifact) {
|
|
126
|
+
const caches = window.__houdini__nav_caches__
|
|
127
|
+
if (!caches?.artifact_cache) {
|
|
128
|
+
_pendingArtifacts.push([name, artifact])
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
if (!caches.artifact_cache.has(name)) {
|
|
132
|
+
caches.artifact_cache.set(name, artifact)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function _flush_pending_artifacts() {
|
|
137
|
+
while (_pendingArtifacts.length > 0) {
|
|
138
|
+
const [name, artifact] = _pendingArtifacts.shift()!
|
|
139
|
+
register_artifact(name, artifact)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export function mount_static_app(App: React.ComponentType<any>, manifest: any) {
|
|
144
|
+
const root = createRoot(document.getElementById('app')!)
|
|
145
|
+
|
|
146
|
+
root.render(
|
|
147
|
+
React.createElement(App, {
|
|
148
|
+
initialURL: window.location.pathname,
|
|
149
|
+
cache: cacheRef,
|
|
150
|
+
session: null,
|
|
151
|
+
manifest,
|
|
152
|
+
...router_cache(),
|
|
153
|
+
})
|
|
154
|
+
)
|
|
155
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { GraphQLObject } from 'houdini/runtime'
|
|
2
|
+
import type { Cache } from 'houdini/runtime/cache'
|
|
3
|
+
|
|
4
|
+
import client from './client.js'
|
|
5
|
+
import manifest from './manifest.js'
|
|
6
|
+
import { Router as RouterImpl, type RouterCache, RouterContextProvider } from './routing/index.js'
|
|
7
|
+
|
|
8
|
+
export * from './hooks/index.js'
|
|
9
|
+
export { router_cache, useSession, useLocation, useRoute } from './routing/index.js'
|
|
10
|
+
|
|
11
|
+
export function Router({
|
|
12
|
+
cache,
|
|
13
|
+
initialURL,
|
|
14
|
+
artifact_cache,
|
|
15
|
+
component_cache,
|
|
16
|
+
data_cache,
|
|
17
|
+
ssr_signals,
|
|
18
|
+
last_variables,
|
|
19
|
+
session,
|
|
20
|
+
assetPrefix,
|
|
21
|
+
injectToStream,
|
|
22
|
+
}: {
|
|
23
|
+
initialURL: string
|
|
24
|
+
initialVariables: GraphQLObject
|
|
25
|
+
cache: Cache
|
|
26
|
+
session?: App.Session
|
|
27
|
+
assetPrefix: string
|
|
28
|
+
injectToStream?: (chunk: string) => void
|
|
29
|
+
} & RouterCache) {
|
|
30
|
+
return (
|
|
31
|
+
<RouterContextProvider
|
|
32
|
+
client={client()}
|
|
33
|
+
cache={cache}
|
|
34
|
+
artifact_cache={artifact_cache}
|
|
35
|
+
component_cache={component_cache}
|
|
36
|
+
data_cache={data_cache}
|
|
37
|
+
ssr_signals={ssr_signals}
|
|
38
|
+
last_variables={last_variables}
|
|
39
|
+
session={session}
|
|
40
|
+
>
|
|
41
|
+
<RouterImpl
|
|
42
|
+
initialURL={initialURL}
|
|
43
|
+
manifest={manifest}
|
|
44
|
+
assetPrefix={assetPrefix}
|
|
45
|
+
injectToStream={injectToStream}
|
|
46
|
+
/>
|
|
47
|
+
</RouterContextProvider>
|
|
48
|
+
)
|
|
49
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module","main":"./index.tsx"}
|