1 | import { __assign } from "tslib";
|
2 | import { invariant, newInvariantError } from "../utilities/globals/index.js";
|
3 | import { ApolloLink, execute } from "../link/core/index.js";
|
4 | import { version } from "../version.js";
|
5 | import { HttpLink } from "../link/http/index.js";
|
6 | import { QueryManager } from "./QueryManager.js";
|
7 | import { LocalState } from "./LocalState.js";
|
8 | var hasSuggestedDevtools = false;
|
9 | // Though mergeOptions now resides in @apollo/client/utilities, it was
|
10 | // previously declared and exported from this module, and then reexported from
|
11 | // @apollo/client/core. Since we need to preserve that API anyway, the easiest
|
12 | // solution is to reexport mergeOptions where it was previously declared (here).
|
13 | import { mergeOptions } from "../utilities/index.js";
|
14 | import { getApolloClientMemoryInternals } from "../utilities/caching/getMemoryInternals.js";
|
15 | export { mergeOptions };
|
16 | /**
|
17 | * This is the primary Apollo Client class. It is used to send GraphQL documents (i.e. queries
|
18 | * and mutations) to a GraphQL spec-compliant server over an `ApolloLink` instance,
|
19 | * receive results from the server and cache the results in a store. It also delivers updates
|
20 | * to GraphQL queries through `Observable` instances.
|
21 | */
|
22 | var ApolloClient = /** @class */ (function () {
|
23 | /**
|
24 | * Constructs an instance of `ApolloClient`.
|
25 | *
|
26 | * @example
|
27 | * ```js
|
28 | * import { ApolloClient, InMemoryCache } from '@apollo/client';
|
29 | *
|
30 | * const cache = new InMemoryCache();
|
31 | *
|
32 | * const client = new ApolloClient({
|
33 | * // Provide required constructor fields
|
34 | * cache: cache,
|
35 | * uri: 'http://localhost:4000/',
|
36 | *
|
37 | * // Provide some optional constructor fields
|
38 | * name: 'react-web-client',
|
39 | * version: '1.3',
|
40 | * queryDeduplication: false,
|
41 | * defaultOptions: {
|
42 | * watchQuery: {
|
43 | * fetchPolicy: 'cache-and-network',
|
44 | * },
|
45 | * },
|
46 | * });
|
47 | * ```
|
48 | */
|
49 | function ApolloClient(options) {
|
50 | var _this = this;
|
51 | this.resetStoreCallbacks = [];
|
52 | this.clearStoreCallbacks = [];
|
53 | if (!options.cache) {
|
54 | throw newInvariantError(15);
|
55 | }
|
56 | var uri = options.uri, credentials = options.credentials, headers = options.headers, cache = options.cache, documentTransform = options.documentTransform, _a = options.ssrMode, ssrMode = _a === void 0 ? false : _a, _b = options.ssrForceFetchDelay, ssrForceFetchDelay = _b === void 0 ? 0 : _b,
|
57 | // Expose the client instance as window.__APOLLO_CLIENT__ and call
|
58 | // onBroadcast in queryManager.broadcastQueries to enable browser
|
59 | // devtools, but disable them by default in production.
|
60 | _c = options.connectToDevTools,
|
61 | // Expose the client instance as window.__APOLLO_CLIENT__ and call
|
62 | // onBroadcast in queryManager.broadcastQueries to enable browser
|
63 | // devtools, but disable them by default in production.
|
64 | connectToDevTools = _c === void 0 ? typeof window === "object" &&
|
65 | !window.__APOLLO_CLIENT__ &&
|
66 | globalThis.__DEV__ !== false : _c, _d = options.queryDeduplication, queryDeduplication = _d === void 0 ? true : _d, defaultOptions = options.defaultOptions, defaultContext = options.defaultContext, _e = options.assumeImmutableResults, assumeImmutableResults = _e === void 0 ? cache.assumeImmutableResults : _e, resolvers = options.resolvers, typeDefs = options.typeDefs, fragmentMatcher = options.fragmentMatcher, clientAwarenessName = options.name, clientAwarenessVersion = options.version;
|
67 | var link = options.link;
|
68 | if (!link) {
|
69 | link =
|
70 | uri ? new HttpLink({ uri: uri, credentials: credentials, headers: headers }) : ApolloLink.empty();
|
71 | }
|
72 | this.link = link;
|
73 | this.cache = cache;
|
74 | this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0;
|
75 | this.queryDeduplication = queryDeduplication;
|
76 | this.defaultOptions = defaultOptions || Object.create(null);
|
77 | this.typeDefs = typeDefs;
|
78 | if (ssrForceFetchDelay) {
|
79 | setTimeout(function () { return (_this.disableNetworkFetches = false); }, ssrForceFetchDelay);
|
80 | }
|
81 | this.watchQuery = this.watchQuery.bind(this);
|
82 | this.query = this.query.bind(this);
|
83 | this.mutate = this.mutate.bind(this);
|
84 | this.resetStore = this.resetStore.bind(this);
|
85 | this.reFetchObservableQueries = this.reFetchObservableQueries.bind(this);
|
86 | this.version = version;
|
87 | this.localState = new LocalState({
|
88 | cache: cache,
|
89 | client: this,
|
90 | resolvers: resolvers,
|
91 | fragmentMatcher: fragmentMatcher,
|
92 | });
|
93 | this.queryManager = new QueryManager({
|
94 | cache: this.cache,
|
95 | link: this.link,
|
96 | defaultOptions: this.defaultOptions,
|
97 | defaultContext: defaultContext,
|
98 | documentTransform: documentTransform,
|
99 | queryDeduplication: queryDeduplication,
|
100 | ssrMode: ssrMode,
|
101 | clientAwareness: {
|
102 | name: clientAwarenessName,
|
103 | version: clientAwarenessVersion,
|
104 | },
|
105 | localState: this.localState,
|
106 | assumeImmutableResults: assumeImmutableResults,
|
107 | onBroadcast: connectToDevTools ?
|
108 | function () {
|
109 | if (_this.devToolsHookCb) {
|
110 | _this.devToolsHookCb({
|
111 | action: {},
|
112 | state: {
|
113 | queries: _this.queryManager.getQueryStore(),
|
114 | mutations: _this.queryManager.mutationStore || {},
|
115 | },
|
116 | dataWithOptimisticResults: _this.cache.extract(true),
|
117 | });
|
118 | }
|
119 | }
|
120 | : void 0,
|
121 | });
|
122 | if (connectToDevTools)
|
123 | this.connectToDevTools();
|
124 | }
|
125 | ApolloClient.prototype.connectToDevTools = function () {
|
126 | if (typeof window === "object") {
|
127 | var windowWithDevTools = window;
|
128 | var devtoolsSymbol = Symbol.for("apollo.devtools");
|
129 | (windowWithDevTools[devtoolsSymbol] =
|
130 | windowWithDevTools[devtoolsSymbol] || []).push(this);
|
131 | windowWithDevTools.__APOLLO_CLIENT__ = this;
|
132 | }
|
133 | /**
|
134 | * Suggest installing the devtools for developers who don't have them
|
135 | */
|
136 | if (!hasSuggestedDevtools && globalThis.__DEV__ !== false) {
|
137 | hasSuggestedDevtools = true;
|
138 | setTimeout(function () {
|
139 | if (typeof window !== "undefined" &&
|
140 | window.document &&
|
141 | window.top === window.self &&
|
142 | !window.__APOLLO_DEVTOOLS_GLOBAL_HOOK__) {
|
143 | var nav = window.navigator;
|
144 | var ua = nav && nav.userAgent;
|
145 | var url = void 0;
|
146 | if (typeof ua === "string") {
|
147 | if (ua.indexOf("Chrome/") > -1) {
|
148 | url =
|
149 | "https://chrome.google.com/webstore/detail/" +
|
150 | "apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm";
|
151 | }
|
152 | else if (ua.indexOf("Firefox/") > -1) {
|
153 | url =
|
154 | "https://addons.mozilla.org/en-US/firefox/addon/apollo-developer-tools/";
|
155 | }
|
156 | }
|
157 | if (url) {
|
158 | globalThis.__DEV__ !== false && invariant.log("Download the Apollo DevTools for a better development " +
|
159 | "experience: %s", url);
|
160 | }
|
161 | }
|
162 | }, 10000);
|
163 | }
|
164 | };
|
165 | Object.defineProperty(ApolloClient.prototype, "documentTransform", {
|
166 | /**
|
167 | * The `DocumentTransform` used to modify GraphQL documents before a request
|
168 | * is made. If a custom `DocumentTransform` is not provided, this will be the
|
169 | * default document transform.
|
170 | */
|
171 | get: function () {
|
172 | return this.queryManager.documentTransform;
|
173 | },
|
174 | enumerable: false,
|
175 | configurable: true
|
176 | });
|
177 | /**
|
178 | * Call this method to terminate any active client processes, making it safe
|
179 | * to dispose of this `ApolloClient` instance.
|
180 | */
|
181 | ApolloClient.prototype.stop = function () {
|
182 | this.queryManager.stop();
|
183 | };
|
184 | /**
|
185 | * This watches the cache store of the query according to the options specified and
|
186 | * returns an `ObservableQuery`. We can subscribe to this `ObservableQuery` and
|
187 | * receive updated results through a GraphQL observer when the cache store changes.
|
188 | *
|
189 | * Note that this method is not an implementation of GraphQL subscriptions. Rather,
|
190 | * it uses Apollo's store in order to reactively deliver updates to your query results.
|
191 | *
|
192 | * For example, suppose you call watchQuery on a GraphQL query that fetches a person's
|
193 | * first and last name and this person has a particular object identifier, provided by
|
194 | * dataIdFromObject. Later, a different query fetches that same person's
|
195 | * first and last name and the first name has now changed. Then, any observers associated
|
196 | * with the results of the first query will be updated with a new result object.
|
197 | *
|
198 | * Note that if the cache does not change, the subscriber will *not* be notified.
|
199 | *
|
200 | * See [here](https://medium.com/apollo-stack/the-concepts-of-graphql-bc68bd819be3#.3mb0cbcmc) for
|
201 | * a description of store reactivity.
|
202 | */
|
203 | ApolloClient.prototype.watchQuery = function (options) {
|
204 | if (this.defaultOptions.watchQuery) {
|
205 | options = mergeOptions(this.defaultOptions.watchQuery, options);
|
206 | }
|
207 | // XXX Overwriting options is probably not the best way to do this long term...
|
208 | if (this.disableNetworkFetches &&
|
209 | (options.fetchPolicy === "network-only" ||
|
210 | options.fetchPolicy === "cache-and-network")) {
|
211 | options = __assign(__assign({}, options), { fetchPolicy: "cache-first" });
|
212 | }
|
213 | return this.queryManager.watchQuery(options);
|
214 | };
|
215 | /**
|
216 | * This resolves a single query according to the options specified and
|
217 | * returns a `Promise` which is either resolved with the resulting data
|
218 | * or rejected with an error.
|
219 | *
|
220 | * @param options - An object of type `QueryOptions` that allows us to
|
221 | * describe how this query should be treated e.g. whether it should hit the
|
222 | * server at all or just resolve from the cache, etc.
|
223 | */
|
224 | ApolloClient.prototype.query = function (options) {
|
225 | if (this.defaultOptions.query) {
|
226 | options = mergeOptions(this.defaultOptions.query, options);
|
227 | }
|
228 | invariant(options.fetchPolicy !== "cache-and-network", 16);
|
229 | if (this.disableNetworkFetches && options.fetchPolicy === "network-only") {
|
230 | options = __assign(__assign({}, options), { fetchPolicy: "cache-first" });
|
231 | }
|
232 | return this.queryManager.query(options);
|
233 | };
|
234 | /**
|
235 | * This resolves a single mutation according to the options specified and returns a
|
236 | * Promise which is either resolved with the resulting data or rejected with an
|
237 | * error. In some cases both `data` and `errors` might be undefined, for example
|
238 | * when `errorPolicy` is set to `'ignore'`.
|
239 | *
|
240 | * It takes options as an object with the following keys and values:
|
241 | */
|
242 | ApolloClient.prototype.mutate = function (options) {
|
243 | if (this.defaultOptions.mutate) {
|
244 | options = mergeOptions(this.defaultOptions.mutate, options);
|
245 | }
|
246 | return this.queryManager.mutate(options);
|
247 | };
|
248 | /**
|
249 | * This subscribes to a graphql subscription according to the options specified and returns an
|
250 | * `Observable` which either emits received data or an error.
|
251 | */
|
252 | ApolloClient.prototype.subscribe = function (options) {
|
253 | return this.queryManager.startGraphQLSubscription(options);
|
254 | };
|
255 | /**
|
256 | * Tries to read some data from the store in the shape of the provided
|
257 | * GraphQL query without making a network request. This method will start at
|
258 | * the root query. To start at a specific id returned by `dataIdFromObject`
|
259 | * use `readFragment`.
|
260 | *
|
261 | * @param optimistic - Set to `true` to allow `readQuery` to return
|
262 | * optimistic results. Is `false` by default.
|
263 | */
|
264 | ApolloClient.prototype.readQuery = function (options, optimistic) {
|
265 | if (optimistic === void 0) { optimistic = false; }
|
266 | return this.cache.readQuery(options, optimistic);
|
267 | };
|
268 | /**
|
269 | * Tries to read some data from the store in the shape of the provided
|
270 | * GraphQL fragment without making a network request. This method will read a
|
271 | * GraphQL fragment from any arbitrary id that is currently cached, unlike
|
272 | * `readQuery` which will only read from the root query.
|
273 | *
|
274 | * You must pass in a GraphQL document with a single fragment or a document
|
275 | * with multiple fragments that represent what you are reading. If you pass
|
276 | * in a document with multiple fragments then you must also specify a
|
277 | * `fragmentName`.
|
278 | *
|
279 | * @param optimistic - Set to `true` to allow `readFragment` to return
|
280 | * optimistic results. Is `false` by default.
|
281 | */
|
282 | ApolloClient.prototype.readFragment = function (options, optimistic) {
|
283 | if (optimistic === void 0) { optimistic = false; }
|
284 | return this.cache.readFragment(options, optimistic);
|
285 | };
|
286 | /**
|
287 | * Writes some data in the shape of the provided GraphQL query directly to
|
288 | * the store. This method will start at the root query. To start at a
|
289 | * specific id returned by `dataIdFromObject` then use `writeFragment`.
|
290 | */
|
291 | ApolloClient.prototype.writeQuery = function (options) {
|
292 | var ref = this.cache.writeQuery(options);
|
293 | if (options.broadcast !== false) {
|
294 | this.queryManager.broadcastQueries();
|
295 | }
|
296 | return ref;
|
297 | };
|
298 | /**
|
299 | * Writes some data in the shape of the provided GraphQL fragment directly to
|
300 | * the store. This method will write to a GraphQL fragment from any arbitrary
|
301 | * id that is currently cached, unlike `writeQuery` which will only write
|
302 | * from the root query.
|
303 | *
|
304 | * You must pass in a GraphQL document with a single fragment or a document
|
305 | * with multiple fragments that represent what you are writing. If you pass
|
306 | * in a document with multiple fragments then you must also specify a
|
307 | * `fragmentName`.
|
308 | */
|
309 | ApolloClient.prototype.writeFragment = function (options) {
|
310 | var ref = this.cache.writeFragment(options);
|
311 | if (options.broadcast !== false) {
|
312 | this.queryManager.broadcastQueries();
|
313 | }
|
314 | return ref;
|
315 | };
|
316 | ApolloClient.prototype.__actionHookForDevTools = function (cb) {
|
317 | this.devToolsHookCb = cb;
|
318 | };
|
319 | ApolloClient.prototype.__requestRaw = function (payload) {
|
320 | return execute(this.link, payload);
|
321 | };
|
322 | /**
|
323 | * Resets your entire store by clearing out your cache and then re-executing
|
324 | * all of your active queries. This makes it so that you may guarantee that
|
325 | * there is no data left in your store from a time before you called this
|
326 | * method.
|
327 | *
|
328 | * `resetStore()` is useful when your user just logged out. You’ve removed the
|
329 | * user session, and you now want to make sure that any references to data you
|
330 | * might have fetched while the user session was active is gone.
|
331 | *
|
332 | * It is important to remember that `resetStore()` *will* refetch any active
|
333 | * queries. This means that any components that might be mounted will execute
|
334 | * their queries again using your network interface. If you do not want to
|
335 | * re-execute any queries then you should make sure to stop watching any
|
336 | * active queries.
|
337 | */
|
338 | ApolloClient.prototype.resetStore = function () {
|
339 | var _this = this;
|
340 | return Promise.resolve()
|
341 | .then(function () {
|
342 | return _this.queryManager.clearStore({
|
343 | discardWatches: false,
|
344 | });
|
345 | })
|
346 | .then(function () { return Promise.all(_this.resetStoreCallbacks.map(function (fn) { return fn(); })); })
|
347 | .then(function () { return _this.reFetchObservableQueries(); });
|
348 | };
|
349 | /**
|
350 | * Remove all data from the store. Unlike `resetStore`, `clearStore` will
|
351 | * not refetch any active queries.
|
352 | */
|
353 | ApolloClient.prototype.clearStore = function () {
|
354 | var _this = this;
|
355 | return Promise.resolve()
|
356 | .then(function () {
|
357 | return _this.queryManager.clearStore({
|
358 | discardWatches: true,
|
359 | });
|
360 | })
|
361 | .then(function () { return Promise.all(_this.clearStoreCallbacks.map(function (fn) { return fn(); })); });
|
362 | };
|
363 | /**
|
364 | * Allows callbacks to be registered that are executed when the store is
|
365 | * reset. `onResetStore` returns an unsubscribe function that can be used
|
366 | * to remove registered callbacks.
|
367 | */
|
368 | ApolloClient.prototype.onResetStore = function (cb) {
|
369 | var _this = this;
|
370 | this.resetStoreCallbacks.push(cb);
|
371 | return function () {
|
372 | _this.resetStoreCallbacks = _this.resetStoreCallbacks.filter(function (c) { return c !== cb; });
|
373 | };
|
374 | };
|
375 | /**
|
376 | * Allows callbacks to be registered that are executed when the store is
|
377 | * cleared. `onClearStore` returns an unsubscribe function that can be used
|
378 | * to remove registered callbacks.
|
379 | */
|
380 | ApolloClient.prototype.onClearStore = function (cb) {
|
381 | var _this = this;
|
382 | this.clearStoreCallbacks.push(cb);
|
383 | return function () {
|
384 | _this.clearStoreCallbacks = _this.clearStoreCallbacks.filter(function (c) { return c !== cb; });
|
385 | };
|
386 | };
|
387 | /**
|
388 | * Refetches all of your active queries.
|
389 | *
|
390 | * `reFetchObservableQueries()` is useful if you want to bring the client back to proper state in case of a network outage
|
391 | *
|
392 | * It is important to remember that `reFetchObservableQueries()` *will* refetch any active
|
393 | * queries. This means that any components that might be mounted will execute
|
394 | * their queries again using your network interface. If you do not want to
|
395 | * re-execute any queries then you should make sure to stop watching any
|
396 | * active queries.
|
397 | * Takes optional parameter `includeStandby` which will include queries in standby-mode when refetching.
|
398 | */
|
399 | ApolloClient.prototype.reFetchObservableQueries = function (includeStandby) {
|
400 | return this.queryManager.reFetchObservableQueries(includeStandby);
|
401 | };
|
402 | /**
|
403 | * Refetches specified active queries. Similar to "reFetchObservableQueries()" but with a specific list of queries.
|
404 | *
|
405 | * `refetchQueries()` is useful for use cases to imperatively refresh a selection of queries.
|
406 | *
|
407 | * It is important to remember that `refetchQueries()` *will* refetch specified active
|
408 | * queries. This means that any components that might be mounted will execute
|
409 | * their queries again using your network interface. If you do not want to
|
410 | * re-execute any queries then you should make sure to stop watching any
|
411 | * active queries.
|
412 | */
|
413 | ApolloClient.prototype.refetchQueries = function (options) {
|
414 | var map = this.queryManager.refetchQueries(options);
|
415 | var queries = [];
|
416 | var results = [];
|
417 | map.forEach(function (result, obsQuery) {
|
418 | queries.push(obsQuery);
|
419 | results.push(result);
|
420 | });
|
421 | var result = Promise.all(results);
|
422 | // In case you need the raw results immediately, without awaiting
|
423 | // Promise.all(results):
|
424 | result.queries = queries;
|
425 | result.results = results;
|
426 | // If you decide to ignore the result Promise because you're using
|
427 | // result.queries and result.results instead, you shouldn't have to worry
|
428 | // about preventing uncaught rejections for the Promise.all result.
|
429 | result.catch(function (error) {
|
430 | globalThis.__DEV__ !== false && invariant.debug(17, error);
|
431 | });
|
432 | return result;
|
433 | };
|
434 | /**
|
435 | * Get all currently active `ObservableQuery` objects, in a `Map` keyed by
|
436 | * query ID strings.
|
437 | *
|
438 | * An "active" query is one that has observers and a `fetchPolicy` other than
|
439 | * "standby" or "cache-only".
|
440 | *
|
441 | * You can include all `ObservableQuery` objects (including the inactive ones)
|
442 | * by passing "all" instead of "active", or you can include just a subset of
|
443 | * active queries by passing an array of query names or DocumentNode objects.
|
444 | */
|
445 | ApolloClient.prototype.getObservableQueries = function (include) {
|
446 | if (include === void 0) { include = "active"; }
|
447 | return this.queryManager.getObservableQueries(include);
|
448 | };
|
449 | /**
|
450 | * Exposes the cache's complete state, in a serializable format for later restoration.
|
451 | */
|
452 | ApolloClient.prototype.extract = function (optimistic) {
|
453 | return this.cache.extract(optimistic);
|
454 | };
|
455 | /**
|
456 | * Replaces existing state in the cache (if any) with the values expressed by
|
457 | * `serializedState`.
|
458 | *
|
459 | * Called when hydrating a cache (server side rendering, or offline storage),
|
460 | * and also (potentially) during hot reloads.
|
461 | */
|
462 | ApolloClient.prototype.restore = function (serializedState) {
|
463 | return this.cache.restore(serializedState);
|
464 | };
|
465 | /**
|
466 | * Add additional local resolvers.
|
467 | */
|
468 | ApolloClient.prototype.addResolvers = function (resolvers) {
|
469 | this.localState.addResolvers(resolvers);
|
470 | };
|
471 | /**
|
472 | * Set (override existing) local resolvers.
|
473 | */
|
474 | ApolloClient.prototype.setResolvers = function (resolvers) {
|
475 | this.localState.setResolvers(resolvers);
|
476 | };
|
477 | /**
|
478 | * Get all registered local resolvers.
|
479 | */
|
480 | ApolloClient.prototype.getResolvers = function () {
|
481 | return this.localState.getResolvers();
|
482 | };
|
483 | /**
|
484 | * Set a custom local state fragment matcher.
|
485 | */
|
486 | ApolloClient.prototype.setLocalStateFragmentMatcher = function (fragmentMatcher) {
|
487 | this.localState.setFragmentMatcher(fragmentMatcher);
|
488 | };
|
489 | /**
|
490 | * Define a new ApolloLink (or link chain) that Apollo Client will use.
|
491 | */
|
492 | ApolloClient.prototype.setLink = function (newLink) {
|
493 | this.link = this.queryManager.link = newLink;
|
494 | };
|
495 | Object.defineProperty(ApolloClient.prototype, "defaultContext", {
|
496 | get: function () {
|
497 | return this.queryManager.defaultContext;
|
498 | },
|
499 | enumerable: false,
|
500 | configurable: true
|
501 | });
|
502 | return ApolloClient;
|
503 | }());
|
504 | export { ApolloClient };
|
505 | if (globalThis.__DEV__ !== false) {
|
506 | ApolloClient.prototype.getMemoryInternals = getApolloClientMemoryInternals;
|
507 | }
|
508 | //# sourceMappingURL=ApolloClient.js.map |
\ | No newline at end of file |