UNPKG

11.5 kBJavaScriptView Raw
1import { __assign } from "tslib";
2import { invariant, InvariantError } from "../utilities/globals/index.js";
3import { ApolloLink, execute } from "../link/core/index.js";
4import { version } from "../version.js";
5import { HttpLink } from "../link/http/index.js";
6import { QueryManager } from "./QueryManager.js";
7import { LocalState, } from "./LocalState.js";
8var hasSuggestedDevtools = false;
9import { mergeOptions } from "../utilities/index.js";
10export { mergeOptions };
11var ApolloClient = (function () {
12 function ApolloClient(options) {
13 var _this = this;
14 this.resetStoreCallbacks = [];
15 this.clearStoreCallbacks = [];
16 var uri = options.uri, credentials = options.credentials, headers = options.headers, cache = options.cache, _a = options.ssrMode, ssrMode = _a === void 0 ? false : _a, _b = options.ssrForceFetchDelay, ssrForceFetchDelay = _b === void 0 ? 0 : _b, _c = options.connectToDevTools, connectToDevTools = _c === void 0 ? typeof window === 'object' &&
17 !window.__APOLLO_CLIENT__ &&
18 __DEV__ : _c, _d = options.queryDeduplication, queryDeduplication = _d === void 0 ? true : _d, defaultOptions = options.defaultOptions, _e = options.assumeImmutableResults, assumeImmutableResults = _e === void 0 ? false : _e, resolvers = options.resolvers, typeDefs = options.typeDefs, fragmentMatcher = options.fragmentMatcher, clientAwarenessName = options.name, clientAwarenessVersion = options.version;
19 var link = options.link;
20 if (!link) {
21 link = uri
22 ? new HttpLink({ uri: uri, credentials: credentials, headers: headers })
23 : ApolloLink.empty();
24 }
25 if (!cache) {
26 throw __DEV__ ? new InvariantError("To initialize Apollo Client, you must specify a 'cache' property " +
27 "in the options object. \n" +
28 "For more information, please visit: https://go.apollo.dev/c/docs") : new InvariantError(9);
29 }
30 this.link = link;
31 this.cache = cache;
32 this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0;
33 this.queryDeduplication = queryDeduplication;
34 this.defaultOptions = defaultOptions || Object.create(null);
35 this.typeDefs = typeDefs;
36 if (ssrForceFetchDelay) {
37 setTimeout(function () { return (_this.disableNetworkFetches = false); }, ssrForceFetchDelay);
38 }
39 this.watchQuery = this.watchQuery.bind(this);
40 this.query = this.query.bind(this);
41 this.mutate = this.mutate.bind(this);
42 this.resetStore = this.resetStore.bind(this);
43 this.reFetchObservableQueries = this.reFetchObservableQueries.bind(this);
44 if (connectToDevTools && typeof window === 'object') {
45 window.__APOLLO_CLIENT__ = this;
46 }
47 if (!hasSuggestedDevtools && connectToDevTools && __DEV__) {
48 hasSuggestedDevtools = true;
49 if (typeof window !== 'undefined' &&
50 window.document &&
51 window.top === window.self &&
52 !window.__APOLLO_DEVTOOLS_GLOBAL_HOOK__) {
53 var nav = window.navigator;
54 var ua = nav && nav.userAgent;
55 var url = void 0;
56 if (typeof ua === "string") {
57 if (ua.indexOf("Chrome/") > -1) {
58 url = "https://chrome.google.com/webstore/detail/" +
59 "apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm";
60 }
61 else if (ua.indexOf("Firefox/") > -1) {
62 url = "https://addons.mozilla.org/en-US/firefox/addon/apollo-developer-tools/";
63 }
64 }
65 if (url) {
66 __DEV__ && invariant.log("Download the Apollo DevTools for a better development " +
67 "experience: " + url);
68 }
69 }
70 }
71 this.version = version;
72 this.localState = new LocalState({
73 cache: cache,
74 client: this,
75 resolvers: resolvers,
76 fragmentMatcher: fragmentMatcher,
77 });
78 this.queryManager = new QueryManager({
79 cache: this.cache,
80 link: this.link,
81 defaultOptions: this.defaultOptions,
82 queryDeduplication: queryDeduplication,
83 ssrMode: ssrMode,
84 clientAwareness: {
85 name: clientAwarenessName,
86 version: clientAwarenessVersion,
87 },
88 localState: this.localState,
89 assumeImmutableResults: assumeImmutableResults,
90 onBroadcast: connectToDevTools ? function () {
91 if (_this.devToolsHookCb) {
92 _this.devToolsHookCb({
93 action: {},
94 state: {
95 queries: _this.queryManager.getQueryStore(),
96 mutations: _this.queryManager.mutationStore || {},
97 },
98 dataWithOptimisticResults: _this.cache.extract(true),
99 });
100 }
101 } : void 0,
102 });
103 }
104 ApolloClient.prototype.stop = function () {
105 this.queryManager.stop();
106 };
107 ApolloClient.prototype.watchQuery = function (options) {
108 if (this.defaultOptions.watchQuery) {
109 options = mergeOptions(this.defaultOptions.watchQuery, options);
110 }
111 if (this.disableNetworkFetches &&
112 (options.fetchPolicy === 'network-only' ||
113 options.fetchPolicy === 'cache-and-network')) {
114 options = __assign(__assign({}, options), { fetchPolicy: 'cache-first' });
115 }
116 return this.queryManager.watchQuery(options);
117 };
118 ApolloClient.prototype.query = function (options) {
119 if (this.defaultOptions.query) {
120 options = mergeOptions(this.defaultOptions.query, options);
121 }
122 __DEV__ ? invariant(options.fetchPolicy !== 'cache-and-network', 'The cache-and-network fetchPolicy does not work with client.query, because ' +
123 'client.query can only return a single result. Please use client.watchQuery ' +
124 'to receive multiple results from the cache and the network, or consider ' +
125 'using a different fetchPolicy, such as cache-first or network-only.') : invariant(options.fetchPolicy !== 'cache-and-network', 10);
126 if (this.disableNetworkFetches && options.fetchPolicy === 'network-only') {
127 options = __assign(__assign({}, options), { fetchPolicy: 'cache-first' });
128 }
129 return this.queryManager.query(options);
130 };
131 ApolloClient.prototype.mutate = function (options) {
132 if (this.defaultOptions.mutate) {
133 options = mergeOptions(this.defaultOptions.mutate, options);
134 }
135 return this.queryManager.mutate(options);
136 };
137 ApolloClient.prototype.subscribe = function (options) {
138 return this.queryManager.startGraphQLSubscription(options);
139 };
140 ApolloClient.prototype.readQuery = function (options, optimistic) {
141 if (optimistic === void 0) { optimistic = false; }
142 return this.cache.readQuery(options, optimistic);
143 };
144 ApolloClient.prototype.readFragment = function (options, optimistic) {
145 if (optimistic === void 0) { optimistic = false; }
146 return this.cache.readFragment(options, optimistic);
147 };
148 ApolloClient.prototype.writeQuery = function (options) {
149 var ref = this.cache.writeQuery(options);
150 if (options.broadcast !== false) {
151 this.queryManager.broadcastQueries();
152 }
153 return ref;
154 };
155 ApolloClient.prototype.writeFragment = function (options) {
156 var ref = this.cache.writeFragment(options);
157 if (options.broadcast !== false) {
158 this.queryManager.broadcastQueries();
159 }
160 return ref;
161 };
162 ApolloClient.prototype.__actionHookForDevTools = function (cb) {
163 this.devToolsHookCb = cb;
164 };
165 ApolloClient.prototype.__requestRaw = function (payload) {
166 return execute(this.link, payload);
167 };
168 ApolloClient.prototype.resetStore = function () {
169 var _this = this;
170 return Promise.resolve()
171 .then(function () { return _this.queryManager.clearStore({
172 discardWatches: false,
173 }); })
174 .then(function () { return Promise.all(_this.resetStoreCallbacks.map(function (fn) { return fn(); })); })
175 .then(function () { return _this.reFetchObservableQueries(); });
176 };
177 ApolloClient.prototype.clearStore = function () {
178 var _this = this;
179 return Promise.resolve()
180 .then(function () { return _this.queryManager.clearStore({
181 discardWatches: true,
182 }); })
183 .then(function () { return Promise.all(_this.clearStoreCallbacks.map(function (fn) { return fn(); })); });
184 };
185 ApolloClient.prototype.onResetStore = function (cb) {
186 var _this = this;
187 this.resetStoreCallbacks.push(cb);
188 return function () {
189 _this.resetStoreCallbacks = _this.resetStoreCallbacks.filter(function (c) { return c !== cb; });
190 };
191 };
192 ApolloClient.prototype.onClearStore = function (cb) {
193 var _this = this;
194 this.clearStoreCallbacks.push(cb);
195 return function () {
196 _this.clearStoreCallbacks = _this.clearStoreCallbacks.filter(function (c) { return c !== cb; });
197 };
198 };
199 ApolloClient.prototype.reFetchObservableQueries = function (includeStandby) {
200 return this.queryManager.reFetchObservableQueries(includeStandby);
201 };
202 ApolloClient.prototype.refetchQueries = function (options) {
203 var map = this.queryManager.refetchQueries(options);
204 var queries = [];
205 var results = [];
206 map.forEach(function (result, obsQuery) {
207 queries.push(obsQuery);
208 results.push(result);
209 });
210 var result = Promise.all(results);
211 result.queries = queries;
212 result.results = results;
213 result.catch(function (error) {
214 __DEV__ && invariant.debug("In client.refetchQueries, Promise.all promise rejected with error ".concat(error));
215 });
216 return result;
217 };
218 ApolloClient.prototype.getObservableQueries = function (include) {
219 if (include === void 0) { include = "active"; }
220 return this.queryManager.getObservableQueries(include);
221 };
222 ApolloClient.prototype.extract = function (optimistic) {
223 return this.cache.extract(optimistic);
224 };
225 ApolloClient.prototype.restore = function (serializedState) {
226 return this.cache.restore(serializedState);
227 };
228 ApolloClient.prototype.addResolvers = function (resolvers) {
229 this.localState.addResolvers(resolvers);
230 };
231 ApolloClient.prototype.setResolvers = function (resolvers) {
232 this.localState.setResolvers(resolvers);
233 };
234 ApolloClient.prototype.getResolvers = function () {
235 return this.localState.getResolvers();
236 };
237 ApolloClient.prototype.setLocalStateFragmentMatcher = function (fragmentMatcher) {
238 this.localState.setFragmentMatcher(fragmentMatcher);
239 };
240 ApolloClient.prototype.setLink = function (newLink) {
241 this.link = this.queryManager.link = newLink;
242 };
243 return ApolloClient;
244}());
245export { ApolloClient };
246//# sourceMappingURL=ApolloClient.js.map
\No newline at end of file