graphql-persisted 0.0.7 → 20.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.
Potentially problematic release.
This version of graphql-persisted might be problematic. Click here for more details.
- package/index.js +46 -0
- package/package.json +9 -76
- package/README.md +0 -54
- package/cjs/GraphQLQueryCache.d.ts +0 -106
- package/cjs/GraphQLQueryCache.js +0 -432
- package/cjs/context.d.ts +0 -21
- package/cjs/context.js +0 -32
- package/cjs/errors.d.ts +0 -19
- package/cjs/errors.js +0 -40
- package/cjs/fragmentData.d.ts +0 -31
- package/cjs/fragmentData.js +0 -16
- package/cjs/graphqlHooks.d.ts +0 -48
- package/cjs/graphqlHooks.js +0 -183
- package/cjs/helpers.d.ts +0 -4
- package/cjs/helpers.js +0 -16
- package/cjs/index.d.ts +0 -8
- package/cjs/index.js +0 -18
- package/cjs/types.d.ts +0 -205
- package/cjs/types.js +0 -2
- package/mjs/GraphQLQueryCache.d.ts +0 -106
- package/mjs/GraphQLQueryCache.js +0 -427
- package/mjs/context.d.ts +0 -21
- package/mjs/context.js +0 -27
- package/mjs/errors.d.ts +0 -19
- package/mjs/errors.js +0 -31
- package/mjs/fragmentData.d.ts +0 -31
- package/mjs/fragmentData.js +0 -10
- package/mjs/graphqlHooks.d.ts +0 -48
- package/mjs/graphqlHooks.js +0 -174
- package/mjs/helpers.d.ts +0 -4
- package/mjs/helpers.js +0 -10
- package/mjs/index.d.ts +0 -8
- package/mjs/index.js +0 -4
- package/mjs/types.d.ts +0 -205
- package/mjs/types.js +0 -1
package/cjs/GraphQLQueryCache.js
DELETED
|
@@ -1,432 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GraphQLQueryCache = void 0;
|
|
4
|
-
const tslib_1 = require("tslib");
|
|
5
|
-
const lodash_isequal_1 = tslib_1.__importDefault(require("lodash.isequal"));
|
|
6
|
-
const graphql_normalize_1 = require("graphql-normalize");
|
|
7
|
-
const immer_1 = require("immer");
|
|
8
|
-
const errors_1 = require("./errors");
|
|
9
|
-
const helpers_1 = require("./helpers");
|
|
10
|
-
/**
|
|
11
|
-
* Manages the cached normalized state, and the execution of
|
|
12
|
-
* data accessed by different components
|
|
13
|
-
*/
|
|
14
|
-
class GraphQLQueryCache {
|
|
15
|
-
constructor(config) {
|
|
16
|
-
// All queries we are "subscribed to", meaning that we React to any changes
|
|
17
|
-
// when there are updates to the cache.
|
|
18
|
-
this._subscribedQueries = {};
|
|
19
|
-
// All "in-flight" fetches, to deduplicate fetches that are already in-flight
|
|
20
|
-
// for an operation
|
|
21
|
-
this._inFlight = {};
|
|
22
|
-
// Keeps track of all the operations that have been issued in the application,
|
|
23
|
-
// as well as the metadata indicating the TTL / polling / invalidation / refetch
|
|
24
|
-
// logic necessary to keep things up to date
|
|
25
|
-
this._runtimeCache = {};
|
|
26
|
-
/**
|
|
27
|
-
* A list of all "effects" that have run, used to keep track of
|
|
28
|
-
*/
|
|
29
|
-
this._effectsIssued = []; // TODO: Include subscriptions
|
|
30
|
-
this._fetcher = config.fetcher;
|
|
31
|
-
this._endpoint = config.endpoint || `/graphql`;
|
|
32
|
-
this._persistedOperations = config.persistedOperations;
|
|
33
|
-
this._persistedDocuments = config.persistedDocuments;
|
|
34
|
-
this._configMeta = unwrapMeta(config.meta);
|
|
35
|
-
this._queryInvalidation = config.queryInvalidation ?? {};
|
|
36
|
-
this._mutationInvalidation = config.mutationInvalidation ?? {};
|
|
37
|
-
this._cacheStore = (0, immer_1.produce)(config.hydratedCache ?? {
|
|
38
|
-
meta: {},
|
|
39
|
-
fields: {},
|
|
40
|
-
operations: {},
|
|
41
|
-
}, noop);
|
|
42
|
-
}
|
|
43
|
-
/**
|
|
44
|
-
* Invalidates a query by name or predicate fn
|
|
45
|
-
*/
|
|
46
|
-
invalidateQuery(toInvalidate) {
|
|
47
|
-
(0, immer_1.produce)(this._runtimeCache, () => {
|
|
48
|
-
//
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Invalidates a query by name or predicate fn
|
|
53
|
-
*/
|
|
54
|
-
refetchQuery(toRefetch) {
|
|
55
|
-
(0, immer_1.produce)(this._runtimeCache, () => {
|
|
56
|
-
//
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
/**
|
|
60
|
-
* JSON.stringify(queryCache) produces the data we need
|
|
61
|
-
* to rehydrate the Client
|
|
62
|
-
*/
|
|
63
|
-
toJSON() {
|
|
64
|
-
const { fields, operations, meta } = this._cacheStore;
|
|
65
|
-
return {
|
|
66
|
-
fields,
|
|
67
|
-
operations: operations,
|
|
68
|
-
meta: meta,
|
|
69
|
-
};
|
|
70
|
-
}
|
|
71
|
-
// /**
|
|
72
|
-
// * Whether we've fetched the query in the cache yet
|
|
73
|
-
// */
|
|
74
|
-
// hasQuery<Q extends keyof GraphQLQuery.QueryRegistry>(
|
|
75
|
-
// queryName: Q,
|
|
76
|
-
// variable: SerializedVariables | GraphQLQuery.OperationVariables[Q]
|
|
77
|
-
// ) {
|
|
78
|
-
// // Object.values(this._cacheStore.operations).find((o) => {
|
|
79
|
-
// // return o.operationType === 'query' && o
|
|
80
|
-
// // })
|
|
81
|
-
// }
|
|
82
|
-
/**
|
|
83
|
-
* Attempts to read a query from the cache, returning undefined
|
|
84
|
-
* if we are unable to for any reason. Used in initial hook execution
|
|
85
|
-
* where we don't want any side-effects, incase we're doing SSR
|
|
86
|
-
*/
|
|
87
|
-
tryReadQuery(args) {
|
|
88
|
-
try {
|
|
89
|
-
return this.readQuery(args);
|
|
90
|
-
}
|
|
91
|
-
catch {
|
|
92
|
-
return undefined;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Reads the query from the cache. Throws an error if we are unable
|
|
97
|
-
* to read the data, due to an incomplete result or lack of operation
|
|
98
|
-
* metadata
|
|
99
|
-
*/
|
|
100
|
-
readQuery(args) {
|
|
101
|
-
const { queryName, variables, options } = args;
|
|
102
|
-
const key = this._getKey(queryName, variables);
|
|
103
|
-
const op = this._cacheStore.operations[key];
|
|
104
|
-
if (op) {
|
|
105
|
-
return op;
|
|
106
|
-
}
|
|
107
|
-
// If we don't have metadata, we can't know how to read the query
|
|
108
|
-
// from the cache
|
|
109
|
-
const meta = this._getMeta(queryName);
|
|
110
|
-
const { result } = (0, graphql_normalize_1.graphqlNormalize)({
|
|
111
|
-
action: 'read',
|
|
112
|
-
variableValues: typeof variables === 'string' ? JSON.parse(variables) : variables,
|
|
113
|
-
meta: meta,
|
|
114
|
-
cache: this._cacheStore.fields,
|
|
115
|
-
isEqual: lodash_isequal_1.default,
|
|
116
|
-
});
|
|
117
|
-
return {
|
|
118
|
-
data: result,
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Reads the query from the cache if any of the following conditions are met:
|
|
123
|
-
* a) We already have the result of this query in the operation cache
|
|
124
|
-
* b) We have the necessary metadata, as well as all of the necessary fields info to complete
|
|
125
|
-
* this query from the field cache
|
|
126
|
-
*/
|
|
127
|
-
readOrFetchQuery(args) {
|
|
128
|
-
// If we don't have the metadata for the Query yet, we won't know how
|
|
129
|
-
// to read it from the cache. In this case we first need to fetch the query
|
|
130
|
-
// and check for the meta in the extensions to know how to normalize it
|
|
131
|
-
try {
|
|
132
|
-
const readResult = this.readQuery(args);
|
|
133
|
-
if (readResult) {
|
|
134
|
-
this.fetchQuery(args);
|
|
135
|
-
}
|
|
136
|
-
return readResult;
|
|
137
|
-
}
|
|
138
|
-
catch {
|
|
139
|
-
return this.fetchQuery(args);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* Loads the query if it's not already in the cache,
|
|
144
|
-
* useful when hydrating the cache outside of a component
|
|
145
|
-
*/
|
|
146
|
-
preloadQuery(queryName, options) {
|
|
147
|
-
const { blockIfStale = true } = options;
|
|
148
|
-
const variables = (0, helpers_1.variableString)(options.variables ?? {});
|
|
149
|
-
const sha256Hash = this._persistedOperations[queryName];
|
|
150
|
-
const key = `${sha256Hash}:${variables}`;
|
|
151
|
-
const operation = this._cacheStore.operations[key];
|
|
152
|
-
// TODO: Give an option to eager return if we already have the operation but it's stale
|
|
153
|
-
if (!operation || operation.stale) {
|
|
154
|
-
const inFlight = this.fetchQuery({ queryName, variables: variables, options: {} });
|
|
155
|
-
return operation && blockIfStale === false ? operation : inFlight;
|
|
156
|
-
}
|
|
157
|
-
return operation;
|
|
158
|
-
}
|
|
159
|
-
fetchQuery(args) {
|
|
160
|
-
return this._executeOperation('query', args.queryName, args.variables, args.options);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Executes a mutation, returning the result of that mutation
|
|
164
|
-
*/
|
|
165
|
-
async executeMutation(mutationName, variables, options = {}) {
|
|
166
|
-
return this._executeOperation('mutation', mutationName, variables, options);
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Executes a query, returning the result of that query
|
|
170
|
-
*/
|
|
171
|
-
async executeQuery(queryName, variables) {
|
|
172
|
-
return this._executeOperation('query', queryName, variables);
|
|
173
|
-
}
|
|
174
|
-
async executeSubscription() {
|
|
175
|
-
throw new Error('Not yet supported');
|
|
176
|
-
}
|
|
177
|
-
_getKey(operationName, variables) {
|
|
178
|
-
const sha256Hash = this._persistedOperations[operationName];
|
|
179
|
-
return `${sha256Hash}:${(0, helpers_1.variableString)(variables)}`;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* "Subscribes" to a query, meaning that when there are updates to fields in the
|
|
183
|
-
* field cache, we'll re-materialize the known value of the query. We'll also
|
|
184
|
-
* process based on configuration options, such as TTL, invalidateOnMutation
|
|
185
|
-
*/
|
|
186
|
-
subscribeToQuery(args) {
|
|
187
|
-
var _a;
|
|
188
|
-
const { queryName, queryResult, variables, onUpdate, options } = args;
|
|
189
|
-
const key = this._getKey(queryName, variables);
|
|
190
|
-
const queries = ((_a = this._subscribedQueries)[key] ?? (_a[key] = []));
|
|
191
|
-
queries.push(args);
|
|
192
|
-
// Now that the component is subscribed, we can begin fetching,
|
|
193
|
-
// refetching, and otherwise managing the query
|
|
194
|
-
if (!queryResult) {
|
|
195
|
-
this.readOrFetchQuery(args);
|
|
196
|
-
}
|
|
197
|
-
else if (options.ttl) {
|
|
198
|
-
//
|
|
199
|
-
}
|
|
200
|
-
return () => {
|
|
201
|
-
queries.splice(queries.indexOf(args), 1);
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
async _executeOperation(operationType, operationName, variables) {
|
|
205
|
-
const sha256Hash = this._persistedOperations[operationName];
|
|
206
|
-
const key = this._getKey(operationName, variables);
|
|
207
|
-
const operationVariables = (0, helpers_1.variableObject)(variables);
|
|
208
|
-
(0, errors_1.assertOperationHash)(sha256Hash, operationType, operationName);
|
|
209
|
-
const inFlight = this._inFlight[key];
|
|
210
|
-
if (operationType === 'query' && inFlight) {
|
|
211
|
-
return inFlight;
|
|
212
|
-
}
|
|
213
|
-
const operationInFlight = this._fetch({
|
|
214
|
-
query: '',
|
|
215
|
-
operationName,
|
|
216
|
-
variables: operationVariables,
|
|
217
|
-
extensions: {
|
|
218
|
-
persistedQuery: { version: 1, sha256Hash },
|
|
219
|
-
},
|
|
220
|
-
});
|
|
221
|
-
this._inFlight[key] = operationInFlight;
|
|
222
|
-
const operationResult = await operationInFlight.finally(() => {
|
|
223
|
-
delete this._inFlight[key];
|
|
224
|
-
});
|
|
225
|
-
if (operationResult.fetchError) {
|
|
226
|
-
return operationResult;
|
|
227
|
-
}
|
|
228
|
-
const meta = operationResult.extensions?.['graphqlNormalizeMeta'] ??
|
|
229
|
-
this._getMeta(operationName);
|
|
230
|
-
// Ensure we have metadata to be able to normalize the operation in our cache
|
|
231
|
-
if (!meta) {
|
|
232
|
-
throw new errors_1.MissingMetaError(operationName);
|
|
233
|
-
}
|
|
234
|
-
const lastCache = this._cacheStore;
|
|
235
|
-
this._cacheStore = (0, immer_1.produce)(this._cacheStore, (c) => {
|
|
236
|
-
const currentResult = c.operations[key];
|
|
237
|
-
const { added, modified, result } = (0, graphql_normalize_1.graphqlNormalize)({
|
|
238
|
-
action: 'write',
|
|
239
|
-
operationResult,
|
|
240
|
-
meta,
|
|
241
|
-
cache: c.fields,
|
|
242
|
-
variableValues: operationVariables,
|
|
243
|
-
currentResult,
|
|
244
|
-
isEqual: lodash_isequal_1.default,
|
|
245
|
-
});
|
|
246
|
-
//
|
|
247
|
-
if (operationType === 'query' && (added || modified)) {
|
|
248
|
-
c.operations[key] = {
|
|
249
|
-
stale: false,
|
|
250
|
-
meta,
|
|
251
|
-
operationName,
|
|
252
|
-
variableValues: operationVariables,
|
|
253
|
-
data: result,
|
|
254
|
-
errors: operationResult.errors,
|
|
255
|
-
extensions: operationResult.extensions,
|
|
256
|
-
operationType: operationType,
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
// If we've modified anything in the cache, we also need to update any
|
|
260
|
-
// mounted queries
|
|
261
|
-
if (modified) {
|
|
262
|
-
for (const [k, val] of Object.entries(c.operations)) {
|
|
263
|
-
if (k === key || val.operationType !== 'query') {
|
|
264
|
-
continue;
|
|
265
|
-
}
|
|
266
|
-
//
|
|
267
|
-
if (val.data) {
|
|
268
|
-
const { result } = (0, graphql_normalize_1.graphqlNormalize)({
|
|
269
|
-
action: 'read',
|
|
270
|
-
currentResult: val.data,
|
|
271
|
-
cache: c.fields,
|
|
272
|
-
meta: val.meta,
|
|
273
|
-
variableValues: val.variableValues,
|
|
274
|
-
});
|
|
275
|
-
if (result !== val) {
|
|
276
|
-
val.data = result;
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
});
|
|
282
|
-
// If this was a mutation, we need to go through and determine if we need to mark
|
|
283
|
-
// any Queries as "stale", or eagerly refetch any of the queries
|
|
284
|
-
if (operationType === 'mutation') {
|
|
285
|
-
const toInvalidate = new Set();
|
|
286
|
-
const mutationName = operationName;
|
|
287
|
-
const mutationFn = this._mutationInvalidation?.[mutationName];
|
|
288
|
-
for (const [operationKey, operation] of Object.entries(this._cacheStore.operations)) {
|
|
289
|
-
if (operation.operationType !== 'query') {
|
|
290
|
-
continue;
|
|
291
|
-
}
|
|
292
|
-
const queryName = operation.operationName;
|
|
293
|
-
const queryVariables = operation.variableValues;
|
|
294
|
-
const queryFn = this._queryInvalidation[queryName];
|
|
295
|
-
if (queryFn?.(mutationName, operationVariables, queryVariables)) {
|
|
296
|
-
toInvalidate.add(operationKey);
|
|
297
|
-
}
|
|
298
|
-
if (mutationFn?.(queryName, queryVariables, operationVariables)) {
|
|
299
|
-
toInvalidate.add(operationKey);
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
if (toInvalidate.size) {
|
|
303
|
-
this._cacheStore = (0, immer_1.produce)(this._cacheStore, (o) => {
|
|
304
|
-
for (const key of toInvalidate) {
|
|
305
|
-
const op = o.operations[key];
|
|
306
|
-
if (op)
|
|
307
|
-
op.stale = true;
|
|
308
|
-
}
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
// If we've updated any operations, we need to update any subscribed components
|
|
313
|
-
if (lastCache.operations !== this._cacheStore.operations) {
|
|
314
|
-
for (const [key, val] of Object.entries(this._cacheStore.operations)) {
|
|
315
|
-
if (this._cacheStore.operations[key] !== lastCache.operations[key]) {
|
|
316
|
-
if (this._subscribedQueries[key]) {
|
|
317
|
-
this._subscribedQueries[key]?.forEach((o) => o.onUpdate(val));
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
if (operationType === 'query') {
|
|
323
|
-
return this._cacheStore.operations[key];
|
|
324
|
-
}
|
|
325
|
-
return operationResult;
|
|
326
|
-
}
|
|
327
|
-
_getMeta(opName) {
|
|
328
|
-
const meta = this._cacheStore.meta[opName] ?? this._configMeta?.[opName];
|
|
329
|
-
if (!meta) {
|
|
330
|
-
throw new errors_1.MissingMetaError(opName);
|
|
331
|
-
}
|
|
332
|
-
return meta;
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Handles "fetch", ensuring we catch network errors and handle non-200
|
|
336
|
-
* responses properly so we're able to forward these on in a normalized fashion
|
|
337
|
-
*/
|
|
338
|
-
async _fetch(body) {
|
|
339
|
-
let result;
|
|
340
|
-
try {
|
|
341
|
-
result = await this._makeFetch(body);
|
|
342
|
-
if (result.status === 200) {
|
|
343
|
-
const json = (await result.json());
|
|
344
|
-
return json;
|
|
345
|
-
}
|
|
346
|
-
else {
|
|
347
|
-
return {
|
|
348
|
-
fetchError: {
|
|
349
|
-
name: 'ResponseError',
|
|
350
|
-
message: await result.text(),
|
|
351
|
-
status: result.status,
|
|
352
|
-
statusText: result.statusText,
|
|
353
|
-
},
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
catch (e) {
|
|
358
|
-
const error = e instanceof Error ? e : new Error(String(e));
|
|
359
|
-
return {
|
|
360
|
-
fetchError: {
|
|
361
|
-
name: error.name,
|
|
362
|
-
message: error.message,
|
|
363
|
-
stack: error.stack,
|
|
364
|
-
status: result?.status,
|
|
365
|
-
statusText: result?.statusText,
|
|
366
|
-
},
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
_makeFetch(body) {
|
|
371
|
-
const reqInit = {
|
|
372
|
-
method: 'POST',
|
|
373
|
-
body: JSON.stringify(body),
|
|
374
|
-
headers: {
|
|
375
|
-
'content-type': 'application/json',
|
|
376
|
-
},
|
|
377
|
-
};
|
|
378
|
-
if (this._fetcher) {
|
|
379
|
-
return this._fetcher(this._endpoint, reqInit);
|
|
380
|
-
}
|
|
381
|
-
return fetch(this._endpoint, reqInit);
|
|
382
|
-
}
|
|
383
|
-
/**
|
|
384
|
-
* Determine whether we should refetch the query based on the
|
|
385
|
-
* current value of the query, and the options passed to the query
|
|
386
|
-
*/
|
|
387
|
-
_shouldRefetchQuery() {
|
|
388
|
-
//
|
|
389
|
-
}
|
|
390
|
-
/**
|
|
391
|
-
* "Garbage collection" for existing operations. If they have
|
|
392
|
-
* a TTL or are invalidated by other operations, and aren't mounted,
|
|
393
|
-
* then we can go ahead and sweep out any data we might have for them
|
|
394
|
-
*/
|
|
395
|
-
_gcOperations() {
|
|
396
|
-
//
|
|
397
|
-
}
|
|
398
|
-
}
|
|
399
|
-
exports.GraphQLQueryCache = GraphQLQueryCache;
|
|
400
|
-
function unwrapMeta(meta) {
|
|
401
|
-
if (!meta)
|
|
402
|
-
return meta;
|
|
403
|
-
const o = {};
|
|
404
|
-
for (const [key, val] of Object.entries(meta)) {
|
|
405
|
-
if (typeof val === 'string') {
|
|
406
|
-
o[key] = isBase64(val) ? JSON.parse(fromBase64(val)) : JSON.parse(val);
|
|
407
|
-
}
|
|
408
|
-
else {
|
|
409
|
-
o[key] = val;
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
return o;
|
|
413
|
-
}
|
|
414
|
-
function isBase64(str) {
|
|
415
|
-
if (typeof Buffer !== 'undefined') {
|
|
416
|
-
return Buffer.from(str, 'base64').toString('base64') === str;
|
|
417
|
-
}
|
|
418
|
-
try {
|
|
419
|
-
window.btoa(str);
|
|
420
|
-
return true;
|
|
421
|
-
}
|
|
422
|
-
catch {
|
|
423
|
-
return false;
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
function fromBase64(str) {
|
|
427
|
-
if (typeof Buffer !== 'undefined') {
|
|
428
|
-
return Buffer.from(str, 'base64').toString('utf8');
|
|
429
|
-
}
|
|
430
|
-
return window.atob(str);
|
|
431
|
-
}
|
|
432
|
-
function noop() { }
|
package/cjs/context.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import type { GraphQLQueryCache } from './GraphQLQueryCache.js';
|
|
3
|
-
/**
|
|
4
|
-
* Provides an initialized `GraphQLQueryCache` for use by hooks throughout
|
|
5
|
-
* the rest of the application.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* <GraphQLQueryProvider cache={cache}>
|
|
9
|
-
* <App />
|
|
10
|
-
* </GraphQLQueryProvider>
|
|
11
|
-
*/
|
|
12
|
-
export declare const GraphQLQueryProvider: React.FC<{
|
|
13
|
-
cache: GraphQLQueryCache;
|
|
14
|
-
children: React.ReactNode | React.ReactNode[];
|
|
15
|
-
}>;
|
|
16
|
-
/**
|
|
17
|
-
* Hook returning the initialized GraphQL Query Cache
|
|
18
|
-
*
|
|
19
|
-
* Throws if accessed outside of a `GraphQLQueryProvider`
|
|
20
|
-
*/
|
|
21
|
-
export declare const useGraphQLQueryCache: () => GraphQLQueryCache;
|
package/cjs/context.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useGraphQLQueryCache = exports.GraphQLQueryProvider = void 0;
|
|
4
|
-
const jsx_runtime_1 = require("react/jsx-runtime");
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const GraphQLQueryContext = (0, react_1.createContext)(null);
|
|
7
|
-
/**
|
|
8
|
-
* Provides an initialized `GraphQLQueryCache` for use by hooks throughout
|
|
9
|
-
* the rest of the application.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* <GraphQLQueryProvider cache={cache}>
|
|
13
|
-
* <App />
|
|
14
|
-
* </GraphQLQueryProvider>
|
|
15
|
-
*/
|
|
16
|
-
const GraphQLQueryProvider = ({ cache, children }) => {
|
|
17
|
-
return (0, jsx_runtime_1.jsx)(GraphQLQueryContext.Provider, { value: cache, children: children });
|
|
18
|
-
};
|
|
19
|
-
exports.GraphQLQueryProvider = GraphQLQueryProvider;
|
|
20
|
-
/**
|
|
21
|
-
* Hook returning the initialized GraphQL Query Cache
|
|
22
|
-
*
|
|
23
|
-
* Throws if accessed outside of a `GraphQLQueryProvider`
|
|
24
|
-
*/
|
|
25
|
-
const useGraphQLQueryCache = () => {
|
|
26
|
-
const ctx = (0, react_1.useContext)(GraphQLQueryContext);
|
|
27
|
-
if (!ctx) {
|
|
28
|
-
throw new Error(`useGraphQLQueryCache must be nested inside a GraphQLQueryProvider`);
|
|
29
|
-
}
|
|
30
|
-
return ctx;
|
|
31
|
-
};
|
|
32
|
-
exports.useGraphQLQueryCache = useGraphQLQueryCache;
|
package/cjs/errors.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
export declare class GraphQLQueryError extends Error {
|
|
2
|
-
}
|
|
3
|
-
/**
|
|
4
|
-
* Thrown in situations where we are expecting graphql-normalize metadata
|
|
5
|
-
* to be available, but it isn't yet available.
|
|
6
|
-
*/
|
|
7
|
-
export declare class MissingMetaError extends Error {
|
|
8
|
-
constructor(queryName: string);
|
|
9
|
-
}
|
|
10
|
-
export declare class UnknownOperationError extends Error {
|
|
11
|
-
constructor(operationName: string, operationType: string);
|
|
12
|
-
}
|
|
13
|
-
export declare class MissingNormalizeMetaError extends Error {
|
|
14
|
-
constructor(operationName: string, operationType: string);
|
|
15
|
-
}
|
|
16
|
-
export declare class ExpectedPreloadedQueryError extends GraphQLQueryError {
|
|
17
|
-
constructor(queryName: string);
|
|
18
|
-
}
|
|
19
|
-
export declare function assertOperationHash(hash: string | undefined, operationType: 'query' | 'mutation' | 'subscription', operationName: string): void;
|
package/cjs/errors.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assertOperationHash = exports.ExpectedPreloadedQueryError = exports.MissingNormalizeMetaError = exports.UnknownOperationError = exports.MissingMetaError = exports.GraphQLQueryError = void 0;
|
|
4
|
-
class GraphQLQueryError extends Error {
|
|
5
|
-
}
|
|
6
|
-
exports.GraphQLQueryError = GraphQLQueryError;
|
|
7
|
-
/**
|
|
8
|
-
* Thrown in situations where we are expecting graphql-normalize metadata
|
|
9
|
-
* to be available, but it isn't yet available.
|
|
10
|
-
*/
|
|
11
|
-
class MissingMetaError extends Error {
|
|
12
|
-
constructor(queryName) {
|
|
13
|
-
super(`Cannot readQuery('${queryName}') without graphql-normalize metadata.`);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
exports.MissingMetaError = MissingMetaError;
|
|
17
|
-
class UnknownOperationError extends Error {
|
|
18
|
-
constructor(operationName, operationType) {
|
|
19
|
-
super(`Unknown ${operationType} operation ${operationName}, not found in the hash of operations`);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
exports.UnknownOperationError = UnknownOperationError;
|
|
23
|
-
class MissingNormalizeMetaError extends Error {
|
|
24
|
-
constructor(operationName, operationType) {
|
|
25
|
-
super(`extensions.graphqlNormalizeMeta is was expected for the ${operationName} ${operationType}.\n You must configure your GraphQLQueryCache with the "meta" property, or add this as part of your repsonse payload`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
exports.MissingNormalizeMetaError = MissingNormalizeMetaError;
|
|
29
|
-
class ExpectedPreloadedQueryError extends GraphQLQueryError {
|
|
30
|
-
constructor(queryName) {
|
|
31
|
-
super(`Expected query ${queryName} to be preloaded with preloadQuery`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
exports.ExpectedPreloadedQueryError = ExpectedPreloadedQueryError;
|
|
35
|
-
function assertOperationHash(hash, operationType, operationName) {
|
|
36
|
-
if (!hash) {
|
|
37
|
-
throw new UnknownOperationError(operationName, operationType);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.assertOperationHash = assertOperationHash;
|
package/cjs/fragmentData.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import './types';
|
|
2
|
-
type Pretty<O> = {
|
|
3
|
-
[K in keyof O]: O[K];
|
|
4
|
-
} & {};
|
|
5
|
-
type UnwrapFragment<T> = T extends Array<infer U> ? Array<UnwrapFragment<U>> : T extends ReadonlyArray<infer U> ? ReadonlyArray<ReadonlyArray<U>> : T extends object ? T extends {
|
|
6
|
-
' $fragmentRefs'?: infer Refs;
|
|
7
|
-
} ? Pretty<{
|
|
8
|
-
[K in keyof T as K extends ' $fragmentName' | ' $fragmentRefs' ? never : K]: UnwrapFragment<T[K]>;
|
|
9
|
-
} & {
|
|
10
|
-
[K in keyof Refs]: UnwrapFragment<Refs[K]>;
|
|
11
|
-
}[keyof Refs]> : {
|
|
12
|
-
[K in keyof T as K extends ' $fragmentName' ? never : K]: UnwrapFragment<T[K]>;
|
|
13
|
-
} : T;
|
|
14
|
-
export type FragmentType<Frag extends keyof GraphQLQuery.FragmentRegistry> = GraphQLQuery.FragmentRegistry[Frag] extends infer FragType ? FragType extends {
|
|
15
|
-
' $fragmentName'?: infer TKey;
|
|
16
|
-
} ? TKey extends string ? {
|
|
17
|
-
' $fragmentRefs'?: {
|
|
18
|
-
[key in TKey]: FragType;
|
|
19
|
-
};
|
|
20
|
-
} : never : never : never;
|
|
21
|
-
export type FragmentTypeUnwrapped<Frag extends keyof GraphQLQuery.FragmentRegistry> = GraphQLQuery.FragmentRegistry[Frag] extends infer FragType ? FragType extends object ? UnwrapFragment<FragType> : never : never;
|
|
22
|
-
export declare function unwrapFragment<Frag extends keyof GraphQLQuery.FragmentRegistry>(_fragmentName: Frag, fragmentType: FragmentType<Frag>): GraphQLQuery.FragmentRegistry[Frag];
|
|
23
|
-
export declare function unwrapFragment<Frag extends keyof GraphQLQuery.FragmentRegistry>(_fragmentName: Frag, fragmentType: FragmentType<Frag> | null | undefined): GraphQLQuery.FragmentRegistry[Frag] | null | undefined;
|
|
24
|
-
export declare function unwrapFragment<Frag extends keyof GraphQLQuery.FragmentRegistry>(_fragmentName: Frag, fragmentType: ReadonlyArray<FragmentType<Frag>>): ReadonlyArray<GraphQLQuery.FragmentRegistry[Frag]>;
|
|
25
|
-
export declare function unwrapFragment<Frag extends keyof GraphQLQuery.FragmentRegistry>(_fragmentName: Frag, fragmentType: ReadonlyArray<FragmentType<Frag>> | null | undefined): ReadonlyArray<GraphQLQuery.FragmentRegistry[Frag]> | null | undefined;
|
|
26
|
-
export declare function unwrapFragmentDeep<Frag extends keyof GraphQLQuery.FragmentRegistry>(_fragmentName: Frag, fragmentType: FragmentType<Frag>): FragmentTypeUnwrapped<Frag>;
|
|
27
|
-
export declare function unwrapFragmentDeep<Frag extends keyof GraphQLQuery.FragmentRegistry>(_fragmentName: Frag, fragmentType: FragmentType<Frag> | null | undefined): FragmentTypeUnwrapped<Frag> | null | undefined;
|
|
28
|
-
export declare function unwrapFragmentDeep<Frag extends keyof GraphQLQuery.FragmentRegistry>(_fragmentName: Frag, fragmentType: ReadonlyArray<FragmentType<Frag>>): ReadonlyArray<FragmentTypeUnwrapped<Frag>>;
|
|
29
|
-
export declare function unwrapFragmentDeep<Frag extends keyof GraphQLQuery.FragmentRegistry>(_fragmentName: Frag, fragmentType: ReadonlyArray<FragmentType<Frag>> | null | undefined): ReadonlyArray<FragmentTypeUnwrapped<Frag>> | null | undefined;
|
|
30
|
-
export declare function castFragmentData<F extends keyof GraphQLQuery.FragmentRegistry, FT extends FragmentTypeUnwrapped<F>>(_fragmentName: F, data: FT): FragmentType<F>;
|
|
31
|
-
export {};
|
package/cjs/fragmentData.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.castFragmentData = exports.unwrapFragmentDeep = exports.unwrapFragment = void 0;
|
|
4
|
-
require("./types");
|
|
5
|
-
function unwrapFragment(_fragmentName, fragmentType) {
|
|
6
|
-
return fragmentType;
|
|
7
|
-
}
|
|
8
|
-
exports.unwrapFragment = unwrapFragment;
|
|
9
|
-
function unwrapFragmentDeep(_fragmentName, fragmentType) {
|
|
10
|
-
return fragmentType;
|
|
11
|
-
}
|
|
12
|
-
exports.unwrapFragmentDeep = unwrapFragmentDeep;
|
|
13
|
-
function castFragmentData(_fragmentName, data) {
|
|
14
|
-
return data;
|
|
15
|
-
}
|
|
16
|
-
exports.castFragmentData = castFragmentData;
|
package/cjs/graphqlHooks.d.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { GraphQLPreloadedQueryResult, GraphQLQueryLazyResult, GraphQLQueryResult, ExecutionOptions, PersistedQueryOptions, QueryVariables, GraphQLMutationResult, ExecuteMutationOptions } from './types';
|
|
2
|
-
/**
|
|
3
|
-
* Loads a preloaded query. Like useQuery, except it throws if the
|
|
4
|
-
* query does not already exist in the cache.
|
|
5
|
-
*
|
|
6
|
-
* This hook keeps track of the value of the last render, so we're always guaranteed
|
|
7
|
-
* to have data here, even if it's potentially stale.
|
|
8
|
-
*/
|
|
9
|
-
export declare function usePreloadedPersistedQuery<QueryName extends keyof GraphQLQuery.QueryRegistry>(query: QueryName, options: PersistedQueryOptions & QueryVariables<QueryName>): GraphQLPreloadedQueryResult<QueryName>;
|
|
10
|
-
/**
|
|
11
|
-
* Retrieves the persisted query from the cache,
|
|
12
|
-
* creating it from the cache if we don't have a mounted version
|
|
13
|
-
*
|
|
14
|
-
* Fetches the query if:
|
|
15
|
-
*
|
|
16
|
-
* 1. We don't already have the operation fetched in the cache
|
|
17
|
-
* 2. The query is considered stale based on:
|
|
18
|
-
* a) The TTL setting
|
|
19
|
-
* b) A mutation which invalidates the query
|
|
20
|
-
*/
|
|
21
|
-
export declare function usePersistedQuery<QueryName extends keyof GraphQLQuery.QueryRegistry>(queryName: QueryName, options: PersistedQueryOptions & QueryVariables<QueryName>): GraphQLQueryResult<QueryName>;
|
|
22
|
-
/**
|
|
23
|
-
* TODO: Not working yet
|
|
24
|
-
* Creates a container for a query that isn't fetched immediately,
|
|
25
|
-
* but provides a loadQuery function to call
|
|
26
|
-
*/
|
|
27
|
-
export declare function useLazyPersistedQuery<QueryName extends keyof GraphQLQuery.QueryRegistry>(query: QueryName): GraphQLQueryLazyResult<QueryName>;
|
|
28
|
-
/**
|
|
29
|
-
* Executes a persisted mutation, keeping track of mutations in-flight to ensure
|
|
30
|
-
* we don't execute the same mutation multiple times with the same values
|
|
31
|
-
* in the same component
|
|
32
|
-
*/
|
|
33
|
-
export declare function usePersistedMutation<MutationName extends keyof GraphQLQuery.MutationRegistry>(mutationName: MutationName, options?: ExecutionOptions): {
|
|
34
|
-
execute: (vars: GraphQLQuery.OperationVariables[MutationName], opts?: ExecuteMutationOptions) => Promise<GraphQLMutationResult<MutationName>>;
|
|
35
|
-
isExecuting: boolean;
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* TODO: Subscription management
|
|
39
|
-
*/
|
|
40
|
-
export declare function usePersistedSubscription<SubscriptionName extends keyof GraphQLQuery.SubscriptionRegistry>(subscriptionName: SubscriptionName): void;
|
|
41
|
-
type UseOnMutationFn<M> = () => void;
|
|
42
|
-
/**
|
|
43
|
-
* Hook called when a mutation is issued, anywhere in the application.
|
|
44
|
-
* Useful for one-off situations where we want to subscribe and refetch something
|
|
45
|
-
* based on a mutation event
|
|
46
|
-
*/
|
|
47
|
-
export declare function useOnMutation<M extends keyof GraphQLQuery.MutationRegistry | '*'>(mutation: M | Array<M>, fn: UseOnMutationFn<M>): void;
|
|
48
|
-
export {};
|