Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset store returns a promise #1674

Merged
merged 7 commits into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ Andrew E. Rhyne <rhyneandrew@gmail.com>
Miroslav Simulcik <simulcik.miro@gmail.com>
Stephen Potter <me@stevepotter.me>
Michaël De Boey <info@michaeldeboey.be>
Andreas Bergenwall <abergenw@gmail.com>
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Change log

### vNEXT
- Make ApolloClient.resetStore() and QueryManager.resetStore() return a promise instead of void [PR #1674](https://github.com/apollographql/apollo-client/pull/1674)
- Fix bug that caused errors in `writeToStore` to be rethrown as uncaught errors [PR #1673](https://github.com/apollographql/apollo-client/pull/1673)

- Feature: Pass a function to `optimisticResponse` and it will be called with the `variables` passed to the mutation.

### 1.2.2
Expand Down
6 changes: 2 additions & 4 deletions src/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,8 @@ export default class ApolloClient implements DataProxy {
* re-execute any queries then you should make sure to stop watching any
* active queries.
*/
public resetStore() {
if (this.queryManager) {
this.queryManager.resetStore();
}
public resetStore(): Promise<ApolloQueryResult<any>[]>|null {
return this.queryManager ? this.queryManager.resetStore() : null;
}

public getInitialState(): { data: Object } {
Expand Down
7 changes: 5 additions & 2 deletions src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ export class QueryManager {
}
}

public resetStore(): void {
public resetStore(): Promise<ApolloQueryResult<any>[]> {
// Before we have sent the reset action to the store,
// we can no longer rely on the results returned by in-flight
// requests since these may depend on values that previously existed
Expand All @@ -819,15 +819,18 @@ export class QueryManager {
// watched. If there is an existing query in flight when the store is reset,
// the promise for it will be rejected and its results will not be written to the
// store.
const observableQueryPromises: Promise<ApolloQueryResult<any>>[] = [];
Object.keys(this.observableQueries).forEach((queryId) => {
const storeQuery = this.reduxRootSelector(this.store.getState()).queries[queryId];

const fetchPolicy = this.observableQueries[queryId].observableQuery.options.fetchPolicy;

if (fetchPolicy !== 'cache-only' && fetchPolicy !== 'standby') {
this.observableQueries[queryId].observableQuery.refetch();
observableQueryPromises.push(this.observableQueries[queryId].observableQuery.refetch());
}
});

return Promise.all(observableQueryPromises);
}

public startQuery<T>(queryId: string, options: WatchQueryOptions, listener: QueryListener) {
Expand Down
92 changes: 92 additions & 0 deletions test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2343,6 +2343,98 @@ describe('QueryManager', () => {
});

describe('store resets', () => {
it('returns a promise resolving when all queries have been refetched', () => {
const query = gql`
query {
author {
firstName
lastName
}
}`;

const data = {
author: {
firstName: 'John',
lastName: 'Smith',
},
};

const dataChanged = {
author: {
firstName: 'John changed',
lastName: 'Smith',
},
};

const query2 = gql`
query {
author2 {
firstName
lastName
}
}`;

const data2 = {
author2: {
firstName: 'John',
lastName: 'Smith',
},
};

const data2Changed = {
author2: {
firstName: 'John changed',
lastName: 'Smith',
},
};

const queryManager = createQueryManager({
networkInterface: mockNetworkInterface(
{
request: {query},
result: {data},
},
{
request: {query: query2},
result: {data: data2},
},
{
request: {query},
result: {data: dataChanged},
},
{
request: {query: query2},
result: {data: data2Changed},
},
),
});

const observable = queryManager.watchQuery<any>({ query });
const observable2 = queryManager.watchQuery<any>({ query: query2 });

return Promise.all([
observableToPromise({ observable },
result => assert.deepEqual(result.data, data),
),
observableToPromise({ observable: observable2 },
result => assert.deepEqual(result.data, data2),
),
]).then(() => {
observable.subscribe({ next: () => null });
observable2.subscribe({ next: () => null });

return queryManager.resetStore().then(() => {
const result = queryManager.getCurrentQueryResult(observable);
assert.isFalse(result.partial);
assert.deepEqual(result.data, dataChanged);

const result2 = queryManager.getCurrentQueryResult(observable2);
assert.isFalse(result2.partial);
assert.deepEqual(result2.data, data2Changed);
});
});
});

it('should change the store state to an empty state', () => {
const queryManager = createQueryManager({});

Expand Down