Skip to content

Commit

Permalink
Make offline support optional (#33)
Browse files Browse the repository at this point in the history
* Make offline support optional

* Bump apollo-client
  • Loading branch information
manueliglesias authored Feb 2, 2018
1 parent baef3ff commit 4c1e5ee
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 76 deletions.
63 changes: 49 additions & 14 deletions packages/aws-appsync/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/aws-appsync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"dependencies": {
"@redux-offline/redux-offline": "^2.2.1",
"apollo-cache-inmemory": "^1.0.0",
"apollo-client": "^2.0.3",
"apollo-client": "^2.2.1",
"apollo-link": "^1.0.0",
"apollo-link-context": "^1.0.0",
"apollo-link-http": "^1.0.0",
Expand Down
78 changes: 17 additions & 61 deletions packages/aws-appsync/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,17 @@
* KIND, express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
import ApolloClient, { ApolloClientOptions, MutationOptions } from 'apollo-client';
import { NormalizedCache } from 'apollo-cache-inmemory';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloLink, FetchResult } from 'apollo-link';
import { HttpLink } from 'apollo-link-http';
import { getMainDefinition, getOperationDefinition, variablesInOperation } from 'apollo-utilities';

import { Action, applyMiddleware, createStore, compose, combineReducers, Store } from 'redux';
import { offline } from '@redux-offline/redux-offline';
import offlineConfig from '@redux-offline/redux-offline/lib/defaults';
import thunk from 'redux-thunk';

import InMemoryCache, { reducer as cacheReducer, NORMALIZED_CACHE_KEY } from './cache/index';
import OfflineCache from './cache/index';
import { OfflineLink, AuthLink, NonTerminatingHttpLink, SubscriptionHandshakeLink, ComplexObjectLink } from './link';
import { reducer as commitReducer, offlineEffect, discard } from './link/offline-link';

/**
*
* @param {Function} persistCallback
* @param {*} effect
* @returns {Store<NormalizedCache>}
*/
const newStore = (persistCallback = () => null, effect, discard) => {
return createStore(
combineReducers({
rehydrated: (state = false, action) => {
switch (action.type) {
case 'REHYDRATE_STORE':
return true;
default:
return state;
}
},
...cacheReducer(),
...commitReducer(),
}),
window && window.__REDUX_DEVTOOLS_EXTENSION__ ? window.__REDUX_DEVTOOLS_EXTENSION__() : {},
compose(
applyMiddleware(thunk),
offline({
...offlineConfig,
persistCallback,
persistOptions: {
whitelist: [NORMALIZED_CACHE_KEY, 'offline']
},
effect,
discard,
})
)
);
};
import { createStore } from './store';

class AWSAppSyncClient extends ApolloClient {

/**
* @type {Store<{}>}
* @private
*/
aasStore;

/**
* @type {Promise<AWSAppSyncClient>}
*/
Expand All @@ -77,30 +30,31 @@ class AWSAppSyncClient extends ApolloClient {
* @param {string} url
* @param {ApolloClientOptions<InMemoryCache>} options
*/
constructor({ url, region, auth, conflictResolver, complexObjectsCredentials }, options) {
constructor({ url, region, auth, conflictResolver, complexObjectsCredentials, disableOffline = false }, options) {
if (!url || !region || !auth) {
throw new Error(`
In order to initialize AWSAppSyncClient, you must specify url, region and auth properties on the config object.
`);
throw new Error(
'In order to initialize AWSAppSyncClient, you must specify url, region and auth properties on the config object.'
);
}

let res;
this.hydratedPromise = new Promise((resolve, reject) => {
res = resolve;
});

const store = newStore(
const store = disableOffline ? null : createStore(
this,
() => {
this.aasStore.dispatch({ type: 'REHYDRATE_STORE' });
store.dispatch({ type: 'REHYDRATE_STORE' });
res(this);
},
(effect, action) => offlineEffect(this, effect, action),
discard(conflictResolver),
conflictResolver,
);
const cache = new InMemoryCache(store);
const cache = disableOffline ? new InMemoryCache() : new OfflineCache(store);

const passthrough = (op, forward) => (forward ? forward(op) : Observable.of());
let link = ApolloLink.from([
new OfflineLink(store),
disableOffline ? passthrough : new OfflineLink(store),
new ComplexObjectLink(complexObjectsCredentials),
new AuthLink({ url, region, auth }),
ApolloLink.split(
Expand All @@ -127,7 +81,9 @@ class AWSAppSyncClient extends ApolloClient {

super(newOptions);

this.aasStore = store;
if (disableOffline) {
res(this);
}
}

/**
Expand Down
48 changes: 48 additions & 0 deletions packages/aws-appsync/src/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Action, applyMiddleware, createStore, compose, combineReducers, Store } from 'redux';
import { offline } from '@redux-offline/redux-offline';
import offlineConfig from '@redux-offline/redux-offline/lib/defaults';
import thunk from 'redux-thunk';

import { AWSAppSyncClient } from './client';
import { reducer as cacheReducer, NORMALIZED_CACHE_KEY } from './cache/index';
import { reducer as commitReducer, offlineEffect, discard } from './link/offline-link';

/**
*
* @param {AWSAppSyncClient} client
* @param {Function} persistCallback
* @param {Function} conflictResolver
*/
const newStore = (client, persistCallback = () => null, conflictResolver) => {
return createStore(
combineReducers({
rehydrated: (state = false, action) => {
switch (action.type) {
case 'REHYDRATE_STORE':
return true;
default:
return state;
}
},
...cacheReducer(),
...commitReducer(),
}),
typeof window !== 'undefined' && window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__(),
compose(
applyMiddleware(thunk),
offline({
...offlineConfig,
persistCallback,
persistOptions: {
whitelist: [NORMALIZED_CACHE_KEY, 'offline']
},
effect: (effect, action) => offlineEffect(client, effect, action),
discard: discard(conflictResolver),
})
)
);
};

export {
newStore as createStore
}

0 comments on commit 4c1e5ee

Please sign in to comment.