Skip to content
This repository has been archived by the owner on Jul 10, 2019. It is now read-only.

Commit

Permalink
Remove self managed store (#18)
Browse files Browse the repository at this point in the history
Redux store must now be created externally and passed to the constructor.
  • Loading branch information
rportugal authored Feb 27, 2018
1 parent 0df480f commit 3264c50
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 34 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ npm install apollo-cache-redux --save
After installing the package:
```js
import { ReduxCache, apolloReducer } from 'apollo-cache-redux';
import { createStore, combineReducers } from 'redux';
import { HttpLink } from 'apollo-link-http';
import ApolloClient from 'apollo-client';

Expand All @@ -30,7 +31,7 @@ const client = new ApolloClient({
```

The following options are accepted for `ReduxCache`:
* `store` (optional). An existing Redux store. If you don't want to pass in an existing store `ReduxCache` will create one for you.
* `store`. An existing Redux store. If you don't have one, please create it as per the example above.
* `reduxRootSelector` (optional). Customises the reducer name for the cache (default: `apollo`).
* Other options accepted by `InMemoryCache`, to customise the underlying `InMemoryCache` (e.g. `fragmentMatcher`).

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-cache-redux",
"version": "0.1.0-alpha.8",
"version": "0.1.0",
"description": "Redux cache for Apollo Client 2.x",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
Expand Down Expand Up @@ -66,4 +66,4 @@
"json"
]
}
}
}
26 changes: 8 additions & 18 deletions src/__tests__/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,26 @@ import { combineReducers, createStore } from 'redux';

import {ReduxCache} from '..';
import {ApolloReducerConfig, NormalizedCache} from 'apollo-cache-inmemory';
import {apolloReducer} from "../reducer";
import { apolloReducer } from "../reducer";

disableFragmentWarnings();

describe('Cache', () => {
describe.skip('instantiation', () => {
it('creates its own store if none is passed', () => {
});

it('allows the use of an existing store', () => {

});

it('allows the use of an existing store, with a custom reducer ', () => {
// const store = createStore(
// combineReducers({apollo: apolloReducer}),
// );
})
});

function createCache({
initialState,
config,
config
}: {
initialState?: any;
config?: ApolloReducerConfig;
} = {},): ApolloCache<NormalizedCache> {
const store = createStore(
combineReducers({
apollo: apolloReducer
})
);

return new ReduxCache(
config || {addTypename: false}
config ? { ...config, store } : {store, addTypename: false}
).restore(initialState ? initialState.apollo.data : {});
}

Expand Down
28 changes: 22 additions & 6 deletions src/__tests__/reduxNormalizedCache.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
import { ReduxNormalizedCache } from '../reduxNormalizedCache';
import { ReduxNormalizedCache, ReduxNormalizedCacheConfig } from '../reduxNormalizedCache';
import { NormalizedCacheObject } from 'apollo-cache-inmemory';
import { apolloReducer } from "../reducer";
import { combineReducers, createStore } from "redux";

describe('ReduxNormalizedCache', () => {
function createCache({
initialState
}: {
initialState?: any;
} = {}, ): ReduxNormalizedCache {
const store = createStore(
combineReducers({
apollo: apolloReducer
})
);

return new ReduxNormalizedCache({ store });
}

it('should create an empty cache', () => {
const cache = new ReduxNormalizedCache();
const cache = createCache();
expect(cache.toObject()).toEqual({});
});

it('should .replace() the store', () => {
const contents: NormalizedCacheObject = { a: {} };
const cache = new ReduxNormalizedCache();
const cache = createCache();
cache.replace(contents);
expect(cache.toObject()).toEqual(contents);
});

it(`should .get() an object from the store by dataId`, () => {
const contents: NormalizedCacheObject = { a: {} };
const cache = new ReduxNormalizedCache();
const cache = createCache();
cache.replace(contents);
expect(cache.get('a')).toBe(contents.a);
});

it(`should .set() an object from the store by dataId`, () => {
const obj = {};
const cache = new ReduxNormalizedCache();
const cache = createCache();
cache.set('a', obj);
expect(cache.get('a')).toEqual(obj);
});

it(`should .clear() the store`, () => {
const obj = {};
const cache = new ReduxNormalizedCache();
const cache = createCache();
cache.set('a', obj);
cache.clear();
expect(cache.get('a')).toBeUndefined();
Expand Down
2 changes: 1 addition & 1 deletion src/reduxCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {cloneDeep} from 'lodash';
export type ReduxCacheConfig = ApolloReducerConfig & ReduxNormalizedCacheConfig;

export class ReduxCache extends InMemoryCache {
constructor(config: ReduxCacheConfig = {}) {
constructor(config: ReduxCacheConfig) {
super(config);
// Overwrite the in-memory data object
this.data = reduxNormalizedCacheFactory(config);
Expand Down
9 changes: 3 additions & 6 deletions src/reduxNormalizedCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@ import {
APOLLO_WRITE
} from "./constants";
import {
combineReducers,
createStore,
Store
} from "redux";
import { apolloReducer } from "./reducer";

export interface ReduxNormalizedCacheConfig {
reduxRootSelector?: string
store?: Store<any>
store: Store<any>
}

export class ReduxNormalizedCache implements NormalizedCache {
private store: Store<any>;
private reduxRootSelector: string;

constructor(reduxCacheConfig: ReduxNormalizedCacheConfig = {}) {
constructor(reduxCacheConfig: ReduxNormalizedCacheConfig) {
this.reduxRootSelector = reduxCacheConfig.reduxRootSelector || 'apollo';
this.store = reduxCacheConfig.store || createStore(combineReducers({ [this.reduxRootSelector]: apolloReducer }));
this.store = reduxCacheConfig.store;
}
public toObject(): NormalizedCacheObject {
return this.getReducer();
Expand Down

0 comments on commit 3264c50

Please sign in to comment.