@remix-relay/react 1.0.0
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/LICENSE +21 -0
- package/dist/index.d.mts +41 -0
- package/dist/index.d.ts +41 -0
- package/dist/index.js +208 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +170 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +79 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Dan Train
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { SuspenseProps, PropsWithChildren } from 'react';
|
|
3
|
+
import relay, { GraphQLTaggedNode as GraphQLTaggedNode$1, PreloadFetchPolicy } from 'react-relay';
|
|
4
|
+
import { Environment, OperationType, GraphQLTaggedNode, VariablesOf, RequestParameters, Variables, CacheConfig, Observable } from 'relay-runtime';
|
|
5
|
+
import { ServerRuntimeMetaFunction } from '@remix-run/server-runtime';
|
|
6
|
+
import { useQueryLoaderHookType } from 'react-relay/relay-hooks/useQueryLoader';
|
|
7
|
+
|
|
8
|
+
declare function Suspense({ children, ...rest }: SuspenseProps): react_jsx_runtime.JSX.Element;
|
|
9
|
+
|
|
10
|
+
declare function getClientLoaderQuery(environment: Environment): <TQuery extends OperationType>(query: GraphQLTaggedNode, variables: VariablesOf<TQuery>) => Promise<{
|
|
11
|
+
queryRef: relay.PreloadedQuery<TQuery, {}>;
|
|
12
|
+
data: Awaited<TQuery["response"]> | undefined;
|
|
13
|
+
}>;
|
|
14
|
+
declare function clientLoaderQuery<TQuery extends OperationType>(environment: Environment, query: GraphQLTaggedNode, variables: VariablesOf<TQuery>): Promise<{
|
|
15
|
+
queryRef: relay.PreloadedQuery<TQuery, {}>;
|
|
16
|
+
data: Awaited<TQuery["response"]> | undefined;
|
|
17
|
+
}>;
|
|
18
|
+
|
|
19
|
+
declare function RemixRelayProvider({ children }: PropsWithChildren): react_jsx_runtime.JSX.Element;
|
|
20
|
+
|
|
21
|
+
declare function getCachedResponse(params: RequestParameters, variables: Variables, cacheConfig: CacheConfig): Observable<unknown> | null;
|
|
22
|
+
|
|
23
|
+
declare function metaQuery<TQuery extends OperationType>(metaFunction: (args: Parameters<ServerRuntimeMetaFunction>[0] & {
|
|
24
|
+
data: TQuery["response"];
|
|
25
|
+
}) => ReturnType<ServerRuntimeMetaFunction>): ServerRuntimeMetaFunction<() => {
|
|
26
|
+
preloadedQuery: {
|
|
27
|
+
response: {
|
|
28
|
+
data: TQuery["response"];
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
} | {
|
|
32
|
+
data: TQuery["response"];
|
|
33
|
+
}>;
|
|
34
|
+
|
|
35
|
+
declare function useLoaderQuery<TQuery extends OperationType>(query: GraphQLTaggedNode$1, fetchPolicy?: PreloadFetchPolicy): [
|
|
36
|
+
TQuery["response"],
|
|
37
|
+
useQueryLoaderHookType<TQuery>[1],
|
|
38
|
+
useQueryLoaderHookType<TQuery>[2]
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
export { RemixRelayProvider, Suspense, clientLoaderQuery, getCachedResponse, getClientLoaderQuery, metaQuery, useLoaderQuery };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
+
import { SuspenseProps, PropsWithChildren } from 'react';
|
|
3
|
+
import relay, { GraphQLTaggedNode as GraphQLTaggedNode$1, PreloadFetchPolicy } from 'react-relay';
|
|
4
|
+
import { Environment, OperationType, GraphQLTaggedNode, VariablesOf, RequestParameters, Variables, CacheConfig, Observable } from 'relay-runtime';
|
|
5
|
+
import { ServerRuntimeMetaFunction } from '@remix-run/server-runtime';
|
|
6
|
+
import { useQueryLoaderHookType } from 'react-relay/relay-hooks/useQueryLoader';
|
|
7
|
+
|
|
8
|
+
declare function Suspense({ children, ...rest }: SuspenseProps): react_jsx_runtime.JSX.Element;
|
|
9
|
+
|
|
10
|
+
declare function getClientLoaderQuery(environment: Environment): <TQuery extends OperationType>(query: GraphQLTaggedNode, variables: VariablesOf<TQuery>) => Promise<{
|
|
11
|
+
queryRef: relay.PreloadedQuery<TQuery, {}>;
|
|
12
|
+
data: Awaited<TQuery["response"]> | undefined;
|
|
13
|
+
}>;
|
|
14
|
+
declare function clientLoaderQuery<TQuery extends OperationType>(environment: Environment, query: GraphQLTaggedNode, variables: VariablesOf<TQuery>): Promise<{
|
|
15
|
+
queryRef: relay.PreloadedQuery<TQuery, {}>;
|
|
16
|
+
data: Awaited<TQuery["response"]> | undefined;
|
|
17
|
+
}>;
|
|
18
|
+
|
|
19
|
+
declare function RemixRelayProvider({ children }: PropsWithChildren): react_jsx_runtime.JSX.Element;
|
|
20
|
+
|
|
21
|
+
declare function getCachedResponse(params: RequestParameters, variables: Variables, cacheConfig: CacheConfig): Observable<unknown> | null;
|
|
22
|
+
|
|
23
|
+
declare function metaQuery<TQuery extends OperationType>(metaFunction: (args: Parameters<ServerRuntimeMetaFunction>[0] & {
|
|
24
|
+
data: TQuery["response"];
|
|
25
|
+
}) => ReturnType<ServerRuntimeMetaFunction>): ServerRuntimeMetaFunction<() => {
|
|
26
|
+
preloadedQuery: {
|
|
27
|
+
response: {
|
|
28
|
+
data: TQuery["response"];
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
} | {
|
|
32
|
+
data: TQuery["response"];
|
|
33
|
+
}>;
|
|
34
|
+
|
|
35
|
+
declare function useLoaderQuery<TQuery extends OperationType>(query: GraphQLTaggedNode$1, fetchPolicy?: PreloadFetchPolicy): [
|
|
36
|
+
TQuery["response"],
|
|
37
|
+
useQueryLoaderHookType<TQuery>[1],
|
|
38
|
+
useQueryLoaderHookType<TQuery>[2]
|
|
39
|
+
];
|
|
40
|
+
|
|
41
|
+
export { RemixRelayProvider, Suspense, clientLoaderQuery, getCachedResponse, getClientLoaderQuery, metaQuery, useLoaderQuery };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
RemixRelayProvider: () => RemixRelayProvider,
|
|
34
|
+
Suspense: () => Suspense,
|
|
35
|
+
clientLoaderQuery: () => clientLoaderQuery,
|
|
36
|
+
getCachedResponse: () => getCachedResponse,
|
|
37
|
+
getClientLoaderQuery: () => getClientLoaderQuery,
|
|
38
|
+
metaQuery: () => metaQuery,
|
|
39
|
+
useLoaderQuery: () => useLoaderQuery
|
|
40
|
+
});
|
|
41
|
+
module.exports = __toCommonJS(src_exports);
|
|
42
|
+
|
|
43
|
+
// src/Suspense.tsx
|
|
44
|
+
var import_react2 = require("@remix-run/react");
|
|
45
|
+
var import_react3 = require("react");
|
|
46
|
+
|
|
47
|
+
// src/deferred-query-context.tsx
|
|
48
|
+
var import_react = require("react");
|
|
49
|
+
var import_jsx_runtime = require("react/jsx-runtime");
|
|
50
|
+
var DeferredQueryContext = (0, import_react.createContext)(null);
|
|
51
|
+
var SetDeferredQueryContext = (0, import_react.createContext)(() => {
|
|
52
|
+
});
|
|
53
|
+
function RemixRelayProvider({ children }) {
|
|
54
|
+
const [deferredQueries, setDeferredQueries] = (0, import_react.useState)(
|
|
55
|
+
null
|
|
56
|
+
);
|
|
57
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(SetDeferredQueryContext.Provider, { value: setDeferredQueries, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(DeferredQueryContext.Provider, { value: deferredQueries, children }) });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// src/Suspense.tsx
|
|
61
|
+
var import_jsx_runtime2 = require("react/jsx-runtime");
|
|
62
|
+
function Suspense({ children, ...rest }) {
|
|
63
|
+
const [mounted, setMounted] = (0, import_react3.useState)(false);
|
|
64
|
+
(0, import_react3.useEffect)(() => {
|
|
65
|
+
if (!mounted && typeof window !== "undefined") {
|
|
66
|
+
setMounted(true);
|
|
67
|
+
}
|
|
68
|
+
}, [mounted]);
|
|
69
|
+
const deferredQuery = (0, import_react3.useContext)(DeferredQueryContext);
|
|
70
|
+
return mounted ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react3.Suspense, { ...rest, children: deferredQuery ? /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_react2.Await, { resolve: deferredQuery, children }) : children }) : /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(import_jsx_runtime2.Fragment, { children: rest.fallback });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/client-loader-query.ts
|
|
74
|
+
var import_react_relay = __toESM(require("react-relay"));
|
|
75
|
+
var { fetchQuery, loadQuery } = import_react_relay.default;
|
|
76
|
+
function getClientLoaderQuery(environment) {
|
|
77
|
+
return async (query, variables) => clientLoaderQuery(environment, query, variables);
|
|
78
|
+
}
|
|
79
|
+
async function clientLoaderQuery(environment, query, variables) {
|
|
80
|
+
const data = await fetchQuery(environment, query, variables, {
|
|
81
|
+
fetchPolicy: "store-or-network"
|
|
82
|
+
}).toPromise();
|
|
83
|
+
const queryRef = loadQuery(environment, query, variables, {
|
|
84
|
+
fetchPolicy: "store-only"
|
|
85
|
+
});
|
|
86
|
+
return { queryRef, data };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/get-cached-response.ts
|
|
90
|
+
var import_relay_runtime = require("relay-runtime");
|
|
91
|
+
var responseCache = new import_relay_runtime.QueryResponseCache({
|
|
92
|
+
size: 100,
|
|
93
|
+
ttl: 5e3
|
|
94
|
+
});
|
|
95
|
+
function getCachedResponse(params, variables, cacheConfig) {
|
|
96
|
+
const isQuery = params.operationKind === "query";
|
|
97
|
+
const cacheKey = params.id ?? params.cacheID;
|
|
98
|
+
const forceFetch = cacheConfig && cacheConfig.force;
|
|
99
|
+
if (responseCache === null || !isQuery || forceFetch) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const fromCache = responseCache.get(cacheKey, variables);
|
|
103
|
+
const deferredFromCache = [];
|
|
104
|
+
for (let i = 0; ; i++) {
|
|
105
|
+
const deferred = responseCache.get(`${cacheKey}-${i}`, variables);
|
|
106
|
+
if (!deferred)
|
|
107
|
+
break;
|
|
108
|
+
deferredFromCache.push(deferred);
|
|
109
|
+
}
|
|
110
|
+
if (fromCache === null) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return import_relay_runtime.Observable.create((sink) => {
|
|
114
|
+
sink.next(fromCache);
|
|
115
|
+
for (const deferred of deferredFromCache) {
|
|
116
|
+
sink.next(deferred);
|
|
117
|
+
}
|
|
118
|
+
sink.complete();
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// src/meta-query.ts
|
|
123
|
+
function metaQuery(metaFunction) {
|
|
124
|
+
return ({ data, ...rest }) => {
|
|
125
|
+
const metaData = data && "data" in data ? data.data : data && "preloadedQuery" in data ? data.preloadedQuery.response.data : null;
|
|
126
|
+
return metaFunction({
|
|
127
|
+
data: metaData,
|
|
128
|
+
...rest
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/useLoaderQuery.ts
|
|
134
|
+
var import_react4 = require("@remix-run/react");
|
|
135
|
+
var import_react5 = require("react");
|
|
136
|
+
var import_react_dom = require("react-dom");
|
|
137
|
+
var import_react_relay2 = __toESM(require("react-relay"));
|
|
138
|
+
var import_tiny_invariant = __toESM(require("tiny-invariant"));
|
|
139
|
+
var { usePreloadedQuery, useQueryLoader, useRelayEnvironment } = import_react_relay2.default;
|
|
140
|
+
function useLoaderQuery(query, fetchPolicy = "network-only") {
|
|
141
|
+
const loaderData = (0, import_react4.useLoaderData)();
|
|
142
|
+
const preloadedQuery = "preloadedQuery" in loaderData ? loaderData.preloadedQuery : null;
|
|
143
|
+
const deferredQueries = "deferredQueries" in loaderData ? loaderData.deferredQueries : null;
|
|
144
|
+
const [deferredResult, setDeferredResult] = (0, import_react5.useState)(preloadedQuery);
|
|
145
|
+
const setDeferredQueries = (0, import_react5.useContext)(SetDeferredQueryContext);
|
|
146
|
+
(0, import_react5.useEffect)(() => {
|
|
147
|
+
if (deferredQueries) {
|
|
148
|
+
setDeferredQueries(
|
|
149
|
+
deferredQueries.then(async (deferredResults) => {
|
|
150
|
+
deferredResults.forEach((result) => {
|
|
151
|
+
(0, import_react_dom.flushSync)(() => {
|
|
152
|
+
setDeferredResult(
|
|
153
|
+
result
|
|
154
|
+
);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
return deferredResults;
|
|
158
|
+
})
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
}, [deferredQueries, preloadedQuery, setDeferredQueries, setDeferredResult]);
|
|
162
|
+
const environment = useRelayEnvironment();
|
|
163
|
+
(0, import_react5.useMemo)(() => {
|
|
164
|
+
if (deferredResult) {
|
|
165
|
+
writePreloadedQueryToCache(deferredResult);
|
|
166
|
+
}
|
|
167
|
+
}, [deferredResult]);
|
|
168
|
+
let ref = "queryRef" in loaderData ? loaderData.queryRef : deferredResult ? {
|
|
169
|
+
environment,
|
|
170
|
+
fetchKey: `${deferredResult.params.id ?? deferredResult.params.cacheID}${JSON.stringify(deferredResult.response).split("").reduce((a, b) => {
|
|
171
|
+
a = (a << 5) - a + b.charCodeAt(0);
|
|
172
|
+
return a & a;
|
|
173
|
+
}, 0)}`,
|
|
174
|
+
fetchPolicy,
|
|
175
|
+
isDisposed: false,
|
|
176
|
+
name: deferredResult.params.name,
|
|
177
|
+
kind: "PreloadedQuery",
|
|
178
|
+
variables: deferredResult.variables,
|
|
179
|
+
dispose: () => {
|
|
180
|
+
}
|
|
181
|
+
} : null;
|
|
182
|
+
(0, import_tiny_invariant.default)(ref);
|
|
183
|
+
const [queryRef, loadQuery2, disposeQuery] = useQueryLoader(query);
|
|
184
|
+
if (queryRef)
|
|
185
|
+
ref = queryRef;
|
|
186
|
+
const reloadQuery = (variables, options = { fetchPolicy: "store-and-network" }) => loadQuery2(variables, options);
|
|
187
|
+
const data = usePreloadedQuery(query, ref);
|
|
188
|
+
return [data, reloadQuery, disposeQuery];
|
|
189
|
+
}
|
|
190
|
+
function writePreloadedQueryToCache(preloadedQueryObject) {
|
|
191
|
+
let cacheKey = preloadedQueryObject.params.id ?? preloadedQueryObject.params.cacheID;
|
|
192
|
+
responseCache?.set(
|
|
193
|
+
cacheKey,
|
|
194
|
+
preloadedQueryObject.variables,
|
|
195
|
+
preloadedQueryObject.response
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
199
|
+
0 && (module.exports = {
|
|
200
|
+
RemixRelayProvider,
|
|
201
|
+
Suspense,
|
|
202
|
+
clientLoaderQuery,
|
|
203
|
+
getCachedResponse,
|
|
204
|
+
getClientLoaderQuery,
|
|
205
|
+
metaQuery,
|
|
206
|
+
useLoaderQuery
|
|
207
|
+
});
|
|
208
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/Suspense.tsx","../src/deferred-query-context.tsx","../src/client-loader-query.ts","../src/get-cached-response.ts","../src/meta-query.ts","../src/useLoaderQuery.ts"],"sourcesContent":["export { Suspense } from \"./Suspense\";\nexport { clientLoaderQuery, getClientLoaderQuery } from \"./client-loader-query\";\nexport { RemixRelayProvider } from \"./deferred-query-context\";\nexport { getCachedResponse } from \"./get-cached-response\";\nexport { metaQuery } from \"./meta-query\";\nexport { useLoaderQuery } from \"./useLoaderQuery\";\n","import { Await } from \"@remix-run/react\";\nimport {\n Suspense as ReactSuspense,\n SuspenseProps,\n useContext,\n useEffect,\n useState,\n} from \"react\";\nimport { DeferredQueryContext } from \"./deferred-query-context\";\n\nexport function Suspense({ children, ...rest }: SuspenseProps) {\n const [mounted, setMounted] = useState(false);\n\n useEffect(() => {\n if (!mounted && typeof window !== \"undefined\") {\n setMounted(true);\n }\n }, [mounted]);\n\n const deferredQuery = useContext(DeferredQueryContext);\n\n return mounted ? (\n <ReactSuspense {...rest}>\n {deferredQuery ? (\n <Await resolve={deferredQuery}>{children}</Await>\n ) : (\n children\n )}\n </ReactSuspense>\n ) : (\n <>{rest.fallback}</>\n );\n}\n","import type { Dispatch, PropsWithChildren, SetStateAction } from \"react\";\nimport { createContext, useState } from \"react\";\n\nexport const DeferredQueryContext = createContext<Promise<any> | null>(null);\nexport const SetDeferredQueryContext = createContext<\n Dispatch<SetStateAction<Promise<any> | null>>\n>(() => {});\n\nexport function RemixRelayProvider({ children }: PropsWithChildren) {\n const [deferredQueries, setDeferredQueries] = useState<Promise<any> | null>(\n null,\n );\n\n return (\n <SetDeferredQueryContext.Provider value={setDeferredQueries}>\n <DeferredQueryContext.Provider value={deferredQueries}>\n {children}\n </DeferredQueryContext.Provider>\n </SetDeferredQueryContext.Provider>\n );\n}\n","import relay from \"react-relay\";\nimport type {\n Environment,\n GraphQLTaggedNode,\n OperationType,\n VariablesOf,\n} from \"relay-runtime\";\n\nconst { fetchQuery, loadQuery } = relay;\n\nexport function getClientLoaderQuery(environment: Environment) {\n return async <TQuery extends OperationType>(\n query: GraphQLTaggedNode,\n variables: VariablesOf<TQuery>,\n ) => clientLoaderQuery(environment, query, variables);\n}\n\nexport async function clientLoaderQuery<TQuery extends OperationType>(\n environment: Environment,\n query: GraphQLTaggedNode,\n variables: VariablesOf<TQuery>,\n) {\n const data = await fetchQuery<TQuery>(environment, query, variables, {\n fetchPolicy: \"store-or-network\",\n }).toPromise();\n\n const queryRef = loadQuery<TQuery>(environment, query, variables, {\n fetchPolicy: \"store-only\",\n });\n\n return { queryRef, data };\n}\n","import type {\n CacheConfig,\n GraphQLResponse,\n RequestParameters,\n Variables,\n} from \"relay-runtime\";\nimport { Observable, QueryResponseCache } from \"relay-runtime\";\n\nexport const responseCache: QueryResponseCache = new QueryResponseCache({\n size: 100,\n ttl: 5000,\n});\n\nexport function getCachedResponse(\n params: RequestParameters,\n variables: Variables,\n cacheConfig: CacheConfig,\n) {\n const isQuery = params.operationKind === \"query\";\n const cacheKey = params.id ?? params.cacheID;\n const forceFetch = cacheConfig && cacheConfig.force;\n\n if (responseCache === null || !isQuery || forceFetch) {\n return null;\n }\n\n const fromCache = responseCache.get(cacheKey, variables);\n\n const deferredFromCache: GraphQLResponse[] = [];\n\n for (let i = 0; ; i++) {\n const deferred = responseCache.get(`${cacheKey}-${i}`, variables);\n if (!deferred) break;\n deferredFromCache.push(deferred);\n }\n\n if (fromCache === null) {\n return null;\n }\n\n return Observable.create((sink) => {\n sink.next(fromCache);\n\n for (const deferred of deferredFromCache) {\n sink.next(deferred);\n }\n\n sink.complete();\n });\n}\n","/* eslint-disable no-unused-vars */\nimport type { ServerRuntimeMetaFunction } from \"@remix-run/server-runtime\";\nimport type { OperationType } from \"relay-runtime\";\n\nexport function metaQuery<TQuery extends OperationType>(\n metaFunction: (\n args: Parameters<ServerRuntimeMetaFunction>[0] & {\n data: TQuery[\"response\"];\n },\n ) => ReturnType<ServerRuntimeMetaFunction>,\n): ServerRuntimeMetaFunction<\n () =>\n | { preloadedQuery: { response: { data: TQuery[\"response\"] } } }\n | { data: TQuery[\"response\"] }\n> {\n return ({ data, ...rest }) => {\n const metaData: TQuery[\"response\"] =\n data && \"data\" in data\n ? data.data\n : data && \"preloadedQuery\" in data\n ? (data.preloadedQuery.response as { data: TQuery[\"response\"] }).data\n : null;\n\n return metaFunction({\n data: metaData,\n ...rest,\n });\n };\n}\n","import type { TypedDeferredData } from \"@remix-run/node\";\nimport { useLoaderData } from \"@remix-run/react\";\nimport { useContext, useEffect, useMemo, useState } from \"react\";\nimport { flushSync } from \"react-dom\";\nimport type {\n GraphQLTaggedNode,\n PreloadFetchPolicy,\n PreloadedQuery,\n} from \"react-relay\";\nimport relay from \"react-relay\";\nimport type { useQueryLoaderHookType } from \"react-relay/relay-hooks/useQueryLoader\";\nimport type {\n ConcreteRequest,\n GraphQLResponse,\n OperationType,\n VariablesOf,\n} from \"relay-runtime\";\nimport invariant from \"tiny-invariant\";\nimport { SetDeferredQueryContext } from \"./deferred-query-context\";\nimport { responseCache } from \"./get-cached-response\";\n\nconst { usePreloadedQuery, useQueryLoader, useRelayEnvironment } = relay;\n\nexport type SerializablePreloadedQuery<TQuery extends OperationType> = {\n params: ConcreteRequest[\"params\"];\n variables: VariablesOf<TQuery>;\n response: GraphQLResponse;\n};\n\nexport function useLoaderQuery<TQuery extends OperationType>(\n query: GraphQLTaggedNode,\n fetchPolicy: PreloadFetchPolicy = \"network-only\",\n): [\n TQuery[\"response\"],\n useQueryLoaderHookType<TQuery>[1],\n useQueryLoaderHookType<TQuery>[2],\n] {\n const loaderData = useLoaderData<\n () => Promise<\n | TypedDeferredData<{\n preloadedQuery: SerializablePreloadedQuery<TQuery>;\n deferredQueries: Promise<SerializablePreloadedQuery<TQuery>[]>;\n }>\n | { queryRef: PreloadedQuery<TQuery> }\n >\n >();\n\n const preloadedQuery =\n \"preloadedQuery\" in loaderData\n ? (loaderData.preloadedQuery as unknown as SerializablePreloadedQuery<TQuery>)\n : null;\n\n const deferredQueries =\n \"deferredQueries\" in loaderData\n ? (loaderData.deferredQueries as unknown as Promise<\n SerializablePreloadedQuery<TQuery>[]\n >)\n : null;\n\n const [deferredResult, setDeferredResult] = useState(preloadedQuery);\n\n const setDeferredQueries = useContext(SetDeferredQueryContext);\n\n useEffect(() => {\n if (deferredQueries) {\n setDeferredQueries(\n deferredQueries.then(async (deferredResults) => {\n deferredResults.forEach((result) => {\n flushSync(() => {\n setDeferredResult(\n result as unknown as SerializablePreloadedQuery<TQuery>,\n );\n });\n });\n\n return deferredResults;\n }),\n );\n }\n }, [deferredQueries, preloadedQuery, setDeferredQueries, setDeferredResult]);\n\n const environment = useRelayEnvironment();\n\n useMemo(() => {\n if (deferredResult) {\n writePreloadedQueryToCache(deferredResult);\n }\n }, [deferredResult]);\n\n let ref: PreloadedQuery<TQuery> | null =\n \"queryRef\" in loaderData\n ? (loaderData.queryRef as unknown as PreloadedQuery<TQuery>)\n : deferredResult\n ? {\n environment,\n fetchKey: `${\n deferredResult.params.id ?? deferredResult.params.cacheID\n }${JSON.stringify(deferredResult.response)\n .split(\"\")\n .reduce((a, b) => {\n a = (a << 5) - a + b.charCodeAt(0);\n return a & a;\n }, 0)}`,\n fetchPolicy,\n isDisposed: false,\n name: deferredResult.params.name,\n kind: \"PreloadedQuery\",\n variables: deferredResult.variables,\n dispose: () => {},\n }\n : null;\n\n invariant(ref);\n\n const [queryRef, loadQuery, disposeQuery] = useQueryLoader<TQuery>(query);\n\n if (queryRef) ref = queryRef;\n\n const reloadQuery: typeof loadQuery = (\n variables,\n options = { fetchPolicy: \"store-and-network\" },\n ) => loadQuery(variables, options);\n\n const data = usePreloadedQuery<TQuery>(query, ref);\n\n return [data, reloadQuery, disposeQuery];\n}\n\nfunction writePreloadedQueryToCache<TQuery extends OperationType>(\n preloadedQueryObject: SerializablePreloadedQuery<TQuery>,\n) {\n let cacheKey =\n preloadedQueryObject.params.id ?? preloadedQueryObject.params.cacheID;\n\n responseCache?.set(\n cacheKey,\n preloadedQueryObject.variables,\n preloadedQueryObject.response,\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,gBAAsB;AACtB,IAAAA,gBAMO;;;ACNP,mBAAwC;AAclC;AAZC,IAAM,2BAAuB,4BAAmC,IAAI;AACpE,IAAM,8BAA0B,4BAErC,MAAM;AAAC,CAAC;AAEH,SAAS,mBAAmB,EAAE,SAAS,GAAsB;AAClE,QAAM,CAAC,iBAAiB,kBAAkB,QAAI;AAAA,IAC5C;AAAA,EACF;AAEA,SACE,4CAAC,wBAAwB,UAAxB,EAAiC,OAAO,oBACvC,sDAAC,qBAAqB,UAArB,EAA8B,OAAO,iBACnC,UACH,GACF;AAEJ;;;ADIQ,IAAAC,sBAAA;AAdD,SAAS,SAAS,EAAE,UAAU,GAAG,KAAK,GAAkB;AAC7D,QAAM,CAAC,SAAS,UAAU,QAAI,wBAAS,KAAK;AAE5C,+BAAU,MAAM;AACd,QAAI,CAAC,WAAW,OAAO,WAAW,aAAa;AAC7C,iBAAW,IAAI;AAAA,IACjB;AAAA,EACF,GAAG,CAAC,OAAO,CAAC;AAEZ,QAAM,oBAAgB,0BAAW,oBAAoB;AAErD,SAAO,UACL,6CAAC,cAAAC,UAAA,EAAe,GAAG,MAChB,0BACC,6CAAC,uBAAM,SAAS,eAAgB,UAAS,IAEzC,UAEJ,IAEA,6EAAG,eAAK,UAAS;AAErB;;;AEhCA,yBAAkB;AAQlB,IAAM,EAAE,YAAY,UAAU,IAAI,mBAAAC;AAE3B,SAAS,qBAAqB,aAA0B;AAC7D,SAAO,OACL,OACA,cACG,kBAAkB,aAAa,OAAO,SAAS;AACtD;AAEA,eAAsB,kBACpB,aACA,OACA,WACA;A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1B,SAAS,MAAM;AAAA,IAAC;AAAA,EAClB,IACA;AAER,4BAAAC,SAAU,GAAG;AAEb,QAAM,CAAC,UAAUC,YAAW,YAAY,IAAI,eAAuB,KAAK;AAExE,MAAI;AAAU,UAAM;AAEpB,QAAM,cAAgC,CACpC,WACA,UAAU,EAAE,aAAa,oBAAoB,MAC1CA,WAAU,WAAW,OAAO;AAEjC,QAAM,OAAO,kBAA0B,OAAO,GAAG;AAEjD,SAAO,CAAC,MAAM,aAAa,YAAY;AACzC;AAEA,SAAS,2BACP,sBACA;AACA,MAAI,WACF,qBAAqB,OAAO,MAAM,qBAAqB,OAAO;AAEhE,iBAAe;AAAA,IACb;AAAA,IACA,qBAAqB;AAAA,IACrB,qBAAqB;AAAA,EACvB;AACF;","names":["import_react","import_jsx_runtime","ReactSuspense","relay","import_react","import_react_relay","relay","invariant","loadQuery"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
// src/Suspense.tsx
|
|
2
|
+
import { Await } from "@remix-run/react";
|
|
3
|
+
import {
|
|
4
|
+
Suspense as ReactSuspense,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useState as useState2
|
|
8
|
+
} from "react";
|
|
9
|
+
|
|
10
|
+
// src/deferred-query-context.tsx
|
|
11
|
+
import { createContext, useState } from "react";
|
|
12
|
+
import { jsx } from "react/jsx-runtime";
|
|
13
|
+
var DeferredQueryContext = createContext(null);
|
|
14
|
+
var SetDeferredQueryContext = createContext(() => {
|
|
15
|
+
});
|
|
16
|
+
function RemixRelayProvider({ children }) {
|
|
17
|
+
const [deferredQueries, setDeferredQueries] = useState(
|
|
18
|
+
null
|
|
19
|
+
);
|
|
20
|
+
return /* @__PURE__ */ jsx(SetDeferredQueryContext.Provider, { value: setDeferredQueries, children: /* @__PURE__ */ jsx(DeferredQueryContext.Provider, { value: deferredQueries, children }) });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// src/Suspense.tsx
|
|
24
|
+
import { Fragment, jsx as jsx2 } from "react/jsx-runtime";
|
|
25
|
+
function Suspense({ children, ...rest }) {
|
|
26
|
+
const [mounted, setMounted] = useState2(false);
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!mounted && typeof window !== "undefined") {
|
|
29
|
+
setMounted(true);
|
|
30
|
+
}
|
|
31
|
+
}, [mounted]);
|
|
32
|
+
const deferredQuery = useContext(DeferredQueryContext);
|
|
33
|
+
return mounted ? /* @__PURE__ */ jsx2(ReactSuspense, { ...rest, children: deferredQuery ? /* @__PURE__ */ jsx2(Await, { resolve: deferredQuery, children }) : children }) : /* @__PURE__ */ jsx2(Fragment, { children: rest.fallback });
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/client-loader-query.ts
|
|
37
|
+
import relay from "react-relay";
|
|
38
|
+
var { fetchQuery, loadQuery } = relay;
|
|
39
|
+
function getClientLoaderQuery(environment) {
|
|
40
|
+
return async (query, variables) => clientLoaderQuery(environment, query, variables);
|
|
41
|
+
}
|
|
42
|
+
async function clientLoaderQuery(environment, query, variables) {
|
|
43
|
+
const data = await fetchQuery(environment, query, variables, {
|
|
44
|
+
fetchPolicy: "store-or-network"
|
|
45
|
+
}).toPromise();
|
|
46
|
+
const queryRef = loadQuery(environment, query, variables, {
|
|
47
|
+
fetchPolicy: "store-only"
|
|
48
|
+
});
|
|
49
|
+
return { queryRef, data };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/get-cached-response.ts
|
|
53
|
+
import { Observable, QueryResponseCache } from "relay-runtime";
|
|
54
|
+
var responseCache = new QueryResponseCache({
|
|
55
|
+
size: 100,
|
|
56
|
+
ttl: 5e3
|
|
57
|
+
});
|
|
58
|
+
function getCachedResponse(params, variables, cacheConfig) {
|
|
59
|
+
const isQuery = params.operationKind === "query";
|
|
60
|
+
const cacheKey = params.id ?? params.cacheID;
|
|
61
|
+
const forceFetch = cacheConfig && cacheConfig.force;
|
|
62
|
+
if (responseCache === null || !isQuery || forceFetch) {
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
const fromCache = responseCache.get(cacheKey, variables);
|
|
66
|
+
const deferredFromCache = [];
|
|
67
|
+
for (let i = 0; ; i++) {
|
|
68
|
+
const deferred = responseCache.get(`${cacheKey}-${i}`, variables);
|
|
69
|
+
if (!deferred)
|
|
70
|
+
break;
|
|
71
|
+
deferredFromCache.push(deferred);
|
|
72
|
+
}
|
|
73
|
+
if (fromCache === null) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
return Observable.create((sink) => {
|
|
77
|
+
sink.next(fromCache);
|
|
78
|
+
for (const deferred of deferredFromCache) {
|
|
79
|
+
sink.next(deferred);
|
|
80
|
+
}
|
|
81
|
+
sink.complete();
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/meta-query.ts
|
|
86
|
+
function metaQuery(metaFunction) {
|
|
87
|
+
return ({ data, ...rest }) => {
|
|
88
|
+
const metaData = data && "data" in data ? data.data : data && "preloadedQuery" in data ? data.preloadedQuery.response.data : null;
|
|
89
|
+
return metaFunction({
|
|
90
|
+
data: metaData,
|
|
91
|
+
...rest
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/useLoaderQuery.ts
|
|
97
|
+
import { useLoaderData } from "@remix-run/react";
|
|
98
|
+
import { useContext as useContext2, useEffect as useEffect2, useMemo, useState as useState3 } from "react";
|
|
99
|
+
import { flushSync } from "react-dom";
|
|
100
|
+
import relay2 from "react-relay";
|
|
101
|
+
import invariant from "tiny-invariant";
|
|
102
|
+
var { usePreloadedQuery, useQueryLoader, useRelayEnvironment } = relay2;
|
|
103
|
+
function useLoaderQuery(query, fetchPolicy = "network-only") {
|
|
104
|
+
const loaderData = useLoaderData();
|
|
105
|
+
const preloadedQuery = "preloadedQuery" in loaderData ? loaderData.preloadedQuery : null;
|
|
106
|
+
const deferredQueries = "deferredQueries" in loaderData ? loaderData.deferredQueries : null;
|
|
107
|
+
const [deferredResult, setDeferredResult] = useState3(preloadedQuery);
|
|
108
|
+
const setDeferredQueries = useContext2(SetDeferredQueryContext);
|
|
109
|
+
useEffect2(() => {
|
|
110
|
+
if (deferredQueries) {
|
|
111
|
+
setDeferredQueries(
|
|
112
|
+
deferredQueries.then(async (deferredResults) => {
|
|
113
|
+
deferredResults.forEach((result) => {
|
|
114
|
+
flushSync(() => {
|
|
115
|
+
setDeferredResult(
|
|
116
|
+
result
|
|
117
|
+
);
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
return deferredResults;
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
}, [deferredQueries, preloadedQuery, setDeferredQueries, setDeferredResult]);
|
|
125
|
+
const environment = useRelayEnvironment();
|
|
126
|
+
useMemo(() => {
|
|
127
|
+
if (deferredResult) {
|
|
128
|
+
writePreloadedQueryToCache(deferredResult);
|
|
129
|
+
}
|
|
130
|
+
}, [deferredResult]);
|
|
131
|
+
let ref = "queryRef" in loaderData ? loaderData.queryRef : deferredResult ? {
|
|
132
|
+
environment,
|
|
133
|
+
fetchKey: `${deferredResult.params.id ?? deferredResult.params.cacheID}${JSON.stringify(deferredResult.response).split("").reduce((a, b) => {
|
|
134
|
+
a = (a << 5) - a + b.charCodeAt(0);
|
|
135
|
+
return a & a;
|
|
136
|
+
}, 0)}`,
|
|
137
|
+
fetchPolicy,
|
|
138
|
+
isDisposed: false,
|
|
139
|
+
name: deferredResult.params.name,
|
|
140
|
+
kind: "PreloadedQuery",
|
|
141
|
+
variables: deferredResult.variables,
|
|
142
|
+
dispose: () => {
|
|
143
|
+
}
|
|
144
|
+
} : null;
|
|
145
|
+
invariant(ref);
|
|
146
|
+
const [queryRef, loadQuery2, disposeQuery] = useQueryLoader(query);
|
|
147
|
+
if (queryRef)
|
|
148
|
+
ref = queryRef;
|
|
149
|
+
const reloadQuery = (variables, options = { fetchPolicy: "store-and-network" }) => loadQuery2(variables, options);
|
|
150
|
+
const data = usePreloadedQuery(query, ref);
|
|
151
|
+
return [data, reloadQuery, disposeQuery];
|
|
152
|
+
}
|
|
153
|
+
function writePreloadedQueryToCache(preloadedQueryObject) {
|
|
154
|
+
let cacheKey = preloadedQueryObject.params.id ?? preloadedQueryObject.params.cacheID;
|
|
155
|
+
responseCache?.set(
|
|
156
|
+
cacheKey,
|
|
157
|
+
preloadedQueryObject.variables,
|
|
158
|
+
preloadedQueryObject.response
|
|
159
|
+
);
|
|
160
|
+
}
|
|
161
|
+
export {
|
|
162
|
+
RemixRelayProvider,
|
|
163
|
+
Suspense,
|
|
164
|
+
clientLoaderQuery,
|
|
165
|
+
getCachedResponse,
|
|
166
|
+
getClientLoaderQuery,
|
|
167
|
+
metaQuery,
|
|
168
|
+
useLoaderQuery
|
|
169
|
+
};
|
|
170
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/Suspense.tsx","../src/deferred-query-context.tsx","../src/client-loader-query.ts","../src/get-cached-response.ts","../src/meta-query.ts","../src/useLoaderQuery.ts"],"sourcesContent":["import { Await } from \"@remix-run/react\";\nimport {\n Suspense as ReactSuspense,\n SuspenseProps,\n useContext,\n useEffect,\n useState,\n} from \"react\";\nimport { DeferredQueryContext } from \"./deferred-query-context\";\n\nexport function Suspense({ children, ...rest }: SuspenseProps) {\n const [mounted, setMounted] = useState(false);\n\n useEffect(() => {\n if (!mounted && typeof window !== \"undefined\") {\n setMounted(true);\n }\n }, [mounted]);\n\n const deferredQuery = useContext(DeferredQueryContext);\n\n return mounted ? (\n <ReactSuspense {...rest}>\n {deferredQuery ? (\n <Await resolve={deferredQuery}>{children}</Await>\n ) : (\n children\n )}\n </ReactSuspense>\n ) : (\n <>{rest.fallback}</>\n );\n}\n","import type { Dispatch, PropsWithChildren, SetStateAction } from \"react\";\nimport { createContext, useState } from \"react\";\n\nexport const DeferredQueryContext = createContext<Promise<any> | null>(null);\nexport const SetDeferredQueryContext = createContext<\n Dispatch<SetStateAction<Promise<any> | null>>\n>(() => {});\n\nexport function RemixRelayProvider({ children }: PropsWithChildren) {\n const [deferredQueries, setDeferredQueries] = useState<Promise<any> | null>(\n null,\n );\n\n return (\n <SetDeferredQueryContext.Provider value={setDeferredQueries}>\n <DeferredQueryContext.Provider value={deferredQueries}>\n {children}\n </DeferredQueryContext.Provider>\n </SetDeferredQueryContext.Provider>\n );\n}\n","import relay from \"react-relay\";\nimport type {\n Environment,\n GraphQLTaggedNode,\n OperationType,\n VariablesOf,\n} from \"relay-runtime\";\n\nconst { fetchQuery, loadQuery } = relay;\n\nexport function getClientLoaderQuery(environment: Environment) {\n return async <TQuery extends OperationType>(\n query: GraphQLTaggedNode,\n variables: VariablesOf<TQuery>,\n ) => clientLoaderQuery(environment, query, variables);\n}\n\nexport async function clientLoaderQuery<TQuery extends OperationType>(\n environment: Environment,\n query: GraphQLTaggedNode,\n variables: VariablesOf<TQuery>,\n) {\n const data = await fetchQuery<TQuery>(environment, query, variables, {\n fetchPolicy: \"store-or-network\",\n }).toPromise();\n\n const queryRef = loadQuery<TQuery>(environment, query, variables, {\n fetchPolicy: \"store-only\",\n });\n\n return { queryRef, data };\n}\n","import type {\n CacheConfig,\n GraphQLResponse,\n RequestParameters,\n Variables,\n} from \"relay-runtime\";\nimport { Observable, QueryResponseCache } from \"relay-runtime\";\n\nexport const responseCache: QueryResponseCache = new QueryResponseCache({\n size: 100,\n ttl: 5000,\n});\n\nexport function getCachedResponse(\n params: RequestParameters,\n variables: Variables,\n cacheConfig: CacheConfig,\n) {\n const isQuery = params.operationKind === \"query\";\n const cacheKey = params.id ?? params.cacheID;\n const forceFetch = cacheConfig && cacheConfig.force;\n\n if (responseCache === null || !isQuery || forceFetch) {\n return null;\n }\n\n const fromCache = responseCache.get(cacheKey, variables);\n\n const deferredFromCache: GraphQLResponse[] = [];\n\n for (let i = 0; ; i++) {\n const deferred = responseCache.get(`${cacheKey}-${i}`, variables);\n if (!deferred) break;\n deferredFromCache.push(deferred);\n }\n\n if (fromCache === null) {\n return null;\n }\n\n return Observable.create((sink) => {\n sink.next(fromCache);\n\n for (const deferred of deferredFromCache) {\n sink.next(deferred);\n }\n\n sink.complete();\n });\n}\n","/* eslint-disable no-unused-vars */\nimport type { ServerRuntimeMetaFunction } from \"@remix-run/server-runtime\";\nimport type { OperationType } from \"relay-runtime\";\n\nexport function metaQuery<TQuery extends OperationType>(\n metaFunction: (\n args: Parameters<ServerRuntimeMetaFunction>[0] & {\n data: TQuery[\"response\"];\n },\n ) => ReturnType<ServerRuntimeMetaFunction>,\n): ServerRuntimeMetaFunction<\n () =>\n | { preloadedQuery: { response: { data: TQuery[\"response\"] } } }\n | { data: TQuery[\"response\"] }\n> {\n return ({ data, ...rest }) => {\n const metaData: TQuery[\"response\"] =\n data && \"data\" in data\n ? data.data\n : data && \"preloadedQuery\" in data\n ? (data.preloadedQuery.response as { data: TQuery[\"response\"] }).data\n : null;\n\n return metaFunction({\n data: metaData,\n ...rest,\n });\n };\n}\n","import type { TypedDeferredData } from \"@remix-run/node\";\nimport { useLoaderData } from \"@remix-run/react\";\nimport { useContext, useEffect, useMemo, useState } from \"react\";\nimport { flushSync } from \"react-dom\";\nimport type {\n GraphQLTaggedNode,\n PreloadFetchPolicy,\n PreloadedQuery,\n} from \"react-relay\";\nimport relay from \"react-relay\";\nimport type { useQueryLoaderHookType } from \"react-relay/relay-hooks/useQueryLoader\";\nimport type {\n ConcreteRequest,\n GraphQLResponse,\n OperationType,\n VariablesOf,\n} from \"relay-runtime\";\nimport invariant from \"tiny-invariant\";\nimport { SetDeferredQueryContext } from \"./deferred-query-context\";\nimport { responseCache } from \"./get-cached-response\";\n\nconst { usePreloadedQuery, useQueryLoader, useRelayEnvironment } = relay;\n\nexport type SerializablePreloadedQuery<TQuery extends OperationType> = {\n params: ConcreteRequest[\"params\"];\n variables: VariablesOf<TQuery>;\n response: GraphQLResponse;\n};\n\nexport function useLoaderQuery<TQuery extends OperationType>(\n query: GraphQLTaggedNode,\n fetchPolicy: PreloadFetchPolicy = \"network-only\",\n): [\n TQuery[\"response\"],\n useQueryLoaderHookType<TQuery>[1],\n useQueryLoaderHookType<TQuery>[2],\n] {\n const loaderData = useLoaderData<\n () => Promise<\n | TypedDeferredData<{\n preloadedQuery: SerializablePreloadedQuery<TQuery>;\n deferredQueries: Promise<SerializablePreloadedQuery<TQuery>[]>;\n }>\n | { queryRef: PreloadedQuery<TQuery> }\n >\n >();\n\n const preloadedQuery =\n \"preloadedQuery\" in loaderData\n ? (loaderData.preloadedQuery as unknown as SerializablePreloadedQuery<TQuery>)\n : null;\n\n const deferredQueries =\n \"deferredQueries\" in loaderData\n ? (loaderData.deferredQueries as unknown as Promise<\n SerializablePreloadedQuery<TQuery>[]\n >)\n : null;\n\n const [deferredResult, setDeferredResult] = useState(preloadedQuery);\n\n const setDeferredQueries = useContext(SetDeferredQueryContext);\n\n useEffect(() => {\n if (deferredQueries) {\n setDeferredQueries(\n deferredQueries.then(async (deferredResults) => {\n deferredResults.forEach((result) => {\n flushSync(() => {\n setDeferredResult(\n result as unknown as SerializablePreloadedQuery<TQuery>,\n );\n });\n });\n\n return deferredResults;\n }),\n );\n }\n }, [deferredQueries, preloadedQuery, setDeferredQueries, setDeferredResult]);\n\n const environment = useRelayEnvironment();\n\n useMemo(() => {\n if (deferredResult) {\n writePreloadedQueryToCache(deferredResult);\n }\n }, [deferredResult]);\n\n let ref: PreloadedQuery<TQuery> | null =\n \"queryRef\" in loaderData\n ? (loaderData.queryRef as unknown as PreloadedQuery<TQuery>)\n : deferredResult\n ? {\n environment,\n fetchKey: `${\n deferredResult.params.id ?? deferredResult.params.cacheID\n }${JSON.stringify(deferredResult.response)\n .split(\"\")\n .reduce((a, b) => {\n a = (a << 5) - a + b.charCodeAt(0);\n return a & a;\n }, 0)}`,\n fetchPolicy,\n isDisposed: false,\n name: deferredResult.params.name,\n kind: \"PreloadedQuery\",\n variables: deferredResult.variables,\n dispose: () => {},\n }\n : null;\n\n invariant(ref);\n\n const [queryRef, loadQuery, disposeQuery] = useQueryLoader<TQuery>(query);\n\n if (queryRef) ref = queryRef;\n\n const reloadQuery: typeof loadQuery = (\n variables,\n options = { fetchPolicy: \"store-and-network\" },\n ) => loadQuery(variables, options);\n\n const data = usePreloadedQuery<TQuery>(query, ref);\n\n return [data, reloadQuery, disposeQuery];\n}\n\nfunction writePreloadedQueryToCache<TQuery extends OperationType>(\n preloadedQueryObject: SerializablePreloadedQuery<TQuery>,\n) {\n let cacheKey =\n preloadedQueryObject.params.id ?? preloadedQueryObject.params.cacheID;\n\n responseCache?.set(\n cacheKey,\n preloadedQueryObject.variables,\n preloadedQueryObject.response,\n );\n}\n"],"mappings":";AAAA,SAAS,aAAa;AACtB;AAAA,EACE,YAAY;AAAA,EAEZ;AAAA,EACA;AAAA,EACA,YAAAA;AAAA,OACK;;;ACNP,SAAS,eAAe,gBAAgB;AAclC;AAZC,IAAM,uBAAuB,cAAmC,IAAI;AACpE,IAAM,0BAA0B,cAErC,MAAM;AAAC,CAAC;AAEH,SAAS,mBAAmB,EAAE,SAAS,GAAsB;AAClE,QAAM,CAAC,iBAAiB,kBAAkB,IAAI;AAAA,IAC5C;AAAA,EACF;AAEA,SACE,oBAAC,wBAAwB,UAAxB,EAAiC,OAAO,oBACvC,8BAAC,qBAAqB,UAArB,EAA8B,OAAO,iBACnC,UACH,GACF;AAEJ;;;ADIQ,SAMJ,UANI,OAAAC,YAAA;AAdD,SAAS,SAAS,EAAE,UAAU,GAAG,KAAK,GAAkB;AAC7D,QAAM,CAAC,SAAS,UAAU,IAAIC,UAAS,KAAK;AAE5C,YAAU,MAAM;AACd,QAAI,CAAC,WAAW,OAAO,WAAW,aAAa;AAC7C,iBAAW,IAAI;AAAA,IACjB;AAAA,EACF,GAAG,CAAC,OAAO,CAAC;AAEZ,QAAM,gBAAgB,WAAW,oBAAoB;AAErD,SAAO,UACL,gBAAAD,KAAC,iBAAe,GAAG,MAChB,0BACC,gBAA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AAU,GACR,eAAe,OAAO,MAAM,eAAe,OAAO,OACpD,GAAG,KAAK,UAAU,eAAe,QAAQ,EACtC,MAAM,EAAE,EACR,OAAO,CAAC,GAAG,MAAM;AAChB,WAAK,KAAK,KAAK,IAAI,EAAE,WAAW,CAAC;AACjC,aAAO,IAAI;AAAA,IACb,GAAG,CAAC,CAAC;AAAA,IACP;AAAA,IACA,YAAY;AAAA,IACZ,MAAM,eAAe,OAAO;AAAA,IAC5B,MAAM;AAAA,IACN,WAAW,eAAe;AAAA,IAC1B,SAAS,MAAM;AAAA,IAAC;AAAA,EAClB,IACA;AAER,YAAU,GAAG;AAEb,QAAM,CAAC,UAAUC,YAAW,YAAY,IAAI,eAAuB,KAAK;AAExE,MAAI;AAAU,UAAM;AAEpB,QAAM,cAAgC,CACpC,WACA,UAAU,EAAE,aAAa,oBAAoB,MAC1CA,WAAU,WAAW,OAAO;AAEjC,QAAM,OAAO,kBAA0B,OAAO,GAAG;AAEjD,SAAO,CAAC,MAAM,aAAa,YAAY;AACzC;AAEA,SAAS,2BACP,sBACA;AACA,MAAI,WACF,qBAAqB,OAAO,MAAM,qBAAqB,OAAO;AAEhE,iBAAe;AAAA,IACb;AAAA,IACA,qBAAqB;AAAA,IACrB,qBAAqB;AAAA,EACvB;AACF;","names":["useState","jsx","useState","useContext","useEffect","useState","relay","relay","useState","useContext","useEffect","loadQuery"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@remix-relay/react",
|
|
3
|
+
"description": "Provides Relay integration with Remix",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Dan Train",
|
|
7
|
+
"url": "https://github.com/dantrain"
|
|
8
|
+
},
|
|
9
|
+
"keywords": [
|
|
10
|
+
"Remix",
|
|
11
|
+
"Relay",
|
|
12
|
+
"React"
|
|
13
|
+
],
|
|
14
|
+
"homepage": "https://github.com/dantrain/remix-relay#readme",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/dantrain/remix-relay.git"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"type": "commonjs",
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"module": "./dist/index.mjs",
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"module": "./dist/index.mjs",
|
|
27
|
+
"import": {
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"default": "./dist/index.mjs"
|
|
30
|
+
},
|
|
31
|
+
"require": {
|
|
32
|
+
"types": "./dist/index.d.ts",
|
|
33
|
+
"default": "./dist/index.js"
|
|
34
|
+
},
|
|
35
|
+
"default": "./dist/index.js"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"files": [
|
|
40
|
+
"dist/**"
|
|
41
|
+
],
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"@remix-run/react": "^2.4.1",
|
|
44
|
+
"react": "^18.2.0",
|
|
45
|
+
"react-dom": "^18.2.0",
|
|
46
|
+
"react-relay": "^16.1.0",
|
|
47
|
+
"relay-runtime": "^16.1.0"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"tiny-invariant": "^1.3.3"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@remix-run/node": "^2.8.1",
|
|
54
|
+
"@remix-run/react": "^2.8.1",
|
|
55
|
+
"@remix-run/server-runtime": "^2.8.1",
|
|
56
|
+
"@types/eslint": "^8.56.5",
|
|
57
|
+
"@types/node": "^20.11.25",
|
|
58
|
+
"@types/react": "^18.2.64",
|
|
59
|
+
"@types/react-dom": "^18.2.21",
|
|
60
|
+
"@types/react-relay": "^16.0.6",
|
|
61
|
+
"@types/relay-runtime": "^14.1.23",
|
|
62
|
+
"eslint": "^8.57.0",
|
|
63
|
+
"graphql": "17.0.0-alpha.2",
|
|
64
|
+
"react": "^18.2.0",
|
|
65
|
+
"react-dom": "^18.2.0",
|
|
66
|
+
"react-relay": "^16.2.0",
|
|
67
|
+
"relay-runtime": "^16.2.0",
|
|
68
|
+
"tsup": "^8.0.2",
|
|
69
|
+
"typescript": "^5.4.2",
|
|
70
|
+
"@remix-relay/eslint-config": "0.0.0",
|
|
71
|
+
"@remix-relay/typescript-config": "0.0.0"
|
|
72
|
+
},
|
|
73
|
+
"scripts": {
|
|
74
|
+
"build": "tsup",
|
|
75
|
+
"dev": "tsup --watch",
|
|
76
|
+
"lint": "eslint . --max-warnings 0",
|
|
77
|
+
"typecheck": "tsc --noEmit"
|
|
78
|
+
}
|
|
79
|
+
}
|