UNPKG

apollo-client

Version:
71 lines 2.96 kB
"use strict"; var networkInterface_1 = require('./networkInterface'); exports.createNetworkInterface = networkInterface_1.createNetworkInterface; var store_1 = require('./store'); exports.createApolloStore = store_1.createApolloStore; exports.createApolloReducer = store_1.createApolloReducer; var QueryManager_1 = require('./QueryManager'); var readFromStore_1 = require('./data/readFromStore'); exports.readQueryFromStore = readFromStore_1.readQueryFromStore; exports.readFragmentFromStore = readFromStore_1.readFragmentFromStore; var isUndefined = require('lodash.isundefined'); var ApolloClient = (function () { function ApolloClient(_a) { var _this = this; var _b = _a === void 0 ? {} : _a, networkInterface = _b.networkInterface, reduxRootKey = _b.reduxRootKey, initialState = _b.initialState; this.watchQuery = function (options) { _this.initStore(); return _this.queryManager.watchQuery(options); }; this.query = function (options) { _this.initStore(); return _this.queryManager.query(options); }; this.mutate = function (options) { _this.initStore(); return _this.queryManager.mutate(options); }; this.middleware = function () { return function (store) { _this.setStore(store); return function (next) { return function (action) { var returnValue = next(action); _this.queryManager.broadcastNewStore(store.getState()); return returnValue; }; }; }; }; this.setStore = function (store) { if (isUndefined(store.getState()[_this.reduxRootKey])) { throw new Error("Existing store does not use apolloReducer for " + _this.reduxRootKey); } _this.store = store; _this.queryManager = new QueryManager_1.QueryManager({ networkInterface: _this.networkInterface, reduxRootKey: _this.reduxRootKey, store: store, }); }; this.reduxRootKey = reduxRootKey ? reduxRootKey : 'apollo'; this.initialState = initialState ? initialState : {}; this.networkInterface = networkInterface ? networkInterface : networkInterface_1.createNetworkInterface('/graphql'); } ApolloClient.prototype.reducer = function () { return store_1.createApolloReducer({}); }; ApolloClient.prototype.initStore = function () { if (this.store) { return; } this.setStore(store_1.createApolloStore({ reduxRootKey: this.reduxRootKey, initialState: this.initialState, })); }; ; return ApolloClient; }()); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = ApolloClient; //# sourceMappingURL=index.js.map