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

Commit

Permalink
[RFR] Extracted realtime saga to its own package
Browse files Browse the repository at this point in the history
- extracted realtime saga and refactored it to [aor-realtime](https://github.com/marmelab/aor-realtime)
Fixes #29

- Allow to override default options for apollo watchRequest at the saga level
Fixes #31
  • Loading branch information
djhi committed May 9, 2017
1 parent 456678e commit 9ffebda
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 292 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"prettier": "~1.3.1"
},
"dependencies": {
"aor-realtime": "0.0.1",
"apollo-client": "~1.2.0",
"babel-plugin-transform-runtime": "~6.23.0",
"graphql": "~0.9.6",
Expand Down
8 changes: 0 additions & 8 deletions src/realtime/buildAorAction.js

This file was deleted.

15 changes: 0 additions & 15 deletions src/realtime/createApolloQueryChannel.js

This file was deleted.

32 changes: 0 additions & 32 deletions src/realtime/createApolloQueryChannel.spec.js

This file was deleted.

12 changes: 7 additions & 5 deletions src/realtime/getWatchOptionsForQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,22 @@ const knownApolloOptions = [
/**
* Get options for calling ApolloClient.watchQuery.
* @param apolloWatchOptions {Object} Options supplied by user
* @param action {Object} Action from admin-on-rest
* @param fetchType {String} The fetch type from admin-on-rest (GET_LIST, GET_ONE, etc.)
* @param resource {String} The resource from admin-on-rest
* @param params {Object} The paremeters for the query from admin-on-rest
*/
export default (apolloWatchOptions, { meta: { fetch, resource } }) => {
export default (apolloWatchOptions, fetchType, resource, params) => {
const options = [];
let tmpOptions = getOrCall(apolloWatchOptions, resource, fetch);
let tmpOptions = getOrCall(apolloWatchOptions, resource, fetchType, params);
options.push(tmpOptions);

if (tmpOptions) {
options.push(tmpOptions);
tmpOptions = getOrCall(tmpOptions[resource], fetch);
tmpOptions = getOrCall(tmpOptions[resource], fetchType, params);

if (tmpOptions) {
options.push(tmpOptions);
tmpOptions = getOrCall(tmpOptions[fetch]);
tmpOptions = getOrCall(tmpOptions[fetchType], params);

if (tmpOptions) {
options.push(tmpOptions);
Expand Down
12 changes: 7 additions & 5 deletions src/realtime/getWatchOptionsForQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import merge from 'lodash.merge';
import getWatchOptionsForQuery, { defaultWatchOptions } from './getWatchOptionsForQuery';

describe('getWatchOptionsForQuery', () => {
const action = { meta: { resource: 'Post', fetch: 'GET_LIST' } };
const fetchType = 'GET_LIST';
const resource = 'Post';
const params = {};

it('returns the default options when no options were specified', () => {
const options = getWatchOptionsForQuery(undefined, action);
const options = getWatchOptionsForQuery(undefined, fetchType, resource, params);

expect(options).toEqual(defaultWatchOptions);
});
Expand All @@ -17,7 +19,7 @@ describe('getWatchOptionsForQuery', () => {
pollInterval: 10000,
noFetch: true,
};
const options = getWatchOptionsForQuery(userOptions, action);
const options = getWatchOptionsForQuery(userOptions, fetchType, resource, params);

expect(options).toEqual(merge({}, defaultWatchOptions, userOptions));
});
Expand All @@ -35,7 +37,7 @@ describe('getWatchOptionsForQuery', () => {
...rootOptions,
Post: postOptions,
};
const options = getWatchOptionsForQuery(userOptions, action);
const options = getWatchOptionsForQuery(userOptions, fetchType, resource, params);

expect(options).toEqual(merge({}, defaultWatchOptions, rootOptions, postOptions));
});
Expand All @@ -60,7 +62,7 @@ describe('getWatchOptionsForQuery', () => {
GET_LIST: verbOptions,
},
};
const options = getWatchOptionsForQuery(userOptions, action);
const options = getWatchOptionsForQuery(userOptions, fetchType, resource, params);

expect(options).toEqual(merge({}, defaultWatchOptions, rootOptions, postOptions, verbOptions));
});
Expand Down
15 changes: 13 additions & 2 deletions src/realtime/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import sagaFactory from './saga';
import realtimeSaga from 'aor-realtime';
import { fork } from 'redux-saga/effects';

export default apolloConfiguredClient => sagaFactory(apolloConfiguredClient);
import getWatchOptionsForQuery from './getWatchOptionsForQuery';

export default (apolloConfiguredClient, apolloWatchOptions) =>
function* aorGraphQlSaga() {
const observeQuery = (fetchType, resource, params) => {
const watchOptions = getWatchOptionsForQuery(apolloWatchOptions, fetchType, resource, params);
return apolloConfiguredClient.watchRequest(fetchType, resource, params, watchOptions);
};

yield fork(realtimeSaga(observeQuery));
};
13 changes: 0 additions & 13 deletions src/realtime/queryObserver.js

This file was deleted.

29 changes: 0 additions & 29 deletions src/realtime/queryObserver.spec.js

This file was deleted.

41 changes: 0 additions & 41 deletions src/realtime/saga.js

This file was deleted.

Loading

0 comments on commit 9ffebda

Please sign in to comment.