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/graphqlHooks.js
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.useOnMutation = exports.usePersistedSubscription = exports.usePersistedMutation = exports.useLazyPersistedQuery = exports.usePersistedQuery = exports.usePreloadedPersistedQuery = void 0;
|
|
4
|
-
const context_1 = require("./context");
|
|
5
|
-
const react_1 = require("react");
|
|
6
|
-
const errors_1 = require("./errors");
|
|
7
|
-
const helpers_1 = require("./helpers");
|
|
8
|
-
/**
|
|
9
|
-
* Loads a preloaded query. Like useQuery, except it throws if the
|
|
10
|
-
* query does not already exist in the cache.
|
|
11
|
-
*
|
|
12
|
-
* This hook keeps track of the value of the last render, so we're always guaranteed
|
|
13
|
-
* to have data here, even if it's potentially stale.
|
|
14
|
-
*/
|
|
15
|
-
function usePreloadedPersistedQuery(query, options) {
|
|
16
|
-
const result = usePersistedQuery(query, options);
|
|
17
|
-
const lastData = (0, react_1.useRef)(result.data);
|
|
18
|
-
const data = result.data ?? lastData.current;
|
|
19
|
-
if (!data) {
|
|
20
|
-
throw new errors_1.ExpectedPreloadedQueryError(query);
|
|
21
|
-
}
|
|
22
|
-
(0, react_1.useEffect)(() => {
|
|
23
|
-
if (result.data) {
|
|
24
|
-
lastData.current = result.data;
|
|
25
|
-
}
|
|
26
|
-
}, [result.data]);
|
|
27
|
-
return { ...result, data };
|
|
28
|
-
}
|
|
29
|
-
exports.usePreloadedPersistedQuery = usePreloadedPersistedQuery;
|
|
30
|
-
/**
|
|
31
|
-
* Retrieves the persisted query from the cache,
|
|
32
|
-
* creating it from the cache if we don't have a mounted version
|
|
33
|
-
*
|
|
34
|
-
* Fetches the query if:
|
|
35
|
-
*
|
|
36
|
-
* 1. We don't already have the operation fetched in the cache
|
|
37
|
-
* 2. The query is considered stale based on:
|
|
38
|
-
* a) The TTL setting
|
|
39
|
-
* b) A mutation which invalidates the query
|
|
40
|
-
*/
|
|
41
|
-
function usePersistedQuery(queryName, options) {
|
|
42
|
-
const cache = (0, context_1.useGraphQLQueryCache)();
|
|
43
|
-
const optionsRef = (0, react_1.useRef)(options);
|
|
44
|
-
// Ensure we always have the latest options in the optionsRef
|
|
45
|
-
(0, react_1.useEffect)(() => {
|
|
46
|
-
optionsRef.current = options;
|
|
47
|
-
});
|
|
48
|
-
// We need the variables stringified anyway to persist in the cache
|
|
49
|
-
// simplest to just stringify them here
|
|
50
|
-
const variableString = (0, react_1.useMemo)(() => (0, helpers_1.serializeVariables)(options.variables ?? {}), [options.variables]);
|
|
51
|
-
// Read the query result from the cache
|
|
52
|
-
const readQueryResult = (0, react_1.useMemo)(() => {
|
|
53
|
-
return cache.tryReadQuery({
|
|
54
|
-
queryName,
|
|
55
|
-
variables: variableString,
|
|
56
|
-
options: optionsRef.current,
|
|
57
|
-
});
|
|
58
|
-
}, [queryName, variableString, cache]);
|
|
59
|
-
const [queryResult, setQueryResult] = (0, react_1.useState)(readQueryResult);
|
|
60
|
-
(0, react_1.useEffect)(() => {
|
|
61
|
-
setQueryResult(readQueryResult);
|
|
62
|
-
}, [readQueryResult]);
|
|
63
|
-
// If we don't have the query on mount, we fetch it.
|
|
64
|
-
// We "subscribe" to the query so we'll receive
|
|
65
|
-
// updates when the query values change, it becomes stale,
|
|
66
|
-
// or it is refetched
|
|
67
|
-
(0, react_1.useEffect)(() => {
|
|
68
|
-
return cache.subscribeToQuery({
|
|
69
|
-
queryResult,
|
|
70
|
-
queryName,
|
|
71
|
-
variables: variableString,
|
|
72
|
-
onUpdate: setQueryResult,
|
|
73
|
-
options: optionsRef.current,
|
|
74
|
-
});
|
|
75
|
-
}, [queryName, cache, queryResult, variableString]);
|
|
76
|
-
return {
|
|
77
|
-
...queryResult,
|
|
78
|
-
fetched: Boolean(queryResult),
|
|
79
|
-
}; // GraphQLQuery.QueryRegistry[QueryName];
|
|
80
|
-
}
|
|
81
|
-
exports.usePersistedQuery = usePersistedQuery;
|
|
82
|
-
/**
|
|
83
|
-
* TODO: Not working yet
|
|
84
|
-
* Creates a container for a query that isn't fetched immediately,
|
|
85
|
-
* but provides a loadQuery function to call
|
|
86
|
-
*/
|
|
87
|
-
function useLazyPersistedQuery(query) {
|
|
88
|
-
const data = (0, react_1.useRef)(undefined);
|
|
89
|
-
const cache = (0, context_1.useGraphQLQueryCache)();
|
|
90
|
-
// const state = useState<'pending' | 'fetched' | 'loading' | 'stale'>('pending')
|
|
91
|
-
const [stale, setIsStale] = (0, react_1.useState)(false);
|
|
92
|
-
const [fetched, setIsFetched] = (0, react_1.useState)(false);
|
|
93
|
-
const [loading, setIsLoading] = (0, react_1.useState)(false);
|
|
94
|
-
const loadQuery = (0, react_1.useCallback)((variables) => {
|
|
95
|
-
return new Promise((resolve, reject) => {
|
|
96
|
-
const result = cache.readOrFetchQuery({
|
|
97
|
-
queryName: query,
|
|
98
|
-
variables: (0, helpers_1.serializeVariables)(variables),
|
|
99
|
-
options: {},
|
|
100
|
-
});
|
|
101
|
-
// if ('then' in result && typeof result.then === 'function') {
|
|
102
|
-
// Promise.resolve(result).then(resolve, reject)
|
|
103
|
-
// }
|
|
104
|
-
});
|
|
105
|
-
}, [cache, query]);
|
|
106
|
-
const errors = undefined;
|
|
107
|
-
return {
|
|
108
|
-
data: data.current,
|
|
109
|
-
errors,
|
|
110
|
-
loadQuery,
|
|
111
|
-
loading,
|
|
112
|
-
stale,
|
|
113
|
-
fetched,
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
exports.useLazyPersistedQuery = useLazyPersistedQuery;
|
|
117
|
-
/**
|
|
118
|
-
* Executes a persisted mutation, keeping track of mutations in-flight to ensure
|
|
119
|
-
* we don't execute the same mutation multiple times with the same values
|
|
120
|
-
* in the same component
|
|
121
|
-
*/
|
|
122
|
-
function usePersistedMutation(mutationName, options) {
|
|
123
|
-
const cache = (0, context_1.useGraphQLQueryCache)();
|
|
124
|
-
// Keeps track of whether we're executing the mutation or not, useful
|
|
125
|
-
// for showing loading states when we know the mutation is in-flight
|
|
126
|
-
const [isExecuting, setIsExecuting] = (0, react_1.useState)(0);
|
|
127
|
-
const executingRef = (0, react_1.useRef)(new Map());
|
|
128
|
-
const optionsRef = (0, react_1.useRef)(options);
|
|
129
|
-
(0, react_1.useEffect)(() => {
|
|
130
|
-
optionsRef.current = options;
|
|
131
|
-
});
|
|
132
|
-
const execute = (0, react_1.useCallback)(async (vars, opts = {}) => {
|
|
133
|
-
const { skipDedupeGuard = false } = opts;
|
|
134
|
-
setIsExecuting((v) => v + 1);
|
|
135
|
-
const inFlightVars = (0, helpers_1.serializeVariables)(vars);
|
|
136
|
-
const executing = executingRef.current.get(inFlightVars);
|
|
137
|
-
if (executing && !skipDedupeGuard) {
|
|
138
|
-
return executing;
|
|
139
|
-
}
|
|
140
|
-
const inFlight = cache.executeMutation(mutationName, vars, optionsRef.current).finally(() => {
|
|
141
|
-
if (!skipDedupeGuard) {
|
|
142
|
-
executingRef.current.delete(inFlightVars);
|
|
143
|
-
}
|
|
144
|
-
setIsExecuting((v) => v - 1);
|
|
145
|
-
});
|
|
146
|
-
if (!skipDedupeGuard) {
|
|
147
|
-
executingRef.current.set(inFlightVars, inFlight);
|
|
148
|
-
}
|
|
149
|
-
return inFlight;
|
|
150
|
-
}, [cache, mutationName]);
|
|
151
|
-
return {
|
|
152
|
-
execute,
|
|
153
|
-
isExecuting: Boolean(isExecuting > 0),
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
exports.usePersistedMutation = usePersistedMutation;
|
|
157
|
-
/**
|
|
158
|
-
* TODO: Subscription management
|
|
159
|
-
*/
|
|
160
|
-
function usePersistedSubscription(subscriptionName) {
|
|
161
|
-
throw new Error(`usePersistedSubscription(${subscriptionName}) not yet supported`);
|
|
162
|
-
}
|
|
163
|
-
exports.usePersistedSubscription = usePersistedSubscription;
|
|
164
|
-
/**
|
|
165
|
-
* Hook called when a mutation is issued, anywhere in the application.
|
|
166
|
-
* Useful for one-off situations where we want to subscribe and refetch something
|
|
167
|
-
* based on a mutation event
|
|
168
|
-
*/
|
|
169
|
-
function useOnMutation(mutation, fn) {
|
|
170
|
-
const fnRef = (0, react_1.useRef)(fn);
|
|
171
|
-
const cache = (0, context_1.useGraphQLQueryCache)();
|
|
172
|
-
(0, react_1.useEffect)(() => {
|
|
173
|
-
fnRef.current = fn;
|
|
174
|
-
});
|
|
175
|
-
const mutationDeps = Array.isArray(mutation) ? mutation : [mutation];
|
|
176
|
-
(0, react_1.useEffect)(() => {
|
|
177
|
-
return () => {
|
|
178
|
-
//
|
|
179
|
-
};
|
|
180
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
181
|
-
}, [cache, ...mutationDeps]);
|
|
182
|
-
}
|
|
183
|
-
exports.useOnMutation = useOnMutation;
|
package/cjs/helpers.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { SerializedVariables } from './types';
|
|
2
|
-
export declare function serializeVariables(vars: object): SerializedVariables;
|
|
3
|
-
export declare function variableString(val: SerializedVariables | object): SerializedVariables;
|
|
4
|
-
export declare function variableObject(val: SerializedVariables | object): any;
|
package/cjs/helpers.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.variableObject = exports.variableString = exports.serializeVariables = void 0;
|
|
4
|
-
const graphql_normalize_1 = require("graphql-normalize");
|
|
5
|
-
function serializeVariables(vars) {
|
|
6
|
-
return (0, graphql_normalize_1.stringifyVariables)(vars);
|
|
7
|
-
}
|
|
8
|
-
exports.serializeVariables = serializeVariables;
|
|
9
|
-
function variableString(val) {
|
|
10
|
-
return typeof val === 'string' ? val : serializeVariables(val);
|
|
11
|
-
}
|
|
12
|
-
exports.variableString = variableString;
|
|
13
|
-
function variableObject(val) {
|
|
14
|
-
return typeof val === 'string' ? JSON.parse(val) : val;
|
|
15
|
-
}
|
|
16
|
-
exports.variableObject = variableObject;
|
package/cjs/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
export { usePersistedQuery, usePersistedMutation, usePreloadedPersistedQuery, useOnMutation, usePersistedSubscription, } from './graphqlHooks.js';
|
|
2
|
-
export { unwrapFragment, castFragmentData, unwrapFragmentDeep } from './fragmentData.js';
|
|
3
|
-
export type { FragmentType, FragmentTypeUnwrapped } from './fragmentData.js';
|
|
4
|
-
export { GraphQLQueryCache } from './GraphQLQueryCache.js';
|
|
5
|
-
export { useGraphQLQueryCache, GraphQLQueryProvider } from './context.js';
|
|
6
|
-
export type { GraphQLPreloadedQueryResult, GraphQLQueryResult, GraphQLQueryCacheConfig } from './types.js';
|
|
7
|
-
import type * as GraphQLQueryTypes from './types.js';
|
|
8
|
-
export type { GraphQLQueryTypes };
|
package/cjs/index.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.GraphQLQueryProvider = exports.useGraphQLQueryCache = exports.GraphQLQueryCache = exports.unwrapFragmentDeep = exports.castFragmentData = exports.unwrapFragment = exports.usePersistedSubscription = exports.useOnMutation = exports.usePreloadedPersistedQuery = exports.usePersistedMutation = exports.usePersistedQuery = void 0;
|
|
4
|
-
var graphqlHooks_js_1 = require("./graphqlHooks.js");
|
|
5
|
-
Object.defineProperty(exports, "usePersistedQuery", { enumerable: true, get: function () { return graphqlHooks_js_1.usePersistedQuery; } });
|
|
6
|
-
Object.defineProperty(exports, "usePersistedMutation", { enumerable: true, get: function () { return graphqlHooks_js_1.usePersistedMutation; } });
|
|
7
|
-
Object.defineProperty(exports, "usePreloadedPersistedQuery", { enumerable: true, get: function () { return graphqlHooks_js_1.usePreloadedPersistedQuery; } });
|
|
8
|
-
Object.defineProperty(exports, "useOnMutation", { enumerable: true, get: function () { return graphqlHooks_js_1.useOnMutation; } });
|
|
9
|
-
Object.defineProperty(exports, "usePersistedSubscription", { enumerable: true, get: function () { return graphqlHooks_js_1.usePersistedSubscription; } });
|
|
10
|
-
var fragmentData_js_1 = require("./fragmentData.js");
|
|
11
|
-
Object.defineProperty(exports, "unwrapFragment", { enumerable: true, get: function () { return fragmentData_js_1.unwrapFragment; } });
|
|
12
|
-
Object.defineProperty(exports, "castFragmentData", { enumerable: true, get: function () { return fragmentData_js_1.castFragmentData; } });
|
|
13
|
-
Object.defineProperty(exports, "unwrapFragmentDeep", { enumerable: true, get: function () { return fragmentData_js_1.unwrapFragmentDeep; } });
|
|
14
|
-
var GraphQLQueryCache_js_1 = require("./GraphQLQueryCache.js");
|
|
15
|
-
Object.defineProperty(exports, "GraphQLQueryCache", { enumerable: true, get: function () { return GraphQLQueryCache_js_1.GraphQLQueryCache; } });
|
|
16
|
-
var context_js_1 = require("./context.js");
|
|
17
|
-
Object.defineProperty(exports, "useGraphQLQueryCache", { enumerable: true, get: function () { return context_js_1.useGraphQLQueryCache; } });
|
|
18
|
-
Object.defineProperty(exports, "GraphQLQueryProvider", { enumerable: true, get: function () { return context_js_1.GraphQLQueryProvider; } });
|
package/cjs/types.d.ts
DELETED
|
@@ -1,205 +0,0 @@
|
|
|
1
|
-
import type { NormalizeMetaShape } from 'graphql-normalize';
|
|
2
|
-
import type { FormattedExecutionResult, GraphQLFormattedError } from 'graphql';
|
|
3
|
-
import type { GraphQLQueryError } from './errors';
|
|
4
|
-
declare global {
|
|
5
|
-
namespace GraphQLQuery {
|
|
6
|
-
interface QueryRegistry {
|
|
7
|
-
}
|
|
8
|
-
interface MutationRegistry {
|
|
9
|
-
}
|
|
10
|
-
interface SubscriptionRegistry {
|
|
11
|
-
}
|
|
12
|
-
interface OperationVariables {
|
|
13
|
-
}
|
|
14
|
-
interface FragmentRegistry {
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
export interface GraphQLOperationStoreShape<TData = unknown> {
|
|
19
|
-
operationType: 'query' | 'mutation' | 'subscription';
|
|
20
|
-
operationName: string;
|
|
21
|
-
meta: NormalizeMetaShape;
|
|
22
|
-
data?: TData | null;
|
|
23
|
-
errors?: ReadonlyArray<GraphQLFormattedError>;
|
|
24
|
-
extensions?: Record<string, unknown>;
|
|
25
|
-
variableValues: object;
|
|
26
|
-
lastFetchedAt?: Date;
|
|
27
|
-
stale: boolean;
|
|
28
|
-
}
|
|
29
|
-
export interface GraphQLCacheShape {
|
|
30
|
-
/**
|
|
31
|
-
* Normalized representation of the values for fields across all operations
|
|
32
|
-
*/
|
|
33
|
-
fields: Record<string, unknown>;
|
|
34
|
-
/**
|
|
35
|
-
* Mapping of the operations we've seen
|
|
36
|
-
*/
|
|
37
|
-
operations: Record<string, GraphQLOperationStoreShape<any>>;
|
|
38
|
-
/**
|
|
39
|
-
* "OperationName:meta" mapping
|
|
40
|
-
*/
|
|
41
|
-
meta: Record<string, NormalizeMetaShape>;
|
|
42
|
-
}
|
|
43
|
-
export interface GraphQLHydratedCacheShape extends GraphQLCacheShape {
|
|
44
|
-
hydrated: Record<string, Record<string, string>>;
|
|
45
|
-
}
|
|
46
|
-
export interface GraphQLQueryCacheConfig {
|
|
47
|
-
/**
|
|
48
|
-
* The endpoint of our GraphQL Application
|
|
49
|
-
* @default /graphql
|
|
50
|
-
*/
|
|
51
|
-
endpoint?: string;
|
|
52
|
-
/**
|
|
53
|
-
* Allows providing our own implementation of "fetch"
|
|
54
|
-
*/
|
|
55
|
-
fetcher?: typeof fetch;
|
|
56
|
-
/**
|
|
57
|
-
* If we're Server-Side Rendering this code, we can serialize
|
|
58
|
-
* the GraphQLQueryCache and send down with the client bundle,
|
|
59
|
-
* so clients are able to re-hydrate and skip refetching
|
|
60
|
-
*/
|
|
61
|
-
hydratedCache?: GraphQLCacheShape;
|
|
62
|
-
/**
|
|
63
|
-
* A mapping of graphql-normalize metadata, if we decide to provide
|
|
64
|
-
* it rather than sending it down alongside the query
|
|
65
|
-
*
|
|
66
|
-
* https://github.com/tgriesser/graphql-normalize
|
|
67
|
-
*/
|
|
68
|
-
meta?: Record<string, any>;
|
|
69
|
-
/**
|
|
70
|
-
* Mapping of Query Name to hash of the query to execute
|
|
71
|
-
*/
|
|
72
|
-
persistedOperations: Record<string, string>;
|
|
73
|
-
/**
|
|
74
|
-
* Reverse mapping of hashes to documents, useful in development situations
|
|
75
|
-
* where we might not have the hashes persisted on the server yet
|
|
76
|
-
*/
|
|
77
|
-
persistedDocuments?: Record<string, string>;
|
|
78
|
-
/**
|
|
79
|
-
* Rather than having a mutation keep track of the queries it needs to invalidate,
|
|
80
|
-
* the invalidateOnMutation can specify that this query becomes "invalidated" when,
|
|
81
|
-
* a mutation, or specific mutation occurs, meaning the next time it's mounted,
|
|
82
|
-
* it'll be refetched
|
|
83
|
-
*/
|
|
84
|
-
queryInvalidation?: Partial<{
|
|
85
|
-
[Q in keyof GraphQLQuery.QueryRegistry]: QueryInvalidationFn<Q>;
|
|
86
|
-
}>;
|
|
87
|
-
/**
|
|
88
|
-
* When the mutation executes and returns, allows us to invalidate queries,
|
|
89
|
-
* meaning they will be marked as "stale" and will be lazily refetched
|
|
90
|
-
* on subsequent renders.
|
|
91
|
-
*/
|
|
92
|
-
mutationInvalidation?: Partial<{
|
|
93
|
-
[M in keyof GraphQLQuery.MutationRegistry]: MutationInvalidationFn<M>;
|
|
94
|
-
}>;
|
|
95
|
-
/**
|
|
96
|
-
* When an error happens in the query layer, it will run through this error handler.
|
|
97
|
-
*/
|
|
98
|
-
globalOnError?: (e: GraphQLQueryError) => void;
|
|
99
|
-
}
|
|
100
|
-
export type QueryInvalidationFn<Q extends keyof GraphQLQuery.QueryRegistry = keyof GraphQLQuery.QueryRegistry> = <M extends keyof GraphQLQuery.MutationRegistry>(mutationName: M, mutationVariables: GraphQLQuery.OperationVariables[M], queryVariables: GraphQLQuery.OperationVariables[Q]) => boolean;
|
|
101
|
-
export type MutationInvalidationFn<M extends keyof GraphQLQuery.MutationRegistry = keyof GraphQLQuery.MutationRegistry> = <Q extends keyof GraphQLQuery.QueryRegistry>(queryName: Q, queryVariables: GraphQLQuery.OperationVariables[Q], mutationVariables: GraphQLQuery.OperationVariables[M]) => boolean;
|
|
102
|
-
export interface FetchError {
|
|
103
|
-
name: string;
|
|
104
|
-
message: string;
|
|
105
|
-
stack?: string;
|
|
106
|
-
status?: number;
|
|
107
|
-
statusText?: string;
|
|
108
|
-
}
|
|
109
|
-
export interface GraphQLOperationResult<Result> extends FormattedExecutionResult<Result> {
|
|
110
|
-
fetchError?: FetchError;
|
|
111
|
-
}
|
|
112
|
-
export type GraphQLMutationResult<MutationName extends keyof GraphQLQuery.MutationRegistry> = GraphQLOperationResult<GraphQLQuery.MutationRegistry[MutationName]>;
|
|
113
|
-
export interface GraphQLQueryResult<QueryName extends keyof GraphQLQuery.QueryRegistry> extends GraphQLOperationResult<GraphQLQuery.QueryRegistry[QueryName]> {
|
|
114
|
-
fetched: boolean;
|
|
115
|
-
}
|
|
116
|
-
export interface GraphQLQueryLazyResult<QueryName extends keyof GraphQLQuery.QueryRegistry> extends GraphQLQueryResult<QueryName> {
|
|
117
|
-
stale: boolean;
|
|
118
|
-
loading: boolean;
|
|
119
|
-
loadQuery: (variables: GraphQLQuery.OperationVariables[QueryName]) => Promise<FormattedExecutionResult<GraphQLQuery.QueryRegistry[QueryName]>>;
|
|
120
|
-
}
|
|
121
|
-
export interface GraphQLPreloadedQueryResult<QueryName extends keyof GraphQLQuery.QueryRegistry> extends GraphQLQueryResult<QueryName> {
|
|
122
|
-
data: GraphQLQuery.QueryRegistry[QueryName];
|
|
123
|
-
}
|
|
124
|
-
export type MaybePromise<T> = T | Promise<T>;
|
|
125
|
-
export interface GraphQLRequestBody {
|
|
126
|
-
query?: string;
|
|
127
|
-
variables: any;
|
|
128
|
-
operationName?: string;
|
|
129
|
-
extensions?: Record<string, any>;
|
|
130
|
-
}
|
|
131
|
-
export interface ExecutionOptions {
|
|
132
|
-
onError?: (e: GraphQLQueryError) => void;
|
|
133
|
-
}
|
|
134
|
-
export type QueryVariables<QueryName extends keyof GraphQLQuery.QueryRegistry> = QueryName extends keyof GraphQLQuery.OperationVariables ? GraphQLQuery.OperationVariables[QueryName] extends {
|
|
135
|
-
[key: string]: never;
|
|
136
|
-
} ? {
|
|
137
|
-
variables?: unknown;
|
|
138
|
-
} : {
|
|
139
|
-
variables: GraphQLQuery.OperationVariables[QueryName];
|
|
140
|
-
} : never;
|
|
141
|
-
export interface PersistedQueryOptions extends ExecutionOptions {
|
|
142
|
-
/**
|
|
143
|
-
* TTL in milliseconds for the query, after this we'll mark the query
|
|
144
|
-
* as "stale" and revalidate
|
|
145
|
-
*/
|
|
146
|
-
ttl?: number;
|
|
147
|
-
/**
|
|
148
|
-
* Poll interval to refetch the query, in milliseconds.
|
|
149
|
-
*/
|
|
150
|
-
pollInterval?: number;
|
|
151
|
-
}
|
|
152
|
-
export interface ReadQueryArgs<Q extends keyof GraphQLQuery.QueryRegistry> {
|
|
153
|
-
/**
|
|
154
|
-
*
|
|
155
|
-
*/
|
|
156
|
-
queryName: Q;
|
|
157
|
-
/**
|
|
158
|
-
*
|
|
159
|
-
*/
|
|
160
|
-
variables: SerializedVariables | GraphQLQuery.OperationVariables[Q];
|
|
161
|
-
/**
|
|
162
|
-
*
|
|
163
|
-
*/
|
|
164
|
-
options: PersistedQueryOptions;
|
|
165
|
-
}
|
|
166
|
-
export interface SubscribeToQueryArgs<Q extends keyof GraphQLQuery.QueryRegistry> extends ReadQueryArgs<Q> {
|
|
167
|
-
/**
|
|
168
|
-
* The result of readQuery, used to check whether the query is stale
|
|
169
|
-
*/
|
|
170
|
-
queryResult?: GraphQLOperationResult<Q>;
|
|
171
|
-
/**
|
|
172
|
-
* Invoked when the values for this query are updated
|
|
173
|
-
*/
|
|
174
|
-
onUpdate: (nextVal: GraphQLOperationResult<Q>) => any;
|
|
175
|
-
}
|
|
176
|
-
export type InvalidateQueryArgs = keyof GraphQLQuery.QueryRegistry | Array<keyof GraphQLQuery.QueryRegistry>;
|
|
177
|
-
export type SerializedVariables = string & {
|
|
178
|
-
__brand: 'SerializedVariables';
|
|
179
|
-
};
|
|
180
|
-
export type MutationTuple = {
|
|
181
|
-
[K in keyof GraphQLQuery.MutationRegistry]: [K, GraphQLQuery.OperationVariables[K]];
|
|
182
|
-
}[keyof GraphQLQuery.MutationRegistry];
|
|
183
|
-
export type SubscriptionTuple = {
|
|
184
|
-
[K in keyof GraphQLQuery.SubscriptionRegistry]: [K, GraphQLQuery.OperationVariables[K]];
|
|
185
|
-
}[keyof GraphQLQuery.SubscriptionRegistry];
|
|
186
|
-
export interface ExecuteMutationOptions {
|
|
187
|
-
/**
|
|
188
|
-
* By default, we check to ensure the mutation isn't already in-flight with the
|
|
189
|
-
* same variables
|
|
190
|
-
*/
|
|
191
|
-
skipDedupeGuard?: boolean;
|
|
192
|
-
}
|
|
193
|
-
export type PreloadQueryOptions<Q extends keyof GraphQLQuery.QueryRegistry = keyof GraphQLQuery.QueryRegistry> = {
|
|
194
|
-
/**
|
|
195
|
-
* If we have the operation, but we know it's stale, we'll typically refetch
|
|
196
|
-
* and return the promise for the refetch. if "blockIfStale" is false, we will
|
|
197
|
-
* resolve immediately and refetch in the background
|
|
198
|
-
*
|
|
199
|
-
* @default true
|
|
200
|
-
*/
|
|
201
|
-
blockIfStale?: boolean;
|
|
202
|
-
} & QueryVariables<Q>;
|
|
203
|
-
export type HasVariables<V extends keyof GraphQLQuery.OperationVariables> = {
|
|
204
|
-
[K in keyof GraphQLQuery.OperationVariables[V]]: GraphQLQuery.OperationVariables[V][K];
|
|
205
|
-
}[keyof GraphQLQuery.OperationVariables[V]] extends never ? false : true;
|
package/cjs/types.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
import { NormalizeMetaShape } from 'graphql-normalize';
|
|
2
|
-
import type { GraphQLCacheShape, GraphQLMutationResult, GraphQLOperationResult, GraphQLQueryCacheConfig, GraphQLQueryResult, GraphQLRequestBody, InvalidateQueryArgs, MaybePromise, ExecutionOptions, PersistedQueryOptions, ReadQueryArgs, SerializedVariables, SubscribeToQueryArgs, PreloadQueryOptions } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* Manages the cached normalized state, and the execution of
|
|
5
|
-
* data accessed by different components
|
|
6
|
-
*/
|
|
7
|
-
export declare class GraphQLQueryCache {
|
|
8
|
-
/**
|
|
9
|
-
* If an alterative fetch is provided, we will use that for operations
|
|
10
|
-
* Otherwise we'll use the native 'fetch'
|
|
11
|
-
*/
|
|
12
|
-
private _fetcher?;
|
|
13
|
-
/**
|
|
14
|
-
* GraphQL API endpoint
|
|
15
|
-
* @default /graphql
|
|
16
|
-
*/
|
|
17
|
-
private _endpoint;
|
|
18
|
-
private _persistedOperations;
|
|
19
|
-
private _persistedDocuments?;
|
|
20
|
-
private _configMeta?;
|
|
21
|
-
private _subscribedQueries;
|
|
22
|
-
private _inFlight;
|
|
23
|
-
private _cacheStore;
|
|
24
|
-
private _runtimeCache;
|
|
25
|
-
private _queryInvalidation;
|
|
26
|
-
private _mutationInvalidation;
|
|
27
|
-
/**
|
|
28
|
-
* A list of all "effects" that have run, used to keep track of
|
|
29
|
-
*/
|
|
30
|
-
private _effectsIssued;
|
|
31
|
-
constructor(config: GraphQLQueryCacheConfig);
|
|
32
|
-
/**
|
|
33
|
-
* Invalidates a query by name or predicate fn
|
|
34
|
-
*/
|
|
35
|
-
invalidateQuery(toInvalidate: InvalidateQueryArgs): void;
|
|
36
|
-
/**
|
|
37
|
-
* Invalidates a query by name or predicate fn
|
|
38
|
-
*/
|
|
39
|
-
refetchQuery(toRefetch: InvalidateQueryArgs): void;
|
|
40
|
-
/**
|
|
41
|
-
* JSON.stringify(queryCache) produces the data we need
|
|
42
|
-
* to rehydrate the Client
|
|
43
|
-
*/
|
|
44
|
-
toJSON(): GraphQLCacheShape;
|
|
45
|
-
/**
|
|
46
|
-
* Attempts to read a query from the cache, returning undefined
|
|
47
|
-
* if we are unable to for any reason. Used in initial hook execution
|
|
48
|
-
* where we don't want any side-effects, incase we're doing SSR
|
|
49
|
-
*/
|
|
50
|
-
tryReadQuery<Q extends keyof GraphQLQuery.QueryRegistry>(args: ReadQueryArgs<Q>): GraphQLOperationResult<Q> | undefined;
|
|
51
|
-
/**
|
|
52
|
-
* Reads the query from the cache. Throws an error if we are unable
|
|
53
|
-
* to read the data, due to an incomplete result or lack of operation
|
|
54
|
-
* metadata
|
|
55
|
-
*/
|
|
56
|
-
readQuery<Q extends keyof GraphQLQuery.QueryRegistry>(args: ReadQueryArgs<Q>): GraphQLOperationResult<Q>;
|
|
57
|
-
/**
|
|
58
|
-
* Reads the query from the cache if any of the following conditions are met:
|
|
59
|
-
* a) We already have the result of this query in the operation cache
|
|
60
|
-
* b) We have the necessary metadata, as well as all of the necessary fields info to complete
|
|
61
|
-
* this query from the field cache
|
|
62
|
-
*/
|
|
63
|
-
readOrFetchQuery<Q extends keyof GraphQLQuery.QueryRegistry>(args: ReadQueryArgs<Q>): GraphQLOperationResult<Q> | Promise<GraphQLOperationResult<Q>>;
|
|
64
|
-
/**
|
|
65
|
-
* Loads the query if it's not already in the cache,
|
|
66
|
-
* useful when hydrating the cache outside of a component
|
|
67
|
-
*/
|
|
68
|
-
preloadQuery<Q extends keyof GraphQLQuery.QueryRegistry>(queryName: Q, options: PreloadQueryOptions<Q>): MaybePromise<GraphQLOperationResult<Q>>;
|
|
69
|
-
fetchQuery<Q extends keyof GraphQLQuery.QueryRegistry>(args: ReadQueryArgs<Q>): Promise<GraphQLOperationResult<Q>>;
|
|
70
|
-
/**
|
|
71
|
-
* Executes a mutation, returning the result of that mutation
|
|
72
|
-
*/
|
|
73
|
-
executeMutation<M extends keyof GraphQLQuery.MutationRegistry>(mutationName: M, variables: SerializedVariables | object, options?: ExecutionOptions): Promise<GraphQLMutationResult<M>>;
|
|
74
|
-
/**
|
|
75
|
-
* Executes a query, returning the result of that query
|
|
76
|
-
*/
|
|
77
|
-
executeQuery<Q extends keyof GraphQLQuery.QueryRegistry>(queryName: Q, variables: SerializedVariables | GraphQLQuery.OperationVariables[Q]): Promise<GraphQLQueryResult<Q>>;
|
|
78
|
-
executeSubscription(): Promise<void>;
|
|
79
|
-
_getKey(operationName: string, variables: SerializedVariables | object): string;
|
|
80
|
-
/**
|
|
81
|
-
* "Subscribes" to a query, meaning that when there are updates to fields in the
|
|
82
|
-
* field cache, we'll re-materialize the known value of the query. We'll also
|
|
83
|
-
* process based on configuration options, such as TTL, invalidateOnMutation
|
|
84
|
-
*/
|
|
85
|
-
subscribeToQuery<Q extends keyof GraphQLQuery.QueryRegistry>(args: SubscribeToQueryArgs<Q>): () => void;
|
|
86
|
-
_executeOperation<QueryName extends keyof GraphQLQuery.QueryRegistry>(operationType: 'query', operationName: QueryName, variables: SerializedVariables | GraphQLQuery.OperationVariables[QueryName], options?: PersistedQueryOptions): Promise<GraphQLQueryResult<QueryName>>;
|
|
87
|
-
_executeOperation<MutationName extends keyof GraphQLQuery.MutationRegistry>(operationType: 'mutation', operationName: MutationName, variables: SerializedVariables | object, options?: ExecutionOptions): Promise<GraphQLMutationResult<MutationName>>;
|
|
88
|
-
_getMeta(opName: string): NormalizeMetaShape;
|
|
89
|
-
/**
|
|
90
|
-
* Handles "fetch", ensuring we catch network errors and handle non-200
|
|
91
|
-
* responses properly so we're able to forward these on in a normalized fashion
|
|
92
|
-
*/
|
|
93
|
-
_fetch(body: GraphQLRequestBody): Promise<GraphQLOperationResult<any>>;
|
|
94
|
-
_makeFetch(body: GraphQLRequestBody): Promise<Response>;
|
|
95
|
-
/**
|
|
96
|
-
* Determine whether we should refetch the query based on the
|
|
97
|
-
* current value of the query, and the options passed to the query
|
|
98
|
-
*/
|
|
99
|
-
_shouldRefetchQuery(): void;
|
|
100
|
-
/**
|
|
101
|
-
* "Garbage collection" for existing operations. If they have
|
|
102
|
-
* a TTL or are invalidated by other operations, and aren't mounted,
|
|
103
|
-
* then we can go ahead and sweep out any data we might have for them
|
|
104
|
-
*/
|
|
105
|
-
_gcOperations(): void;
|
|
106
|
-
}
|