-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathstate.js
52 lines (48 loc) · 1.67 KB
/
state.js
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
/**
* Setup apollo-link-state
* Apollo-link-state helps to manage a local store for caching and client-side
* data storing
* It replaces previous implementation using redux
* Link state doc:
* @see https://www.apollographql.com/docs/react/essentials/local-state.html
* @see https://www.apollographql.com/docs/link/links/state.html
* General presentation on Links
* @see https://www.apollographql.com/docs/link/
* Example
* @see https://hackernoon.com/storing-local-state-in-react-with-apollo-link-state-738f6ca45569
*/
import { withClientState } from 'apollo-link-state';
/**
* Create a state link
* TODO: Deprecated
*/
export const createStateLink = ({ cache, resolvers, defaults, ...otherOptions }) => {
const stateLink = withClientState({
cache,
defaults: defaults || getStateLinkDefaults(),
resolvers: resolvers || getStateLinkResolvers(),
...otherOptions,
});
return stateLink;
};
// enhancement workflow
const registeredDefaults = {};
/**
* Defaults are default response to queries
*/
export const registerStateLinkDefault = ({ name, defaultValue, options = {} }) => {
registeredDefaults[name] = defaultValue;
return registeredDefaults;
};
export const getStateLinkDefaults = () => registeredDefaults;
// Mutation are equivalent to a Redux Action + Reducer
// except it uses GraphQL to retrieve/update data in the cache
const registeredMutations = {};
export const registerStateLinkMutation = ({ name, mutation, options = {} }) => {
registeredMutations[name] = mutation;
return registeredMutations;
};
export const getStateLinkMutations = () => registeredMutations;
export const getStateLinkResolvers = () => ({
Mutation: getStateLinkMutations(),
});