"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var apollo_link_1 = require("apollo-link"); var ts_invariant_1 = require("ts-invariant"); var QueryManager_1 = require("./core/QueryManager"); var LocalState_1 = require("./core/LocalState"); var store_1 = require("./data/store"); var version_1 = require("./version"); var hasSuggestedDevtools = false; var ApolloClient = (function () { function ApolloClient(options) { var _this = this; this.defaultOptions = {}; this.resetStoreCallbacks = []; this.clearStoreCallbacks = []; 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; var link = options.link; if (!link && resolvers) { link = apollo_link_1.ApolloLink.empty(); } if (!link || !cache) { throw new ts_invariant_1.InvariantError("In order to initialize Apollo Client, you must specify 'link' and 'cache' properties in the options object.\n" + "These options are part of the upgrade requirements when migrating from Apollo Client 1.x to Apollo Client 2.x.\n" + "For more information, please visit: https://www.apollographql.com/docs/tutorial/client.html#apollo-client-setup"); } this.link = link; this.cache = cache; this.store = new store_1.DataStore(cache); this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0; this.queryDeduplication = queryDeduplication; this.defaultOptions = defaultOptions || {}; this.typeDefs = typeDefs; if (ssrForceFetchDelay) { setTimeout(function () { return (_this.disableNetworkFetches = false); }, ssrForceFetchDelay); } this.watchQuery = this.watchQuery.bind(this); this.query = this.query.bind(this); this.mutate = this.mutate.bind(this); this.resetStore = this.resetStore.bind(this); this.reFetchObservableQueries = this.reFetchObservableQueries.bind(this); var defaultConnectToDevTools = process.env.NODE_ENV !== 'production' && typeof window !== 'undefined' && !window.__APOLLO_CLIENT__; if (typeof connectToDevTools === 'undefined' ? defaultConnectToDevTools : connectToDevTools && typeof window !== 'undefined') { window.__APOLLO_CLIENT__ = this; } if (!hasSuggestedDevtools && process.env.NODE_ENV !== 'production') { hasSuggestedDevtools = true; if (typeof window !== 'undefined' && window.document && window.top === window.self) { if (typeof window.__APOLLO_DEVTOOLS_GLOBAL_HOOK__ === 'undefined') { if (window.navigator && window.navigator.userAgent && window.navigator.userAgent.indexOf('Chrome') > -1) { console.debug('Download the Apollo DevTools ' + 'for a better development experience: ' + 'https://chrome.google.com/webstore/detail/apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm'); } } } } this.version = version_1.version; this.localState = new LocalState_1.LocalState({ cache: cache, client: this, resolvers: resolvers, fragmentMatcher: fragmentMatcher, }); this.queryManager = new QueryManager_1.QueryManager({ link: this.link, store: this.store, queryDeduplication: queryDeduplication, ssrMode: ssrMode, clientAwareness: { name: clientAwarenessName, version: clientAwarenessVersion, }, localState: this.localState, assumeImmutableResults: assumeImmutableResults, onBroadcast: function () { if (_this.devToolsHookCb) { _this.devToolsHookCb({ action: {}, state: { queries: _this.queryManager.queryStore.getStore(), mutations: _this.queryManager.mutationStore.getStore(), }, dataWithOptimisticResults: _this.cache.extract(true), }); } }, }); } ApolloClient.prototype.stop = function () { this.queryManager.stop(); }; ApolloClient.prototype.watchQuery = function (options) { if (this.defaultOptions.watchQuery) { options = tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions.watchQuery), options); } if (this.disableNetworkFetches && (options.fetchPolicy === 'network-only' || options.fetchPolicy === 'cache-and-network')) { options = tslib_1.__assign(tslib_1.__assign({}, options), { fetchPolicy: 'cache-first' }); } return this.queryManager.watchQuery(options); }; ApolloClient.prototype.query = function (options) { if (this.defaultOptions.query) { options = tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions.query), options); } ts_invariant_1.invariant(options.fetchPolicy !== 'cache-and-network', 'The cache-and-network fetchPolicy does not work with client.query, because ' + 'client.query can only return a single result. Please use client.watchQuery ' + 'to receive multiple results from the cache and the network, or consider ' + 'using a different fetchPolicy, such as cache-first or network-only.'); if (this.disableNetworkFetches && options.fetchPolicy === 'network-only') { options = tslib_1.__assign(tslib_1.__assign({}, options), { fetchPolicy: 'cache-first' }); } return this.queryManager.query(options); }; ApolloClient.prototype.mutate = function (options) { if (this.defaultOptions.mutate) { options = tslib_1.__assign(tslib_1.__assign({}, this.defaultOptions.mutate), options); } return this.queryManager.mutate(options); }; ApolloClient.prototype.subscribe = function (options) { return this.queryManager.startGraphQLSubscription(options); }; ApolloClient.prototype.readQuery = function (options, optimistic) { if (optimistic === void 0) { optimistic = false; } return this.cache.readQuery(options, optimistic); }; ApolloClient.prototype.readFragment = function (options, optimistic) { if (optimistic === void 0) { optimistic = false; } return this.cache.readFragment(options, optimistic); }; ApolloClient.prototype.writeQuery = function (options) { var result = this.cache.writeQuery(options); this.queryManager.broadcastQueries(); return result; }; ApolloClient.prototype.writeFragment = function (options) { var result = this.cache.writeFragment(options); this.queryManager.broadcastQueries(); return result; }; ApolloClient.prototype.writeData = function (options) { var result = this.cache.writeData(options); this.queryManager.broadcastQueries(); return result; }; ApolloClient.prototype.__actionHookForDevTools = function (cb) { this.devToolsHookCb = cb; }; ApolloClient.prototype.__requestRaw = function (payload) { return apollo_link_1.execute(this.link, payload); }; ApolloClient.prototype.initQueryManager = function () { ts_invariant_1.invariant.warn('Calling the initQueryManager method is no longer necessary, ' + 'and it will be removed from ApolloClient in version 3.0.'); return this.queryManager; }; ApolloClient.prototype.resetStore = function () { var _this = this; return Promise.resolve() .then(function () { return _this.queryManager.clearStore(); }) .then(function () { return Promise.all(_this.resetStoreCallbacks.map(function (fn) { return fn(); })); }) .then(function () { return _this.reFetchObservableQueries(); }); }; ApolloClient.prototype.clearStore = function () { var _this = this; return Promise.resolve() .then(function () { return _this.queryManager.clearStore(); }) .then(function () { return Promise.all(_this.clearStoreCallbacks.map(function (fn) { return fn(); })); }); }; ApolloClient.prototype.onResetStore = function (cb) { var _this = this; this.resetStoreCallbacks.push(cb); return function () { _this.resetStoreCallbacks = _this.resetStoreCallbacks.filter(function (c) { return c !== cb; }); }; }; ApolloClient.prototype.onClearStore = function (cb) { var _this = this; this.clearStoreCallbacks.push(cb); return function () { _this.clearStoreCallbacks = _this.clearStoreCallbacks.filter(function (c) { return c !== cb; }); }; }; ApolloClient.prototype.reFetchObservableQueries = function (includeStandby) { return this.queryManager.reFetchObservableQueries(includeStandby); }; ApolloClient.prototype.extract = function (optimistic) { return this.cache.extract(optimistic); }; ApolloClient.prototype.restore = function (serializedState) { return this.cache.restore(serializedState); }; ApolloClient.prototype.addResolvers = function (resolvers) { this.localState.addResolvers(resolvers); }; ApolloClient.prototype.setResolvers = function (resolvers) { this.localState.setResolvers(resolvers); }; ApolloClient.prototype.getResolvers = function () { return this.localState.getResolvers(); }; ApolloClient.prototype.setLocalStateFragmentMatcher = function (fragmentMatcher) { this.localState.setFragmentMatcher(fragmentMatcher); }; return ApolloClient; }()); exports.default = ApolloClient; //# sourceMappingURL=ApolloClient.js.map