houdini-react 2.0.0-next.4 → 2.0.0-next.40
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/Link.tsx +81 -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/recycleNodesInto.ts +60 -0
- package/runtime/hooks/useDeepCompareEffect.ts +93 -0
- package/runtime/hooks/useDocumentHandle.ts +238 -0
- package/runtime/hooks/useDocumentStore.ts +112 -0
- package/runtime/hooks/useDocumentSubscription.ts +65 -0
- package/runtime/hooks/useFragment.ts +101 -0
- package/runtime/hooks/useFragmentHandle.ts +191 -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 +194 -0
- package/runtime/hooks/useSubscription.ts +12 -0
- package/runtime/hooks/useSubscriptionHandle.ts +38 -0
- package/runtime/hydration.tsx +155 -0
- package/runtime/index.tsx +66 -0
- package/runtime/manifest.ts +6 -0
- package/runtime/package.json +1 -0
- package/runtime/resolve-href.ts +14 -0
- package/runtime/routing/Router.tsx +959 -0
- package/runtime/routing/cache.ts +57 -0
- package/runtime/routing/errors.tsx +145 -0
- package/runtime/routing/index.ts +17 -0
- package/runtime/tsconfig.json +39 -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 +384 -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
package/runtime/Link.tsx
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// this file is generated by houdini — do not edit
|
|
2
|
+
// @refresh reset
|
|
3
|
+
import type { AnchorHTMLAttributes, DetailedHTMLProps } from 'react'
|
|
4
|
+
import React from 'react'
|
|
5
|
+
|
|
6
|
+
// @ts-ignore
|
|
7
|
+
import type rawManifest from './manifest.js'
|
|
8
|
+
// @ts-ignore
|
|
9
|
+
import type { RouteScalars } from './manifest.js'
|
|
10
|
+
|
|
11
|
+
import { resolveHref } from './resolve-href.js'
|
|
12
|
+
|
|
13
|
+
type _Pages = (typeof rawManifest)['pages']
|
|
14
|
+
type _TSType<T extends string> = T extends keyof RouteScalars
|
|
15
|
+
? RouteScalars[T]
|
|
16
|
+
: T extends 'Int' | 'Float'
|
|
17
|
+
? number
|
|
18
|
+
: T extends 'ID'
|
|
19
|
+
? string | number
|
|
20
|
+
: T extends 'Boolean'
|
|
21
|
+
? boolean
|
|
22
|
+
: string
|
|
23
|
+
type _Param = { readonly name: string; readonly type: string; readonly optional: boolean }
|
|
24
|
+
type _ParamObj<Ps extends readonly _Param[]> = {
|
|
25
|
+
[P in Ps[number] as P['optional'] extends true ? P['name'] : never]?: _TSType<P['type']>
|
|
26
|
+
} & {
|
|
27
|
+
[P in Ps[number] as P['optional'] extends true ? never : P['name']]: _TSType<P['type']>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type _ExternalHref =
|
|
31
|
+
| `http://${string}`
|
|
32
|
+
| `https://${string}`
|
|
33
|
+
| `mailto:${string}`
|
|
34
|
+
| `tel:${string}`
|
|
35
|
+
| `blob:${string}`
|
|
36
|
+
| `data:${string}`
|
|
37
|
+
| `//${string}`
|
|
38
|
+
| `#${string}`
|
|
39
|
+
| `./${string}`
|
|
40
|
+
| `../${string}`
|
|
41
|
+
|
|
42
|
+
// All known app route URL strings — useful as a constraint for custom link wrappers.
|
|
43
|
+
export type RouteHrefs = _Pages[keyof _Pages] extends { readonly url: infer U extends string }
|
|
44
|
+
? U
|
|
45
|
+
: never
|
|
46
|
+
|
|
47
|
+
// Separate 'to' from 'params' so TypeScript evaluates them independently:
|
|
48
|
+
// - 'to' completions show all routes (including parameterized) because 'to' itself is always valid
|
|
49
|
+
// - 'params' is a separate intersection that errors when required but absent
|
|
50
|
+
type _PageForRoute<H extends string> = Extract<_Pages[keyof _Pages], { readonly url: H }>
|
|
51
|
+
type _ParamsForRoute<H extends string> = [_PageForRoute<H>] extends [never]
|
|
52
|
+
? { params?: never }
|
|
53
|
+
: _PageForRoute<H> extends { readonly params: readonly [] }
|
|
54
|
+
? { params?: never }
|
|
55
|
+
: _PageForRoute<H> extends { readonly params: infer Ps extends readonly _Param[] }
|
|
56
|
+
? { params: _ParamObj<Ps> }
|
|
57
|
+
: { params?: never }
|
|
58
|
+
|
|
59
|
+
export type LinkProps<H extends RouteHrefs | _ExternalHref = RouteHrefs | _ExternalHref> = Omit<
|
|
60
|
+
DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>,
|
|
61
|
+
'href'
|
|
62
|
+
> & {
|
|
63
|
+
to: H
|
|
64
|
+
disabled?: boolean
|
|
65
|
+
preload?: boolean | 'data' | 'component' | 'page'
|
|
66
|
+
} & _ParamsForRoute<H>
|
|
67
|
+
|
|
68
|
+
export function Link<H extends RouteHrefs | _ExternalHref>({
|
|
69
|
+
to,
|
|
70
|
+
params,
|
|
71
|
+
disabled,
|
|
72
|
+
preload,
|
|
73
|
+
...rest
|
|
74
|
+
}: LinkProps<H>): React.ReactElement {
|
|
75
|
+
const href = disabled
|
|
76
|
+
? undefined
|
|
77
|
+
: params != null
|
|
78
|
+
? resolveHref(to as string, params as Record<string, string | number | boolean>)
|
|
79
|
+
: (to as string)
|
|
80
|
+
return React.createElement('a', { ...rest, href, 'data-houdini-preload': preload })
|
|
81
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ClientPlugin } from 'houdini/runtime/client'
|
|
2
|
+
|
|
3
|
+
const plugin: () => ClientPlugin = () => () => {
|
|
4
|
+
return {
|
|
5
|
+
start(ctx, { next }) {
|
|
6
|
+
next({
|
|
7
|
+
...ctx,
|
|
8
|
+
cacheParams: {
|
|
9
|
+
...ctx.cacheParams,
|
|
10
|
+
serverSideFallback: false,
|
|
11
|
+
},
|
|
12
|
+
})
|
|
13
|
+
},
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export default plugin
|
|
@@ -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.js'
|
|
2
|
+
export { useQueryHandle } from './useQueryHandle.js'
|
|
3
|
+
export { useFragment } from './useFragment.js'
|
|
4
|
+
export { useFragmentHandle } from './useFragmentHandle.js'
|
|
5
|
+
export { useMutation } from './useMutation.js'
|
|
6
|
+
export { useSubscription } from './useSubscription.js'
|
|
7
|
+
|
|
8
|
+
export type { DocumentHandle } from './useDocumentHandle.js'
|
|
9
|
+
export type { UseQueryConfig } from './useQueryHandle.js'
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Walk `next` and, wherever a sub-tree is deeply equal to the corresponding
|
|
3
|
+
* sub-tree in `prev`, substitute `prev`'s reference. This preserves object
|
|
4
|
+
* identity for unchanged branches so React.memo can bail out on re-renders
|
|
5
|
+
* when a cache write touches unrelated parts of the graph.
|
|
6
|
+
*
|
|
7
|
+
* Arrays are reconciled by index. When lengths differ the array itself gets a
|
|
8
|
+
* new reference, but individual matching elements are still recycled so that
|
|
9
|
+
* items from the previous render keep their identities.
|
|
10
|
+
*/
|
|
11
|
+
export function recycleNodesInto<T>(prev: T | null | undefined, next: T): T {
|
|
12
|
+
if (Object.is(prev, next)) return prev as T
|
|
13
|
+
|
|
14
|
+
if (next === null || typeof next !== 'object') return next
|
|
15
|
+
if (prev === null || prev === undefined || typeof prev !== 'object') return next
|
|
16
|
+
|
|
17
|
+
if (Array.isArray(next)) {
|
|
18
|
+
if (!Array.isArray(prev)) return next
|
|
19
|
+
|
|
20
|
+
const nextLen = next.length
|
|
21
|
+
const prevLen = (prev as unknown[]).length
|
|
22
|
+
const minLen = Math.min(prevLen, nextLen)
|
|
23
|
+
|
|
24
|
+
let changed = false
|
|
25
|
+
const result: unknown[] = new Array(nextLen)
|
|
26
|
+
|
|
27
|
+
for (let i = 0; i < minLen; i++) {
|
|
28
|
+
result[i] = recycleNodesInto((prev as unknown[])[i], next[i])
|
|
29
|
+
if (result[i] !== (prev as unknown[])[i]) changed = true
|
|
30
|
+
}
|
|
31
|
+
for (let i = minLen; i < nextLen; i++) {
|
|
32
|
+
result[i] = next[i]
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (!changed && prevLen === nextLen) return prev as T
|
|
36
|
+
|
|
37
|
+
// Copy non-index own properties from next (e.g. __id from @includeListID)
|
|
38
|
+
for (const key of Object.keys(next as any)) {
|
|
39
|
+
if (isNaN(Number(key))) {
|
|
40
|
+
;(result as any)[key] = (next as any)[key]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return result as unknown as T
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const prevObj = prev as Record<string, unknown>
|
|
47
|
+
const nextObj = next as Record<string, unknown>
|
|
48
|
+
const nextKeys = Object.keys(nextObj)
|
|
49
|
+
const prevKeyCount = Object.keys(prevObj).length
|
|
50
|
+
|
|
51
|
+
let changed = prevKeyCount !== nextKeys.length
|
|
52
|
+
const result: Record<string, unknown> = {}
|
|
53
|
+
|
|
54
|
+
for (const key of nextKeys) {
|
|
55
|
+
result[key] = recycleNodesInto(prevObj[key], nextObj[key])
|
|
56
|
+
if (result[key] !== prevObj[key]) changed = true
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return changed ? (result as T) : (prev as T)
|
|
60
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
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
|
+
|
|
40
|
+
if (!deepEquals(value, ref.current)) {
|
|
41
|
+
ref.current = value
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Return ref.current directly so React.useEffect sees a new reference only
|
|
45
|
+
// when the deep value actually changes — matching the original use-deep-compare-effect.
|
|
46
|
+
// The useMemo(() => ref.current, []) wrapper that was here previously was incorrect:
|
|
47
|
+
// it froze the returned value at mount time, preventing effects from ever re-firing.
|
|
48
|
+
return ref.current
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function useDeepCompareEffect(
|
|
52
|
+
callback: EffectCallback,
|
|
53
|
+
dependencies: DependencyList
|
|
54
|
+
): UseEffectReturn {
|
|
55
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
56
|
+
checkDeps(dependencies)
|
|
57
|
+
}
|
|
58
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: intentional deep-compare effect
|
|
59
|
+
return React.useEffect(callback, useDeepCompareMemoize(dependencies))
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function useDeepCompareEffectNoCheck(
|
|
63
|
+
callback: EffectCallback,
|
|
64
|
+
dependencies: DependencyList
|
|
65
|
+
): UseEffectReturn {
|
|
66
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: intentional deep-compare effect
|
|
67
|
+
return React.useEffect(callback, useDeepCompareMemoize(dependencies))
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export default useDeepCompareEffect
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
The MIT License (MIT)
|
|
74
|
+
Copyright (c) 2020 Kent C. Dodds
|
|
75
|
+
|
|
76
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
77
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
78
|
+
in the Software without restriction, including without limitation the rights
|
|
79
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
80
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
81
|
+
furnished to do so, subject to the following conditions:
|
|
82
|
+
|
|
83
|
+
The above copyright notice and this permission notice shall be included in all
|
|
84
|
+
copies or substantial portions of the Software.
|
|
85
|
+
|
|
86
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
87
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
88
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
89
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
90
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
91
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
92
|
+
SOFTWARE.
|
|
93
|
+
*/
|
|
@@ -0,0 +1,238 @@
|
|
|
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
|
+
GraphQLError,
|
|
8
|
+
CursorHandlers,
|
|
9
|
+
OffsetHandlers,
|
|
10
|
+
PageInfo,
|
|
11
|
+
FetchFn,
|
|
12
|
+
QueryResult,
|
|
13
|
+
DocumentArtifact,
|
|
14
|
+
QueryArtifact,
|
|
15
|
+
} from 'houdini/runtime'
|
|
16
|
+
import type { DocumentStore } from 'houdini/runtime/client'
|
|
17
|
+
import React from 'react'
|
|
18
|
+
|
|
19
|
+
import { useClient, useLocation, useSession } from '../routing/Router.js'
|
|
20
|
+
|
|
21
|
+
export function useDocumentHandle<
|
|
22
|
+
_Artifact extends QueryArtifact,
|
|
23
|
+
_Data extends GraphQLObject,
|
|
24
|
+
_Input extends GraphQLVariables,
|
|
25
|
+
>({
|
|
26
|
+
artifact,
|
|
27
|
+
observer,
|
|
28
|
+
storeValue,
|
|
29
|
+
}: {
|
|
30
|
+
artifact: DocumentArtifact
|
|
31
|
+
observer: DocumentStore<_Data, _Input>
|
|
32
|
+
storeValue: QueryResult<_Data, _Input>
|
|
33
|
+
}): DocumentHandle<_Artifact, _Data, _Input> & { fetch: FetchFn<_Data, _Input> } {
|
|
34
|
+
const [forwardPending, setForwardPending] = React.useState(false)
|
|
35
|
+
const [backwardPending, setBackwardPending] = React.useState(false)
|
|
36
|
+
// Stable cursor stacks for SinglePage pagination — must survive re-renders caused by store updates
|
|
37
|
+
const previousCursorsRef = React.useRef<(string | null)[]>([])
|
|
38
|
+
const nextCursorsRef = React.useRef<(string | null)[]>([])
|
|
39
|
+
const location = useLocation()
|
|
40
|
+
|
|
41
|
+
// grab the current session value
|
|
42
|
+
const [session] = useSession()
|
|
43
|
+
|
|
44
|
+
// we want to use a separate observer for pagination queries
|
|
45
|
+
const client = useClient()
|
|
46
|
+
const paginationObserver = React.useMemo(() => {
|
|
47
|
+
// if the artifact doesn't support pagination, don't do anything
|
|
48
|
+
if (!artifact.refetch?.paginated) {
|
|
49
|
+
return null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return client.observe<_Data, _Input>({ artifact })
|
|
53
|
+
}, [artifact.name, artifact, client.observe])
|
|
54
|
+
|
|
55
|
+
// @ts-expect-error: avoiding an as DocumentHandle<_Artifact, _Data, _Input>
|
|
56
|
+
return React.useMemo<DocumentHandle<_Artifact, _Data, _Input>>(() => {
|
|
57
|
+
const wrapLoad = <_Result>(
|
|
58
|
+
setLoading: (val: boolean) => void,
|
|
59
|
+
fn: (value: any) => Promise<_Result>
|
|
60
|
+
) => {
|
|
61
|
+
return async (value: any) => {
|
|
62
|
+
setLoading(true)
|
|
63
|
+
let result: _Result | null = null
|
|
64
|
+
let err: Error | null = null
|
|
65
|
+
try {
|
|
66
|
+
result = await fn(value)
|
|
67
|
+
} catch (e) {
|
|
68
|
+
err = e as Error
|
|
69
|
+
}
|
|
70
|
+
setLoading(false)
|
|
71
|
+
// ignore abort errors when loading pages
|
|
72
|
+
if (err && err.name !== 'AbortError') {
|
|
73
|
+
throw err
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// we're done
|
|
77
|
+
return result || observer.state
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// add the session value to the
|
|
82
|
+
const fetchQuery: FetchFn<_Data, _Input> = (args) => {
|
|
83
|
+
// before we send the query, we need to figure out which variables are
|
|
84
|
+
// actually useful for this document
|
|
85
|
+
const usedVariables = Object.fromEntries(
|
|
86
|
+
Object.keys(observer.artifact.input?.fields ?? {}).reduce<[string, any][]>(
|
|
87
|
+
(entries, fieldName) => {
|
|
88
|
+
// if the field is not a url parameter, skip it
|
|
89
|
+
if (!(fieldName in location.params)) {
|
|
90
|
+
return entries
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return [...entries, [fieldName, location.params[fieldName]]]
|
|
94
|
+
},
|
|
95
|
+
[]
|
|
96
|
+
)
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
return observer.send({
|
|
100
|
+
...args,
|
|
101
|
+
variables: {
|
|
102
|
+
...usedVariables,
|
|
103
|
+
...args?.variables,
|
|
104
|
+
},
|
|
105
|
+
session,
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const baseFields = {
|
|
110
|
+
artifact,
|
|
111
|
+
data: storeValue.data,
|
|
112
|
+
variables: storeValue.variables,
|
|
113
|
+
partial: storeValue.partial,
|
|
114
|
+
fetching: storeValue.fetching,
|
|
115
|
+
errors: storeValue.errors,
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// only consider paginated queries
|
|
119
|
+
if (artifact.kind !== ArtifactKind.Query || !artifact.refetch?.paginated) {
|
|
120
|
+
return {
|
|
121
|
+
...baseFields,
|
|
122
|
+
fetch: fetchQuery,
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// if the artifact supports cursor pagination, then add the cursor handlers
|
|
127
|
+
if (artifact.refetch.method === 'cursor') {
|
|
128
|
+
const handlers = cursorHandlers<_Data, _Input>({
|
|
129
|
+
artifact,
|
|
130
|
+
getState: () => storeValue.data,
|
|
131
|
+
getVariables: () => storeValue.variables!,
|
|
132
|
+
fetch: fetchQuery,
|
|
133
|
+
previousCursors: previousCursorsRef.current,
|
|
134
|
+
nextCursors: nextCursorsRef.current,
|
|
135
|
+
fetchUpdate: (args, updates) => {
|
|
136
|
+
return paginationObserver!.send({
|
|
137
|
+
...args,
|
|
138
|
+
cacheParams: {
|
|
139
|
+
...args?.cacheParams,
|
|
140
|
+
disableSubscriptions: true,
|
|
141
|
+
applyUpdates: updates,
|
|
142
|
+
},
|
|
143
|
+
session,
|
|
144
|
+
})
|
|
145
|
+
},
|
|
146
|
+
getSession: async () => session,
|
|
147
|
+
})
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
...baseFields,
|
|
151
|
+
fetch: handlers.fetch,
|
|
152
|
+
loadNext: wrapLoad(setForwardPending, handlers.loadNextPage),
|
|
153
|
+
loadNextPending: forwardPending,
|
|
154
|
+
loadPrevious: wrapLoad(setBackwardPending, handlers.loadPreviousPage),
|
|
155
|
+
loadPreviousPending: backwardPending,
|
|
156
|
+
pageInfo: extractPageInfo(storeValue.data, artifact.refetch!.path),
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (artifact.refetch.method === 'offset') {
|
|
161
|
+
const handlers = offsetHandlers({
|
|
162
|
+
artifact,
|
|
163
|
+
getState: () => storeValue.data,
|
|
164
|
+
getVariables: () => storeValue.variables!,
|
|
165
|
+
storeName: artifact.name,
|
|
166
|
+
fetch: fetchQuery,
|
|
167
|
+
fetchUpdate: async (args, updates = ['append']) => {
|
|
168
|
+
return paginationObserver!.send({
|
|
169
|
+
...args,
|
|
170
|
+
cacheParams: {
|
|
171
|
+
disableSubscriptions: true,
|
|
172
|
+
applyUpdates: updates,
|
|
173
|
+
...args?.cacheParams,
|
|
174
|
+
},
|
|
175
|
+
})
|
|
176
|
+
},
|
|
177
|
+
getSession: async () => session,
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
return {
|
|
181
|
+
...baseFields,
|
|
182
|
+
fetch: handlers.fetch,
|
|
183
|
+
loadNext: wrapLoad(setForwardPending, handlers.loadNextPage),
|
|
184
|
+
loadNextPending: forwardPending,
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// we don't want to add anything
|
|
189
|
+
return {
|
|
190
|
+
...baseFields,
|
|
191
|
+
fetch: fetchQuery,
|
|
192
|
+
refetch: fetchQuery,
|
|
193
|
+
}
|
|
194
|
+
}, [
|
|
195
|
+
artifact,
|
|
196
|
+
observer,
|
|
197
|
+
session,
|
|
198
|
+
storeValue,
|
|
199
|
+
backwardPending,
|
|
200
|
+
forwardPending,
|
|
201
|
+
paginationObserver,
|
|
202
|
+
location.params,
|
|
203
|
+
])
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export type DocumentHandle<
|
|
207
|
+
_Artifact extends QueryArtifact,
|
|
208
|
+
_Data extends GraphQLObject = GraphQLObject,
|
|
209
|
+
_Input extends GraphQLVariables = GraphQLVariables,
|
|
210
|
+
> = {
|
|
211
|
+
data: _Data
|
|
212
|
+
partial: boolean
|
|
213
|
+
fetching: boolean
|
|
214
|
+
errors: GraphQLError[] | null
|
|
215
|
+
fetch: FetchFn<_Data, Partial<_Input>>
|
|
216
|
+
variables: _Input
|
|
217
|
+
} & RefetchHandlers<_Artifact, _Data, _Input>
|
|
218
|
+
|
|
219
|
+
type RefetchHandlers<_Artifact extends QueryArtifact, _Data extends GraphQLObject, _Input> =
|
|
220
|
+
// we need to add different methods if the artifact supports cursor pagination
|
|
221
|
+
_Artifact extends {
|
|
222
|
+
refetch: { paginated: true; method: 'cursor' }
|
|
223
|
+
}
|
|
224
|
+
? {
|
|
225
|
+
loadNext: CursorHandlers<_Data, _Input>['loadNextPage']
|
|
226
|
+
loadNextPending: boolean
|
|
227
|
+
loadPrevious: CursorHandlers<_Data, _Input>['loadPreviousPage']
|
|
228
|
+
loadPreviousPending: boolean
|
|
229
|
+
pageInfo: PageInfo
|
|
230
|
+
}
|
|
231
|
+
: // offset pagination
|
|
232
|
+
_Artifact extends { refetch: { paginated: true; method: 'offset' } }
|
|
233
|
+
? {
|
|
234
|
+
loadNext: OffsetHandlers<_Data, _Input>['loadNextPage']
|
|
235
|
+
loadNextPending: boolean
|
|
236
|
+
}
|
|
237
|
+
: // the artifact does not support a known pagination method, don't add anything
|
|
238
|
+
{}
|
|
@@ -0,0 +1,112 @@
|
|
|
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/index.js'
|
|
11
|
+
import { useIsMountedRef } from './useIsMounted.js'
|
|
12
|
+
import { recycleNodesInto } from './recycleNodesInto.js'
|
|
13
|
+
|
|
14
|
+
export type UseDocumentStoreParams<
|
|
15
|
+
_Artifact extends DocumentArtifact,
|
|
16
|
+
_Data extends GraphQLObject,
|
|
17
|
+
_Input extends GraphQLVariables,
|
|
18
|
+
> = {
|
|
19
|
+
artifact: _Artifact
|
|
20
|
+
observer?: DocumentStore<_Data, _Input>
|
|
21
|
+
// Optional synchronous seed for box.current. When provided, box.current is updated
|
|
22
|
+
// during render so useSyncExternalStore's snapshot is immediately correct (e.g. on
|
|
23
|
+
// fragment parent change). Must be memoized by the caller — tracked by reference.
|
|
24
|
+
initialState?: QueryResult<_Data, _Input>
|
|
25
|
+
} & Partial<ObserveParams<_Data, DocumentArtifact, _Input>>
|
|
26
|
+
|
|
27
|
+
export function useDocumentStore<
|
|
28
|
+
_Data extends GraphQLObject = GraphQLObject,
|
|
29
|
+
_Input extends GraphQLVariables = GraphQLVariables,
|
|
30
|
+
_Artifact extends DocumentArtifact = DocumentArtifact,
|
|
31
|
+
>({
|
|
32
|
+
artifact,
|
|
33
|
+
observer: obs,
|
|
34
|
+
initialState,
|
|
35
|
+
...observeParams
|
|
36
|
+
}: UseDocumentStoreParams<_Artifact, _Data, _Input>): [
|
|
37
|
+
QueryResult<_Data, _Input>,
|
|
38
|
+
DocumentStore<_Data, _Input>,
|
|
39
|
+
] {
|
|
40
|
+
const client = useClient()
|
|
41
|
+
const isMountedRef = useIsMountedRef()
|
|
42
|
+
|
|
43
|
+
// hold onto an observer we'll use
|
|
44
|
+
const [observer, setObserver] = React.useState(
|
|
45
|
+
() =>
|
|
46
|
+
obs ??
|
|
47
|
+
client.observe<_Data, _Input>({
|
|
48
|
+
artifact,
|
|
49
|
+
...observeParams,
|
|
50
|
+
})
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
const box = React.useRef(observer.state)
|
|
54
|
+
|
|
55
|
+
// if the observer changes, we need to track the new one
|
|
56
|
+
if (obs && obs !== observer) {
|
|
57
|
+
box.current = obs.state
|
|
58
|
+
setObserver(obs)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Relay-style synchronous seeding: when initialState changes (i.e., the fragment
|
|
62
|
+
// parent changed), update box.current immediately during this render so
|
|
63
|
+
// useSyncExternalStore's getSnapshot returns the correct data without waiting for
|
|
64
|
+
// the subscription effect to fire. Tracked by reference — if provided, callers
|
|
65
|
+
// must memoize initialState to avoid spurious reseeds on every render.
|
|
66
|
+
const prevInitialStateRef = React.useRef<QueryResult<_Data, _Input> | undefined>(undefined)
|
|
67
|
+
if (initialState !== undefined && initialState !== prevInitialStateRef.current) {
|
|
68
|
+
prevInitialStateRef.current = initialState
|
|
69
|
+
box.current = initialState
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// the function that registers a new subscription for the observer
|
|
73
|
+
const subscribe: any = React.useCallback(
|
|
74
|
+
(fn: () => void) => {
|
|
75
|
+
return observer.subscribe((val) => {
|
|
76
|
+
const prev = box.current
|
|
77
|
+
// Preserve object identity for unchanged subtrees so React.memo on
|
|
78
|
+
// fragment components can bail out when their data wasn't touched.
|
|
79
|
+
const stableData = recycleNodesInto(prev?.data, val.data)
|
|
80
|
+
const next = stableData === val.data ? val : { ...val, data: stableData }
|
|
81
|
+
|
|
82
|
+
// Skip the re-render entirely if the new state is semantically identical
|
|
83
|
+
// to what React already has (e.g. an idempotent cache write).
|
|
84
|
+
if (
|
|
85
|
+
next === prev ||
|
|
86
|
+
(stableData === prev?.data &&
|
|
87
|
+
val.fetching === prev?.fetching &&
|
|
88
|
+
val.errors === prev?.errors &&
|
|
89
|
+
val.source === prev?.source &&
|
|
90
|
+
val.stale === prev?.stale)
|
|
91
|
+
) {
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
box.current = next
|
|
96
|
+
if (isMountedRef.current) {
|
|
97
|
+
fn()
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
},
|
|
101
|
+
[observer, isMountedRef.current]
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
// get a safe reference to the cache
|
|
105
|
+
const storeValue = React.useSyncExternalStore(
|
|
106
|
+
subscribe,
|
|
107
|
+
() => box.current,
|
|
108
|
+
() => box.current
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
return [storeValue!, observer]
|
|
112
|
+
}
|