Skip to content

Commit

Permalink
Merge pull request #1455 from cesarsolorzano/master
Browse files Browse the repository at this point in the history
Make updateQuery option of subscribeToMore optional
  • Loading branch information
helfer authored Mar 21, 2017
2 parents 7d3cc53 + 3562838 commit 3ef7cdf
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Expect active development and potentially significant breaking changes in the `0
### vNEXT
- Added `HTTPBatchedNetworkInterface` as an index export to make it easier
to subclass externally, consistent with `HTTPFetchNetworkInterface`. [PR #1446](https://github.com/apollographql/apollo-client/pull/1446)

- Make `updateQuery` option of `subscribeToMore` optional [PR #1455](https://github.com/apollographql/apollo-client/pull/1455)

### 1.0.0-rc.5
- Fix: Revert PR that caused uncaught promise rejections [PR #1133](https://github.com/apollographql/apollo-client/pull/1133)
Expand Down
23 changes: 12 additions & 11 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,19 +262,20 @@ export class ObservableQuery<T> extends Observable<ApolloQueryResult<T>> {
variables: options.variables,
});

const reducer = options.updateQuery;

const subscription = observable.subscribe({
next: (data) => {
const mapFn = (previousResult: Object, { variables }: { variables: Object }) => {
return reducer(
previousResult, {
subscriptionData: { data },
variables,
},
);
};
this.updateQuery(mapFn);
if (options.updateQuery) {
const reducer = options.updateQuery;
const mapFn = (previousResult: Object, { variables }: { variables: Object }) => {
return reducer(
previousResult, {
subscriptionData: { data },
variables,
},
);
};
this.updateQuery(mapFn);
}
},
error: (err) => {
if (options.onError) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/watchQueryOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export interface FetchMoreQueryOptions {
export type SubscribeToMoreOptions = {
document: DocumentNode;
variables?: { [key: string]: any };
updateQuery: (previousQueryResult: Object, options: {
updateQuery?: (previousQueryResult: Object, options: {
subscriptionData: { data: any },
variables: { [key: string]: any },
}) => Object;
Expand Down
66 changes: 66 additions & 0 deletions test/subscribeToMore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ describe('subscribeToMore', () => {
results: [...results3],
};

const results4 = ['Vyacheslav Kim', 'Changping Chen'].map(
name => ({ result: { name: name }, delay: 10 }),
);

const sub4 = {
request: {
query: gql`
subscription newValues {
name
}
`,
},
id: 0,
results: [...results4],
};

it('triggers new result from subscription data', (done) => {
let latestResult: any = null;
const networkInterface = mockSubscriptionNetworkInterface([sub1], req1);
Expand Down Expand Up @@ -226,5 +242,55 @@ describe('subscribeToMore', () => {
}
});

it('updates new result from subscription via a reducer in watchQuery options', (done) => {
let latestResult: any = null;
const networkInterface = mockSubscriptionNetworkInterface([sub4], req1);
let counter = 0;

const client = new ApolloClient({
networkInterface,
addTypename: false,
});

const obsHandle = client.watchQuery({
query,
reducer: (previousResult, action) => {
if (action.type === 'APOLLO_SUBSCRIPTION_RESULT' && action.operationName === 'newValues') {
if (action.result.data) {
return { entry: { value: action.result.data.name } };
}
}
return previousResult;
},
});
const sub = obsHandle.subscribe({
next(queryResult) {
latestResult = queryResult;
counter++;
},
});

obsHandle.subscribeToMore({
document: gql`
subscription newValues {
name
}
`,
});

setTimeout(() => {
sub.unsubscribe();
assert.equal(counter, 3);
assert.deepEqual(
latestResult,
{ data: { entry: { value: 'Changping Chen' } }, loading: false, networkStatus: 7, stale: false },
);
done();
}, 50);

for (let i = 0; i < 2; i++) {
networkInterface.fireResult(0); // 0 is the id of the subscription for the NI
}
});
// TODO add a test that checks that subscriptions are cancelled when obs is unsubscribed from.
});

0 comments on commit 3ef7cdf

Please sign in to comment.