apollo-client
Version:
A simple yet functional GraphQL client.
80 lines • 3.36 kB
JavaScript
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 { createStore, compose as reduxCompose, applyMiddleware, combineReducers, } from 'redux';
import { data, } from './data/store';
import { queries, } from './queries/store';
import { mutations, } from './mutations/store';
import { optimistic, getDataWithOptimisticResults, } from './optimistic-data/store';
export { getDataWithOptimisticResults };
var crashReporter = function (store) { return function (next) { return function (action) {
try {
return next(action);
}
catch (err) {
console.error('Caught an exception!', err);
console.error(err.stack);
throw err;
}
}; }; };
export function createApolloReducer(config) {
return function apolloReducer(state, action) {
if (state === void 0) { state = {}; }
try {
var newState = {
queries: queries(state.queries, action),
mutations: mutations(state.mutations, action),
data: data(state.data, action, state.queries, state.mutations, config),
optimistic: [],
reducerError: null,
};
newState.optimistic = optimistic(state.optimistic, action, newState, config);
if (state.data === newState.data &&
state.mutations === newState.mutations &&
state.queries === newState.queries &&
state.optimistic === newState.optimistic &&
state.reducerError === newState.reducerError) {
return state;
}
return newState;
}
catch (reducerError) {
return __assign({}, state, { reducerError: reducerError });
}
};
}
export function createApolloStore(_a) {
var _b = _a === void 0 ? {} : _a, _c = _b.reduxRootKey, reduxRootKey = _c === void 0 ? 'apollo' : _c, initialState = _b.initialState, _d = _b.config, config = _d === void 0 ? {} : _d, _e = _b.reportCrashes, reportCrashes = _e === void 0 ? true : _e, logger = _b.logger;
var enhancers = [];
var middlewares = [];
if (reportCrashes) {
middlewares.push(crashReporter);
}
if (logger) {
middlewares.push(logger);
}
if (middlewares.length > 0) {
enhancers.push(applyMiddleware.apply(void 0, middlewares));
}
if (typeof window !== 'undefined') {
var anyWindow = window;
if (anyWindow.devToolsExtension) {
enhancers.push(anyWindow.devToolsExtension());
}
}
var compose = reduxCompose;
if (initialState && initialState[reduxRootKey] && initialState[reduxRootKey]['queries']) {
throw new Error('Apollo initial state may not contain queries, only data');
}
if (initialState && initialState[reduxRootKey] && initialState[reduxRootKey]['mutations']) {
throw new Error('Apollo initial state may not contain mutations, only data');
}
return createStore(combineReducers((_f = {}, _f[reduxRootKey] = createApolloReducer(config), _f)), initialState, compose.apply(void 0, enhancers));
var _f;
}
//# sourceMappingURL=store.js.map