forked from apollographql/apollo-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.ts
188 lines (157 loc) · 4.67 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import {
createStore,
compose as reduxCompose,
applyMiddleware,
combineReducers,
Middleware,
} from 'redux';
import {
data,
} from './data/store';
import {
NormalizedCache,
} from './data/storeUtils';
import {
queries,
QueryStore,
} from './queries/store';
import {
mutations,
MutationStore,
} from './mutations/store';
import {
optimistic,
OptimisticStore,
getDataWithOptimisticResults,
} from './optimistic-data/store';
export { getDataWithOptimisticResults };
import {
ApolloAction,
} from './actions';
import {
IdGetter,
} from './core/types';
import {
CustomResolverMap,
} from './data/readFromStore';
import { assign } from './util/assign';
export interface Store {
data: NormalizedCache;
queries: QueryStore;
mutations: MutationStore;
optimistic: OptimisticStore;
reducerError: Error | null;
}
/**
* This is an interface that describes the behavior of a Apollo store, which is currently
* implemented through redux.
*/
export interface ApolloStore {
dispatch: (action: ApolloAction) => void;
// We don't know what this will return because it could have any number of custom keys when
// integrating with an existing store
getState: () => any;
}
const crashReporter = (store: any) => (next: any) => (action: any) => {
try {
return next(action);
} catch (err) {
console.error('Caught an exception!', err);
console.error(err.stack);
throw err;
}
};
export type ApolloReducer = (store: NormalizedCache, action: ApolloAction) => NormalizedCache;
export function createApolloReducer(config: ApolloReducerConfig): Function {
return function apolloReducer(state = {} as Store, action: ApolloAction) {
try {
const newState: Store = {
queries: queries(state.queries, action),
mutations: mutations(state.mutations, action),
// Note that we are passing the queries into this, because it reads them to associate
// the query ID in the result with the actual query
data: data(state.data, action, state.queries, state.mutations, config),
optimistic: [] as any,
reducerError: null,
};
// use the two lines below to debug tests :)
// console.log('ACTION', action.type, action);
// console.log('new state', newState);
// Note, we need to have the results of the
// APOLLO_MUTATION_INIT action to simulate
// the APOLLO_MUTATION_RESULT action. That's
// why we pass in newState
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 {
...state,
reducerError,
};
}
};
}
export function createApolloStore({
reduxRootKey = 'apollo',
initialState,
config = {},
reportCrashes = true,
logger,
}: {
reduxRootKey?: string,
initialState?: any,
config?: ApolloReducerConfig,
reportCrashes?: boolean,
logger?: Middleware,
} = {}): ApolloStore {
const enhancers: any[] = [];
const middlewares: Middleware[] = [];
if (reportCrashes) {
middlewares.push(crashReporter);
}
if (logger) {
middlewares.push(logger);
}
if (middlewares.length > 0) {
enhancers.push(applyMiddleware(...middlewares));
}
// Dev tools enhancer should be last
if (typeof window !== 'undefined') {
const anyWindow = window as any;
if (anyWindow.devToolsExtension) {
enhancers.push(anyWindow.devToolsExtension());
}
}
// XXX to avoid type fail
const compose: (...args: any[]) => () => any = reduxCompose;
// Note: The below checks are what make it OK for QueryManager to start from 0 when generating
// new query IDs. If we let people rehydrate query state for some reason, we would need to make
// sure newly generated IDs don't overlap with old queries.
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({ [reduxRootKey]: createApolloReducer(config) as any }), // XXX see why this type fails
initialState,
compose(...enhancers),
);
}
export type ApolloReducerConfig = {
dataIdFromObject?: IdGetter;
customResolvers?: CustomResolverMap;
};