1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | var apollo_link_1 = require("apollo-link");
|
5 | var ts_invariant_1 = require("ts-invariant");
|
6 | var QueryManager_1 = require("./core/QueryManager");
|
7 | var LocalState_1 = require("./core/LocalState");
|
8 | var store_1 = require("./data/store");
|
9 | var version_1 = require("./version");
|
10 | var hasSuggestedDevtools = false;
|
11 | var ApolloClient = (function () {
|
12 | function ApolloClient(options) {
|
13 | var _this = this;
|
14 | this.defaultOptions = {};
|
15 | this.resetStoreCallbacks = [];
|
16 | this.clearStoreCallbacks = [];
|
17 | var cache = options.cache, _a = options.ssrMode, ssrMode = _a === void 0 ? false : _a, _b = options.ssrForceFetchDelay, ssrForceFetchDelay = _b === void 0 ? 0 : _b, connectToDevTools = options.connectToDevTools, _c = options.queryDeduplication, queryDeduplication = _c === void 0 ? true : _c, defaultOptions = options.defaultOptions, _d = options.assumeImmutableResults, assumeImmutableResults = _d === void 0 ? false : _d, resolvers = options.resolvers, typeDefs = options.typeDefs, fragmentMatcher = options.fragmentMatcher, clientAwarenessName = options.name, clientAwarenessVersion = options.version;
|
18 | var link = options.link;
|
19 | if (!link && resolvers) {
|
20 | link = apollo_link_1.ApolloLink.empty();
|
21 | }
|
22 | if (!link || !cache) {
|
23 | throw new ts_invariant_1.InvariantError("In order to initialize Apollo Client, you must specify 'link' and 'cache' properties in the options object.\n" +
|
24 | "These options are part of the upgrade requirements when migrating from Apollo Client 1.x to Apollo Client 2.x.\n" +
|
25 | "For more information, please visit: https://www.apollographql.com/docs/tutorial/client.html#apollo-client-setup");
|
26 | }
|
27 | this.link = link;
|
28 | this.cache = cache;
|
29 | this.store = new store_1.DataStore(cache);
|
30 | this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0;
|
31 | this.queryDeduplication = queryDeduplication;
|
32 | this.defaultOptions = defaultOptions || {};
|
33 | this.typeDefs = typeDefs;
|
34 | if (ssrForceFetchDelay) {
|
35 | setTimeout(function () { return (_this.disableNetworkFetches = false); }, ssrForceFetchDelay);
|
36 | }
|
37 | this.watchQuery = this.watchQuery.bind(this);
|
38 | this.query = this.query.bind(this);
|
39 | this.mutate = this.mutate.bind(this);
|
40 | this.resetStore = this.resetStore.bind(this);
|
41 | this.reFetchObservableQueries = this.reFetchObservableQueries.bind(this);
|
42 | var defaultConnectToDevTools = process.env.NODE_ENV !== 'production' &&
|
43 | typeof window !== 'undefined' &&
|
44 | !window.__APOLLO_CLIENT__;
|
45 | if (typeof connectToDevTools === 'undefined'
|
46 | ? defaultConnectToDevTools
|
47 | : connectToDevTools && typeof window !== 'undefined') {
|
48 | window.__APOLLO_CLIENT__ = this;
|
49 | }
|
50 | if (!hasSuggestedDevtools && process.env.NODE_ENV !== 'production') {
|
51 | hasSuggestedDevtools = true;
|
52 | if (typeof window !== 'undefined' &&
|
53 | window.document &&
|
54 | window.top === window.self) {
|
55 | if (typeof window.__APOLLO_DEVTOOLS_GLOBAL_HOOK__ === 'undefined') {
|
56 | if (window.navigator &&
|
57 | window.navigator.userAgent &&
|
58 | window.navigator.userAgent.indexOf('Chrome') > -1) {
|
59 | console.debug('Download the Apollo DevTools ' +
|
60 | 'for a better development experience: ' +
|
61 | 'https://chrome.google.com/webstore/detail/apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm');
|
62 | }
|
63 | }
|
64 | }
|
65 | }
|
66 | this.version = version_1.version;
|
67 | this.localState = new LocalState_1.LocalState({
|
68 | cache: cache,
|
69 | client: this,
|
70 | resolvers: resolvers,
|
71 | fragmentMatcher: fragmentMatcher,
|
72 | });
|
73 | this.queryManager = new QueryManager_1.QueryManager({
|
74 | link: this.link,
|
75 | store: this.store,
|
76 | queryDeduplication: queryDeduplication,
|
77 | ssrMode: ssrMode,
|
78 | clientAwareness: {
|
79 | name: clientAwarenessName,
|
80 | version: clientAwarenessVersion,
|
81 | },
|
82 | localState: this.localState,
|
83 | assumeImmutableResults: assumeImmutableResults,
|
84 | onBroadcast: function () {
|
85 | if (_this.devToolsHookCb) {
|
86 | _this.devToolsHookCb({
|
87 | action: {},
|
88 | state: {
|
89 | queries: _this.queryManager.queryStore.getStore(),
|
90 | mutations: _this.queryManager.mutationStore.getStore(),
|
91 | },
|
92 | dataWithOptimisticResults: _this.cache.extract(true),
|
93 | });
|
94 | }
|
95 | },
|
96 | });
|
97 | }
|
98 | ApolloClient.prototype.stop = function () {
|
99 | this.queryManager.stop();
|
100 | };
|
101 | ApolloClient.prototype.watchQuery = function (options) {
|
102 | if (this.defaultOptions.watchQuery) {
|
103 | options = tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions.watchQuery), options);
|
104 | }
|
105 | if (this.disableNetworkFetches &&
|
106 | (options.fetchPolicy === 'network-only' ||
|
107 | options.fetchPolicy === 'cache-and-network')) {
|
108 | options = tslib_1.__assign(tslib_1.__assign({}, options), { fetchPolicy: 'cache-first' });
|
109 | }
|
110 | return this.queryManager.watchQuery(options);
|
111 | };
|
112 | ApolloClient.prototype.query = function (options) {
|
113 | if (this.defaultOptions.query) {
|
114 | options = tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions.query), options);
|
115 | }
|
116 | ts_invariant_1.invariant(options.fetchPolicy !== 'cache-and-network', 'The cache-and-network fetchPolicy does not work with client.query, because ' +
|
117 | 'client.query can only return a single result. Please use client.watchQuery ' +
|
118 | 'to receive multiple results from the cache and the network, or consider ' +
|
119 | 'using a different fetchPolicy, such as cache-first or network-only.');
|
120 | if (this.disableNetworkFetches && options.fetchPolicy === 'network-only') {
|
121 | options = tslib_1.__assign(tslib_1.__assign({}, options), { fetchPolicy: 'cache-first' });
|
122 | }
|
123 | return this.queryManager.query(options);
|
124 | };
|
125 | ApolloClient.prototype.mutate = function (options) {
|
126 | if (this.defaultOptions.mutate) {
|
127 | options = tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions.mutate), options);
|
128 | }
|
129 | return this.queryManager.mutate(options);
|
130 | };
|
131 | ApolloClient.prototype.subscribe = function (options) {
|
132 | return this.queryManager.startGraphQLSubscription(options);
|
133 | };
|
134 | ApolloClient.prototype.readQuery = function (options, optimistic) {
|
135 | if (optimistic === void 0) { optimistic = false; }
|
136 | return this.cache.readQuery(options, optimistic);
|
137 | };
|
138 | ApolloClient.prototype.readFragment = function (options, optimistic) {
|
139 | if (optimistic === void 0) { optimistic = false; }
|
140 | return this.cache.readFragment(options, optimistic);
|
141 | };
|
142 | ApolloClient.prototype.writeQuery = function (options) {
|
143 | var result = this.cache.writeQuery(options);
|
144 | this.queryManager.broadcastQueries();
|
145 | return result;
|
146 | };
|
147 | ApolloClient.prototype.writeFragment = function (options) {
|
148 | var result = this.cache.writeFragment(options);
|
149 | this.queryManager.broadcastQueries();
|
150 | return result;
|
151 | };
|
152 | ApolloClient.prototype.writeData = function (options) {
|
153 | var result = this.cache.writeData(options);
|
154 | this.queryManager.broadcastQueries();
|
155 | return result;
|
156 | };
|
157 | ApolloClient.prototype.__actionHookForDevTools = function (cb) {
|
158 | this.devToolsHookCb = cb;
|
159 | };
|
160 | ApolloClient.prototype.__requestRaw = function (payload) {
|
161 | return apollo_link_1.execute(this.link, payload);
|
162 | };
|
163 | ApolloClient.prototype.initQueryManager = function () {
|
164 | ts_invariant_1.invariant.warn('Calling the initQueryManager method is no longer necessary, ' +
|
165 | 'and it will be removed from ApolloClient in version 3.0.');
|
166 | return this.queryManager;
|
167 | };
|
168 | ApolloClient.prototype.resetStore = function () {
|
169 | var _this = this;
|
170 | return Promise.resolve()
|
171 | .then(function () { return _this.queryManager.clearStore(); })
|
172 | .then(function () { return Promise.all(_this.resetStoreCallbacks.map(function (fn) { return fn(); })); })
|
173 | .then(function () { return _this.reFetchObservableQueries(); });
|
174 | };
|
175 | ApolloClient.prototype.clearStore = function () {
|
176 | var _this = this;
|
177 | return Promise.resolve()
|
178 | .then(function () { return _this.queryManager.clearStore(); })
|
179 | .then(function () { return Promise.all(_this.clearStoreCallbacks.map(function (fn) { return fn(); })); });
|
180 | };
|
181 | ApolloClient.prototype.onResetStore = function (cb) {
|
182 | var _this = this;
|
183 | this.resetStoreCallbacks.push(cb);
|
184 | return function () {
|
185 | _this.resetStoreCallbacks = _this.resetStoreCallbacks.filter(function (c) { return c !== cb; });
|
186 | };
|
187 | };
|
188 | ApolloClient.prototype.onClearStore = function (cb) {
|
189 | var _this = this;
|
190 | this.clearStoreCallbacks.push(cb);
|
191 | return function () {
|
192 | _this.clearStoreCallbacks = _this.clearStoreCallbacks.filter(function (c) { return c !== cb; });
|
193 | };
|
194 | };
|
195 | ApolloClient.prototype.reFetchObservableQueries = function (includeStandby) {
|
196 | return this.queryManager.reFetchObservableQueries(includeStandby);
|
197 | };
|
198 | ApolloClient.prototype.extract = function (optimistic) {
|
199 | return this.cache.extract(optimistic);
|
200 | };
|
201 | ApolloClient.prototype.restore = function (serializedState) {
|
202 | return this.cache.restore(serializedState);
|
203 | };
|
204 | ApolloClient.prototype.addResolvers = function (resolvers) {
|
205 | this.localState.addResolvers(resolvers);
|
206 | };
|
207 | ApolloClient.prototype.setResolvers = function (resolvers) {
|
208 | this.localState.setResolvers(resolvers);
|
209 | };
|
210 | ApolloClient.prototype.getResolvers = function () {
|
211 | return this.localState.getResolvers();
|
212 | };
|
213 | ApolloClient.prototype.setLocalStateFragmentMatcher = function (fragmentMatcher) {
|
214 | this.localState.setFragmentMatcher(fragmentMatcher);
|
215 | };
|
216 | return ApolloClient;
|
217 | }());
|
218 | exports.default = ApolloClient;
|
219 |
|
\ | No newline at end of file |