UNPKG

apollo-client-cors-hack

Version:

A simple yet functional GraphQL client.

229 lines 10.5 kB
var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; import { createNetworkInterface, } from './transport/networkInterface'; import { HeuristicFragmentMatcher, } from './data/fragmentMatcher'; import { createApolloStore, createApolloReducer, } from './store'; import { QueryManager, } from './core/QueryManager'; import { isProduction, } from './util/environment'; import { storeKeyNameFromFieldNameAndArgs, } from './data/storeUtils'; import { ReduxDataProxy, } from './data/proxy'; import { version, } from './version'; var DEFAULT_REDUX_ROOT_KEY = 'apollo'; function defaultReduxRootSelector(state) { return state[DEFAULT_REDUX_ROOT_KEY]; } function defaultDataIdFromObject(result) { if (result.__typename) { if (result.id !== undefined) { return result.__typename + ":" + result.id; } if (result._id !== undefined) { return result.__typename + ":" + result._id; } } return null; } var hasSuggestedDevtools = false; var ApolloClient = (function () { function ApolloClient(options) { if (options === void 0) { options = {}; } var _this = this; this.middleware = function () { return function (store) { _this.setStore(store); return function (next) { return function (action) { var previousApolloState = _this.queryManager.selectApolloState(store); var returnValue = next(action); var newApolloState = _this.queryManager.selectApolloState(store); if (newApolloState !== previousApolloState) { _this.queryManager.broadcastNewStore(store.getState()); } if (_this.devToolsHookCb) { _this.devToolsHookCb({ action: action, state: _this.queryManager.getApolloState(), dataWithOptimisticResults: _this.queryManager.getDataWithOptimisticResults(), }); } return returnValue; }; }; }; }; var dataIdFromObject = options.dataIdFromObject; var networkInterface = options.networkInterface, reduxRootSelector = options.reduxRootSelector, initialState = options.initialState, _a = options.ssrMode, ssrMode = _a === void 0 ? false : _a, _b = options.ssrForceFetchDelay, ssrForceFetchDelay = _b === void 0 ? 0 : _b, _c = options.addTypename, addTypename = _c === void 0 ? true : _c, customResolvers = options.customResolvers, connectToDevTools = options.connectToDevTools, fragmentMatcher = options.fragmentMatcher, _d = options.queryDeduplication, queryDeduplication = _d === void 0 ? true : _d; if (typeof reduxRootSelector === 'function') { this.reduxRootSelector = reduxRootSelector; } else if (typeof reduxRootSelector !== 'undefined') { throw new Error('"reduxRootSelector" must be a function.'); } if (typeof fragmentMatcher === 'undefined') { this.fragmentMatcher = new HeuristicFragmentMatcher(); } else { this.fragmentMatcher = fragmentMatcher; } this.initialState = initialState ? initialState : {}; this.networkInterface = networkInterface ? networkInterface : createNetworkInterface({ uri: '/graphql' }); this.addTypename = addTypename; this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0; this.dataId = dataIdFromObject = dataIdFromObject || defaultDataIdFromObject; this.dataIdFromObject = this.dataId; this.fieldWithArgs = storeKeyNameFromFieldNameAndArgs; this.queryDeduplication = queryDeduplication; this.ssrMode = ssrMode; if (ssrForceFetchDelay) { setTimeout(function () { return _this.disableNetworkFetches = false; }, ssrForceFetchDelay); } this.reducerConfig = { dataIdFromObject: dataIdFromObject, customResolvers: customResolvers, addTypename: addTypename, fragmentMatcher: this.fragmentMatcher.match, }; this.watchQuery = this.watchQuery.bind(this); this.query = this.query.bind(this); this.mutate = this.mutate.bind(this); this.setStore = this.setStore.bind(this); this.resetStore = this.resetStore.bind(this); var defaultConnectToDevTools = !isProduction() && typeof window !== 'undefined' && (!window.__APOLLO_CLIENT__); if (typeof connectToDevTools === 'undefined' ? defaultConnectToDevTools : connectToDevTools) { window.__APOLLO_CLIENT__ = this; } if (!hasSuggestedDevtools && !isProduction()) { hasSuggestedDevtools = true; if (typeof window !== 'undefined' && window.document && window.top === window.self) { if (typeof window.__APOLLO_DEVTOOLS_GLOBAL_HOOK__ === 'undefined') { if (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; } ApolloClient.prototype.watchQuery = function (options) { this.initStore(); if (this.disableNetworkFetches && options.fetchPolicy === 'network-only') { options = __assign({}, options, { fetchPolicy: 'cache-first' }); } return this.queryManager.watchQuery(options); }; ApolloClient.prototype.query = function (options) { this.initStore(); if (options.fetchPolicy === 'cache-and-network') { throw new Error('cache-and-network fetchPolicy can only be used with watchQuery'); } if (this.disableNetworkFetches && options.fetchPolicy === 'network-only') { options = __assign({}, options, { fetchPolicy: 'cache-first' }); } return this.queryManager.query(options); }; ApolloClient.prototype.mutate = function (options) { this.initStore(); return this.queryManager.mutate(options); }; ApolloClient.prototype.subscribe = function (options) { this.initStore(); return this.queryManager.startGraphQLSubscription(options); }; ApolloClient.prototype.readQuery = function (options) { return this.initProxy().readQuery(options); }; ApolloClient.prototype.readFragment = function (options) { return this.initProxy().readFragment(options); }; ApolloClient.prototype.writeQuery = function (options) { return this.initProxy().writeQuery(options); }; ApolloClient.prototype.writeFragment = function (options) { return this.initProxy().writeFragment(options); }; ApolloClient.prototype.reducer = function () { return createApolloReducer(this.reducerConfig); }; ApolloClient.prototype.__actionHookForDevTools = function (cb) { this.devToolsHookCb = cb; }; ApolloClient.prototype.initStore = function () { var _this = this; if (this.store) { return; } if (this.reduxRootSelector) { throw new Error('Cannot initialize the store because "reduxRootSelector" is provided. ' + 'reduxRootSelector should only be used when the store is created outside of the client. ' + 'This may lead to unexpected results when querying the store internally. ' + "Please remove that option from ApolloClient constructor."); } this.setStore(createApolloStore({ reduxRootKey: DEFAULT_REDUX_ROOT_KEY, initialState: this.initialState, config: this.reducerConfig, logger: function (store) { return function (next) { return function (action) { var result = next(action); if (_this.devToolsHookCb) { _this.devToolsHookCb({ action: action, state: _this.queryManager.getApolloState(), dataWithOptimisticResults: _this.queryManager.getDataWithOptimisticResults(), }); } return result; }; }; }, })); }; ApolloClient.prototype.resetStore = function () { if (this.queryManager) { this.queryManager.resetStore(); } }; ApolloClient.prototype.getInitialState = function () { this.initStore(); return this.queryManager.getInitialState(); }; ApolloClient.prototype.setStore = function (store) { var reduxRootSelector; if (this.reduxRootSelector) { reduxRootSelector = this.reduxRootSelector; } else { reduxRootSelector = defaultReduxRootSelector; } if (typeof reduxRootSelector(store.getState()) === 'undefined') { throw new Error('Existing store does not use apolloReducer. Please make sure the store ' + 'is properly configured and "reduxRootSelector" is correctly specified.'); } this.store = store; this.queryManager = new QueryManager({ networkInterface: this.networkInterface, reduxRootSelector: reduxRootSelector, store: store, addTypename: this.addTypename, reducerConfig: this.reducerConfig, queryDeduplication: this.queryDeduplication, fragmentMatcher: this.fragmentMatcher, ssrMode: this.ssrMode, }); }; ApolloClient.prototype.initProxy = function () { if (!this.proxy) { this.initStore(); this.proxy = new ReduxDataProxy(this.store, this.reduxRootSelector || defaultReduxRootSelector, this.fragmentMatcher, this.reducerConfig); } return this.proxy; }; return ApolloClient; }()); export default ApolloClient; //# sourceMappingURL=ApolloClient.js.map