houdini-react 2.0.0-go.4 → 2.0.0-go.6
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 +13 -9
- package/postInstall.js +147 -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 +89 -0
- package/runtime/hooks/useDocumentHandle.ts +224 -0
- package/runtime/hooks/useDocumentStore.ts +76 -0
- package/runtime/hooks/useDocumentSubscription.ts +62 -0
- package/runtime/hooks/useFragment.ts +102 -0
- package/runtime/hooks/useFragmentHandle.ts +47 -0
- package/runtime/hooks/useIsMounted.ts +14 -0
- package/runtime/hooks/useMutation.ts +54 -0
- package/runtime/hooks/useQuery.ts +17 -0
- package/runtime/hooks/useQueryHandle.ts +184 -0
- package/runtime/hooks/useSubscription.ts +12 -0
- package/runtime/hooks/useSubscriptionHandle.ts +33 -0
- package/runtime/index.tsx +49 -0
- package/runtime/manifest.ts +6 -0
- package/runtime/package.json +1 -0
- package/runtime/routing/Router.tsx +887 -0
- package/runtime/routing/cache.ts +52 -0
- package/runtime/routing/index.ts +2 -0
- package/server/index.d.ts +1 -0
- package/server/index.js +4 -0
- package/server/package.json +1 -0
- package/vite/index.d.ts +3 -0
- package/vite/index.js +11 -0
- package/vite/package.json +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "houdini-react",
|
|
3
|
-
"version": "2.0.0-go.
|
|
3
|
+
"version": "2.0.0-go.6",
|
|
4
4
|
"description": "The React plugin for houdini",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"express": "^4.18.2",
|
|
36
36
|
"graphql": "^15.8.0",
|
|
37
37
|
"graphql-yoga": "^4.0.4",
|
|
38
|
-
"houdini": "^2.0.0-go.
|
|
38
|
+
"houdini": "^2.0.0-go.6",
|
|
39
39
|
"react": "^19.0.0",
|
|
40
40
|
"react-dom": "^19.0.0",
|
|
41
41
|
"react-streaming-compat": "^0.3.18",
|
|
@@ -44,7 +44,11 @@
|
|
|
44
44
|
"use-deep-compare-effect": "^1.8.1"
|
|
45
45
|
},
|
|
46
46
|
"files": [
|
|
47
|
-
"
|
|
47
|
+
"postInstall.js",
|
|
48
|
+
"runtime",
|
|
49
|
+
"server",
|
|
50
|
+
"shim.cjs",
|
|
51
|
+
"vite"
|
|
48
52
|
],
|
|
49
53
|
"exports": {
|
|
50
54
|
"./package.json": "./package.json",
|
|
@@ -76,12 +80,12 @@
|
|
|
76
80
|
}
|
|
77
81
|
},
|
|
78
82
|
"optionalDependencies": {
|
|
79
|
-
"houdini-react-darwin-x64": "2.0.0-go.
|
|
80
|
-
"houdini-react-darwin-arm64": "2.0.0-go.
|
|
81
|
-
"houdini-react-linux-x64": "2.0.0-go.
|
|
82
|
-
"houdini-react-linux-arm64": "2.0.0-go.
|
|
83
|
-
"houdini-react-win32-x64": "2.0.0-go.
|
|
84
|
-
"houdini-react-win32-arm64": "2.0.0-go.
|
|
83
|
+
"houdini-react-darwin-x64": "2.0.0-go.6",
|
|
84
|
+
"houdini-react-darwin-arm64": "2.0.0-go.6",
|
|
85
|
+
"houdini-react-linux-x64": "2.0.0-go.6",
|
|
86
|
+
"houdini-react-linux-arm64": "2.0.0-go.6",
|
|
87
|
+
"houdini-react-win32-x64": "2.0.0-go.6",
|
|
88
|
+
"houdini-react-win32-arm64": "2.0.0-go.6"
|
|
85
89
|
},
|
|
86
90
|
"bin": "./shim.cjs",
|
|
87
91
|
"scripts": {
|
package/postInstall.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
const fs = require('fs')
|
|
2
|
+
const path = require('path')
|
|
3
|
+
const zlib = require('zlib')
|
|
4
|
+
const https = require('https')
|
|
5
|
+
|
|
6
|
+
// Adjust the version you want to install. You can also make this dynamic.
|
|
7
|
+
const BINARY_DISTRIBUTION_VERSION = '2.0.0-go.6'
|
|
8
|
+
|
|
9
|
+
// Windows binaries end with .exe so we need to special case them.
|
|
10
|
+
const binaryName = process.platform === 'win32' ? 'houdini-react.exe' : 'houdini-react'
|
|
11
|
+
|
|
12
|
+
// Determine package name for this platform
|
|
13
|
+
const platformSpecificPackageName = `houdini-react-${process.platform}-${process.arch}`
|
|
14
|
+
|
|
15
|
+
// Compute the path we want to emit the fallback binary to
|
|
16
|
+
const fallbackBinaryPath = path.join(__dirname, binaryName)
|
|
17
|
+
|
|
18
|
+
function makeRequest(url) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
https
|
|
21
|
+
.get(url, (response) => {
|
|
22
|
+
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
23
|
+
const chunks = []
|
|
24
|
+
response.on('data', (chunk) => chunks.push(chunk))
|
|
25
|
+
response.on('end', () => {
|
|
26
|
+
resolve(Buffer.concat(chunks))
|
|
27
|
+
})
|
|
28
|
+
} else if (
|
|
29
|
+
response.statusCode >= 300 &&
|
|
30
|
+
response.statusCode < 400 &&
|
|
31
|
+
response.headers.location
|
|
32
|
+
) {
|
|
33
|
+
// Follow redirects
|
|
34
|
+
makeRequest(response.headers.location).then(resolve, reject)
|
|
35
|
+
} else {
|
|
36
|
+
reject(
|
|
37
|
+
new Error(
|
|
38
|
+
`npm responded with status code ${response.statusCode} when downloading the package!`
|
|
39
|
+
)
|
|
40
|
+
)
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
.on('error', (error) => {
|
|
44
|
+
reject(error)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function extractFileFromTarball(tarballBuffer, filepath) {
|
|
50
|
+
// Tar archives are organized in 512 byte blocks.
|
|
51
|
+
// Blocks can either be header blocks or data blocks.
|
|
52
|
+
// Header blocks contain file names of the archive in the first 100 bytes, terminated by a null byte.
|
|
53
|
+
// The size of a file is contained in bytes 124-135 of a header block and in octal format.
|
|
54
|
+
// The following blocks will be data blocks containing the file.
|
|
55
|
+
let offset = 0
|
|
56
|
+
while (offset < tarballBuffer.length) {
|
|
57
|
+
const header = tarballBuffer.subarray(offset, offset + 512)
|
|
58
|
+
offset += 512
|
|
59
|
+
|
|
60
|
+
const fileName = header.toString('utf-8', 0, 100).replace(/\0.*/g, '')
|
|
61
|
+
const fileSize = parseInt(header.toString('utf-8', 124, 136).replace(/\0.*/g, ''), 8)
|
|
62
|
+
|
|
63
|
+
if (fileName === filepath) {
|
|
64
|
+
return tarballBuffer.subarray(offset, offset + fileSize)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Clamp offset to the uppoer multiple of 512
|
|
68
|
+
offset = (offset + fileSize + 511) & ~511
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function downloadBinaryFromNpm() {
|
|
73
|
+
// Download the tarball of the right binary distribution package
|
|
74
|
+
const tarballDownloadBuffer = await makeRequest(
|
|
75
|
+
`https://registry.npmjs.org/${platformSpecificPackageName}/-/${platformSpecificPackageName}-${BINARY_DISTRIBUTION_VERSION}.tgz`
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
const tarballBuffer = zlib.unzipSync(tarballDownloadBuffer)
|
|
79
|
+
|
|
80
|
+
// Extract binary from package and write to disk
|
|
81
|
+
fs.writeFileSync(
|
|
82
|
+
fallbackBinaryPath,
|
|
83
|
+
extractFileFromTarball(tarballBuffer, `package/bin/${binaryName}`),
|
|
84
|
+
{ mode: 0o755 } // Make binary file executable
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function isPlatformSpecificPackageInstalled() {
|
|
89
|
+
try {
|
|
90
|
+
// Try to resolve the platform package itself
|
|
91
|
+
require.resolve(`${platformSpecificPackageName}/package.json`)
|
|
92
|
+
return true
|
|
93
|
+
} catch (e) {
|
|
94
|
+
// Also check if it exists as a sibling directory
|
|
95
|
+
const siblingPath = path.join(__dirname, '..', platformSpecificPackageName)
|
|
96
|
+
const siblingBinaryPath = path.join(siblingPath, 'bin', binaryName)
|
|
97
|
+
return fs.existsSync(siblingBinaryPath)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!platformSpecificPackageName) {
|
|
102
|
+
throw new Error('Platform not supported!')
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// once we've confirmed the required package is installed we want to overwrite the bin entry of our package.json
|
|
106
|
+
// to point to the correct binary for optimal performance (skip Node.js overhead)
|
|
107
|
+
function overwriteBinary() {
|
|
108
|
+
const packageJsonPath = path.join(__dirname, 'package.json')
|
|
109
|
+
const packageJson = require(packageJsonPath)
|
|
110
|
+
|
|
111
|
+
let binaryPath = null
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
// Method 1: Use require.resolve to find the actual path to the platform-specific package
|
|
115
|
+
// This works with pnpm and other package managers that use symlinks
|
|
116
|
+
const platformPackagePath = require.resolve(`${platformSpecificPackageName}/package.json`)
|
|
117
|
+
const platformPackageDir = path.dirname(platformPackagePath)
|
|
118
|
+
binaryPath = path.join(platformPackageDir, 'bin', binaryName)
|
|
119
|
+
} catch (error) {
|
|
120
|
+
// Method 2: Check if platform package is installed as a sibling directory
|
|
121
|
+
// This works with npm and local installs
|
|
122
|
+
const siblingPath = path.join(__dirname, '..', platformSpecificPackageName)
|
|
123
|
+
const siblingBinaryPath = path.join(siblingPath, 'bin', binaryName)
|
|
124
|
+
|
|
125
|
+
if (fs.existsSync(siblingBinaryPath)) {
|
|
126
|
+
binaryPath = siblingBinaryPath
|
|
127
|
+
} else {
|
|
128
|
+
// Use shim as fallback
|
|
129
|
+
return
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (binaryPath) {
|
|
134
|
+
// Make the path relative to the main package directory
|
|
135
|
+
const relativeBinaryPath = path.relative(__dirname, binaryPath)
|
|
136
|
+
packageJson.bin = relativeBinaryPath
|
|
137
|
+
|
|
138
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Skip downloading the binary if it was already installed via optionalDependencies
|
|
143
|
+
if (!isPlatformSpecificPackageInstalled()) {
|
|
144
|
+
downloadBinaryFromNpm().then(overwriteBinary)
|
|
145
|
+
} else {
|
|
146
|
+
overwriteBinary()
|
|
147
|
+
}
|
|
@@ -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'
|
|
2
|
+
export { useQueryHandle } from './useQueryHandle'
|
|
3
|
+
export { useFragment } from './useFragment'
|
|
4
|
+
export { useFragmentHandle } from './useFragmentHandle'
|
|
5
|
+
export { useMutation } from './useMutation'
|
|
6
|
+
export { useSubscription } from './useSubscription'
|
|
7
|
+
|
|
8
|
+
export { type DocumentHandle } from './useDocumentHandle'
|
|
9
|
+
export { type UseQueryConfig } from './useQueryHandle'
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { deepEquals } from 'houdini/runtime'
|
|
2
|
+
import * as React from 'react'
|
|
3
|
+
|
|
4
|
+
// This file is largely a copy and paste from Kent C. Dodd's use-deep-compare-effect (appropriate license at the bottom).
|
|
5
|
+
// It has been copied locally in order to avoid any awkward third party peer dependencies
|
|
6
|
+
// on generated files (which would make the install annoying). The deep equals library has
|
|
7
|
+
// also been changed to use one that was already included in the runtime (avoiding the extra bundle size)
|
|
8
|
+
|
|
9
|
+
type UseEffectParams = Parameters<typeof React.useEffect>
|
|
10
|
+
type EffectCallback = UseEffectParams[0]
|
|
11
|
+
type DependencyList = UseEffectParams[1]
|
|
12
|
+
// yes, I know it's void, but I like what this communicates about
|
|
13
|
+
// the intent of these functions: It's just like useEffect
|
|
14
|
+
type UseEffectReturn = ReturnType<typeof React.useEffect>
|
|
15
|
+
|
|
16
|
+
function checkDeps(deps: DependencyList) {
|
|
17
|
+
if (!deps || !deps.length) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
'useDeepCompareEffect should not be used with no dependencies. Use React.useEffect instead.'
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
if (deps.every(isPrimitive)) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
'useDeepCompareEffect should not be used with dependencies that are all primitive values. Use React.useEffect instead.'
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function isPrimitive(val: unknown) {
|
|
30
|
+
return val == null || /^[sbn]/.test(typeof val)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @param value the value to be memoized (usually a dependency list)
|
|
35
|
+
* @returns a memoized version of the value as long as it remains deeply equal
|
|
36
|
+
*/
|
|
37
|
+
export function useDeepCompareMemoize<T>(value: T) {
|
|
38
|
+
const ref = React.useRef<T>(value)
|
|
39
|
+
const signalRef = React.useRef<number>(0)
|
|
40
|
+
|
|
41
|
+
if (!deepEquals(value, ref.current)) {
|
|
42
|
+
ref.current = value
|
|
43
|
+
signalRef.current += 1
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return React.useMemo(() => ref.current, [signalRef.current])
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function useDeepCompareEffect(
|
|
50
|
+
callback: EffectCallback,
|
|
51
|
+
dependencies: DependencyList
|
|
52
|
+
): UseEffectReturn {
|
|
53
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
54
|
+
checkDeps(dependencies)
|
|
55
|
+
}
|
|
56
|
+
return React.useEffect(callback, useDeepCompareMemoize(dependencies))
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function useDeepCompareEffectNoCheck(
|
|
60
|
+
callback: EffectCallback,
|
|
61
|
+
dependencies: DependencyList
|
|
62
|
+
): UseEffectReturn {
|
|
63
|
+
return React.useEffect(callback, useDeepCompareMemoize(dependencies))
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export default useDeepCompareEffect
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
The MIT License (MIT)
|
|
70
|
+
Copyright (c) 2020 Kent C. Dodds
|
|
71
|
+
|
|
72
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
73
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
74
|
+
in the Software without restriction, including without limitation the rights
|
|
75
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
76
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
77
|
+
furnished to do so, subject to the following conditions:
|
|
78
|
+
|
|
79
|
+
The above copyright notice and this permission notice shall be included in all
|
|
80
|
+
copies or substantial portions of the Software.
|
|
81
|
+
|
|
82
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
83
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
84
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
85
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
86
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
87
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
88
|
+
SOFTWARE.
|
|
89
|
+
*/
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { extractPageInfo } from 'houdini/runtime'
|
|
2
|
+
import { cursorHandlers, offsetHandlers } from 'houdini/runtime'
|
|
3
|
+
import { ArtifactKind } from 'houdini/runtime'
|
|
4
|
+
import type {
|
|
5
|
+
GraphQLObject,
|
|
6
|
+
GraphQLVariables,
|
|
7
|
+
CursorHandlers,
|
|
8
|
+
OffsetHandlers,
|
|
9
|
+
PageInfo,
|
|
10
|
+
FetchFn,
|
|
11
|
+
QueryResult,
|
|
12
|
+
DocumentArtifact,
|
|
13
|
+
QueryArtifact,
|
|
14
|
+
} from 'houdini/runtime'
|
|
15
|
+
import type { DocumentStore } from 'houdini/runtime/client'
|
|
16
|
+
import React from 'react'
|
|
17
|
+
|
|
18
|
+
import { useClient, useLocation, useSession } from '../routing/Router'
|
|
19
|
+
|
|
20
|
+
export function useDocumentHandle<
|
|
21
|
+
_Artifact extends QueryArtifact,
|
|
22
|
+
_Data extends GraphQLObject,
|
|
23
|
+
_Input extends GraphQLVariables
|
|
24
|
+
>({
|
|
25
|
+
artifact,
|
|
26
|
+
observer,
|
|
27
|
+
storeValue,
|
|
28
|
+
}: {
|
|
29
|
+
artifact: DocumentArtifact
|
|
30
|
+
observer: DocumentStore<_Data, _Input>
|
|
31
|
+
storeValue: QueryResult<_Data, _Input>
|
|
32
|
+
}): DocumentHandle<_Artifact, _Data, _Input> & { fetch: FetchFn<_Data, _Input> } {
|
|
33
|
+
const [forwardPending, setForwardPending] = React.useState(false)
|
|
34
|
+
const [backwardPending, setBackwardPending] = React.useState(false)
|
|
35
|
+
const location = useLocation()
|
|
36
|
+
|
|
37
|
+
// grab the current session value
|
|
38
|
+
const [session] = useSession()
|
|
39
|
+
|
|
40
|
+
// we want to use a separate observer for pagination queries
|
|
41
|
+
const client = useClient()
|
|
42
|
+
const paginationObserver = React.useMemo(() => {
|
|
43
|
+
// if the artifact doesn't support pagination, don't do anything
|
|
44
|
+
if (!artifact.refetch?.paginated) {
|
|
45
|
+
return null
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return client.observe<_Data, _Input>({ artifact })
|
|
49
|
+
}, [artifact.name])
|
|
50
|
+
|
|
51
|
+
// @ts-expect-error: avoiding an as DocumentHandle<_Artifact, _Data, _Input>
|
|
52
|
+
return React.useMemo<DocumentHandle<_Artifact, _Data, _Input>>(() => {
|
|
53
|
+
const wrapLoad = <_Result>(
|
|
54
|
+
setLoading: (val: boolean) => void,
|
|
55
|
+
fn: (value: any) => Promise<_Result>
|
|
56
|
+
) => {
|
|
57
|
+
return async (value: any) => {
|
|
58
|
+
setLoading(true)
|
|
59
|
+
let result: _Result | null = null
|
|
60
|
+
let err: Error | null = null
|
|
61
|
+
try {
|
|
62
|
+
result = await fn(value)
|
|
63
|
+
} catch (e) {
|
|
64
|
+
err = e as Error
|
|
65
|
+
}
|
|
66
|
+
setLoading(false)
|
|
67
|
+
// ignore abort errors when loading pages
|
|
68
|
+
if (err && err.name !== 'AbortError') {
|
|
69
|
+
throw err
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// we're done
|
|
73
|
+
return result || observer.state
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// add the session value to the
|
|
78
|
+
const fetchQuery: FetchFn<_Data, _Input> = (args) => {
|
|
79
|
+
// before we send the query, we need to figure out which variables are
|
|
80
|
+
// actually useful for this document
|
|
81
|
+
const usedVariables = Object.fromEntries(
|
|
82
|
+
Object.keys(observer.artifact.input?.fields ?? {}).reduce<[string, any][]>(
|
|
83
|
+
(entries, fieldName) => {
|
|
84
|
+
// if the field is not a url parameter, skip it
|
|
85
|
+
if (!(fieldName in location.params)) {
|
|
86
|
+
return entries
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return [...entries, [fieldName, location.params[fieldName]]]
|
|
90
|
+
},
|
|
91
|
+
[]
|
|
92
|
+
)
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
return observer.send({
|
|
96
|
+
...args,
|
|
97
|
+
variables: {
|
|
98
|
+
...usedVariables,
|
|
99
|
+
...args?.variables,
|
|
100
|
+
},
|
|
101
|
+
session,
|
|
102
|
+
})
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// only consider paginated queries
|
|
106
|
+
if (artifact.kind !== ArtifactKind.Query || !artifact.refetch?.paginated) {
|
|
107
|
+
return {
|
|
108
|
+
artifact,
|
|
109
|
+
data: storeValue.data,
|
|
110
|
+
variables: storeValue.variables,
|
|
111
|
+
fetch: fetchQuery,
|
|
112
|
+
partial: storeValue.partial,
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// if the artifact supports cursor pagination, then add the cursor handlers
|
|
117
|
+
if (artifact.refetch.method === 'cursor') {
|
|
118
|
+
const handlers = cursorHandlers<_Data, _Input>({
|
|
119
|
+
artifact,
|
|
120
|
+
getState: () => storeValue.data,
|
|
121
|
+
getVariables: () => storeValue.variables!,
|
|
122
|
+
fetch: fetchQuery,
|
|
123
|
+
fetchUpdate: (args, updates) => {
|
|
124
|
+
return paginationObserver!.send({
|
|
125
|
+
...args,
|
|
126
|
+
cacheParams: {
|
|
127
|
+
...args?.cacheParams,
|
|
128
|
+
disableSubscriptions: true,
|
|
129
|
+
applyUpdates: updates,
|
|
130
|
+
},
|
|
131
|
+
session,
|
|
132
|
+
})
|
|
133
|
+
},
|
|
134
|
+
getSession: async () => session,
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
artifact,
|
|
139
|
+
data: storeValue.data,
|
|
140
|
+
variables: storeValue.variables,
|
|
141
|
+
fetch: handlers.fetch,
|
|
142
|
+
partial: storeValue.partial,
|
|
143
|
+
loadNext: wrapLoad(setForwardPending, handlers.loadNextPage),
|
|
144
|
+
loadNextPending: forwardPending,
|
|
145
|
+
loadPrevious: wrapLoad(setBackwardPending, handlers.loadPreviousPage),
|
|
146
|
+
loadPreviousPending: backwardPending,
|
|
147
|
+
pageInfo: extractPageInfo(storeValue.data, artifact.refetch!.path),
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (artifact.refetch.method === 'offset') {
|
|
152
|
+
const handlers = offsetHandlers({
|
|
153
|
+
artifact,
|
|
154
|
+
getState: () => storeValue.data,
|
|
155
|
+
getVariables: () => storeValue.variables!,
|
|
156
|
+
storeName: artifact.name,
|
|
157
|
+
fetch: fetchQuery,
|
|
158
|
+
fetchUpdate: async (args, updates = ['append']) => {
|
|
159
|
+
return paginationObserver!.send({
|
|
160
|
+
...args,
|
|
161
|
+
cacheParams: {
|
|
162
|
+
disableSubscriptions: true,
|
|
163
|
+
applyUpdates: updates,
|
|
164
|
+
...args?.cacheParams,
|
|
165
|
+
},
|
|
166
|
+
})
|
|
167
|
+
},
|
|
168
|
+
getSession: async () => session,
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
return {
|
|
172
|
+
artifact,
|
|
173
|
+
data: storeValue.data,
|
|
174
|
+
variables: storeValue.variables,
|
|
175
|
+
fetch: handlers.fetch,
|
|
176
|
+
partial: storeValue.partial,
|
|
177
|
+
loadNext: wrapLoad(setForwardPending, handlers.loadNextPage),
|
|
178
|
+
loadNextPending: forwardPending,
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// we don't want to add anything
|
|
183
|
+
return {
|
|
184
|
+
artifact,
|
|
185
|
+
data: storeValue.data,
|
|
186
|
+
variables: storeValue.variables,
|
|
187
|
+
fetch: fetchQuery,
|
|
188
|
+
refetch: fetchQuery,
|
|
189
|
+
partial: storeValue.partial,
|
|
190
|
+
}
|
|
191
|
+
}, [artifact, observer, session, storeValue])
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export type DocumentHandle<
|
|
195
|
+
_Artifact extends QueryArtifact,
|
|
196
|
+
_Data extends GraphQLObject = GraphQLObject,
|
|
197
|
+
_Input extends GraphQLVariables = GraphQLVariables
|
|
198
|
+
> = {
|
|
199
|
+
data: _Data
|
|
200
|
+
partial: boolean
|
|
201
|
+
fetch: FetchFn<_Data, Partial<_Input>>
|
|
202
|
+
variables: _Input
|
|
203
|
+
} & RefetchHandlers<_Artifact, _Data, _Input>
|
|
204
|
+
|
|
205
|
+
type RefetchHandlers<_Artifact extends QueryArtifact, _Data extends GraphQLObject, _Input> =
|
|
206
|
+
// we need to add different methods if the artifact supports cursor pagination
|
|
207
|
+
_Artifact extends {
|
|
208
|
+
refetch: { paginated: true; method: 'cursor' }
|
|
209
|
+
}
|
|
210
|
+
? {
|
|
211
|
+
loadNext: CursorHandlers<_Data, _Input>['loadNextPage']
|
|
212
|
+
loadNextPending: boolean
|
|
213
|
+
loadPrevious: CursorHandlers<_Data, _Input>['loadPreviousPage']
|
|
214
|
+
loadPreviousPending: boolean
|
|
215
|
+
pageInfo: PageInfo
|
|
216
|
+
}
|
|
217
|
+
: // offset pagination
|
|
218
|
+
_Artifact extends { refetch: { paginated: true; method: 'offset' } }
|
|
219
|
+
? {
|
|
220
|
+
loadNext: OffsetHandlers<_Data, _Input>['loadNextPage']
|
|
221
|
+
loadNextPending: boolean
|
|
222
|
+
}
|
|
223
|
+
: // the artifact does not support a known pagination method, don't add anything
|
|
224
|
+
{}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
DocumentArtifact,
|
|
3
|
+
GraphQLVariables,
|
|
4
|
+
QueryResult,
|
|
5
|
+
GraphQLObject,
|
|
6
|
+
} from 'houdini/runtime'
|
|
7
|
+
import type { DocumentStore, ObserveParams } from 'houdini/runtime/client'
|
|
8
|
+
import * as React from 'react'
|
|
9
|
+
|
|
10
|
+
import { useClient } from '../routing'
|
|
11
|
+
import { useIsMountedRef } from './useIsMounted'
|
|
12
|
+
|
|
13
|
+
export type UseDocumentStoreParams<
|
|
14
|
+
_Artifact extends DocumentArtifact,
|
|
15
|
+
_Data extends GraphQLObject,
|
|
16
|
+
_Input extends GraphQLVariables
|
|
17
|
+
> = {
|
|
18
|
+
artifact: _Artifact
|
|
19
|
+
observer?: DocumentStore<_Data, _Input>
|
|
20
|
+
} & Partial<ObserveParams<_Data, DocumentArtifact, _Input>>
|
|
21
|
+
|
|
22
|
+
export function useDocumentStore<
|
|
23
|
+
_Data extends GraphQLObject = GraphQLObject,
|
|
24
|
+
_Input extends GraphQLVariables = GraphQLVariables,
|
|
25
|
+
_Artifact extends DocumentArtifact = DocumentArtifact
|
|
26
|
+
>({
|
|
27
|
+
artifact,
|
|
28
|
+
observer: obs,
|
|
29
|
+
...observeParams
|
|
30
|
+
}: UseDocumentStoreParams<_Artifact, _Data, _Input>): [
|
|
31
|
+
QueryResult<_Data, _Input>,
|
|
32
|
+
DocumentStore<_Data, _Input>
|
|
33
|
+
] {
|
|
34
|
+
const client = useClient()
|
|
35
|
+
const isMountedRef = useIsMountedRef()
|
|
36
|
+
|
|
37
|
+
// hold onto an observer we'll use
|
|
38
|
+
let [observer, setObserver] = React.useState(
|
|
39
|
+
() =>
|
|
40
|
+
obs ??
|
|
41
|
+
client.observe<_Data, _Input>({
|
|
42
|
+
artifact,
|
|
43
|
+
...observeParams,
|
|
44
|
+
})
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
const box = React.useRef(observer.state)
|
|
48
|
+
|
|
49
|
+
// if the observer changes, we need to track the new one
|
|
50
|
+
if (obs && obs !== observer) {
|
|
51
|
+
box.current = obs.state
|
|
52
|
+
setObserver(obs)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// the function that registers a new subscription for the observer
|
|
56
|
+
const subscribe: any = React.useCallback(
|
|
57
|
+
(fn: () => void) => {
|
|
58
|
+
return observer.subscribe((val) => {
|
|
59
|
+
box.current = val
|
|
60
|
+
if (isMountedRef.current) {
|
|
61
|
+
fn()
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
},
|
|
65
|
+
[observer]
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
// get a safe reference to the cache
|
|
69
|
+
const storeValue = React.useSyncExternalStore(
|
|
70
|
+
subscribe,
|
|
71
|
+
() => box.current,
|
|
72
|
+
() => box.current
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
return [storeValue!, observer]
|
|
76
|
+
}
|