Skip to content

Commit

Permalink
Merge pull request #700 from jasonphillips/refetchQueries-warning
Browse files Browse the repository at this point in the history
allow unknown query in refetchQueries, warn but continue
  • Loading branch information
helfer authored Sep 28, 2016
2 parents 089a991 + 4d87aa3 commit 4a018c1
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Expect active development and potentially significant breaking changes in the `0.x` track. We'll try to be diligent about releasing a `1.0` version in a timely fashion (ideally within 3 to 6 months), to signal the start of a more stable API.

### vNEXT
- Fix: Warn but do not fail when refetchQueries includes an unknown query name [PR #700](https://github.com/apollostack/apollo-client/pull/700)

### v0.4.19
- Fix: set default reduxRootKey for backwards-compatibility when using ApolloClient as middleware [PR #688](https://github.com/apollostack/apollo-client/pull/688)
Expand Down
12 changes: 9 additions & 3 deletions src/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,15 @@ export class QueryManager {
// Refetches a query given that query's name. Refetches
// all ObservableQuery instances associated with the query name.
private refetchQueryByName(queryName: string) {
this.queryIdsByName[queryName].forEach((queryId) => {
this.observableQueries[queryId].observableQuery.refetch();
});
const refetchedQueries = this.queryIdsByName[queryName];
// Warn if the query named does not exist (misnamed, or merely not yet fetched)
if (refetchedQueries === undefined) {
console.warn(`Warning: unknown query with name ${queryName} asked to refetch`);
} else {
refetchedQueries.forEach((queryId) => {
this.observableQueries[queryId].observableQuery.refetch();
});
}
}

// check to see if two results are the same, given our resultComparator
Expand Down
156 changes: 156 additions & 0 deletions test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3145,6 +3145,22 @@ describe('QueryManager', () => {
});

describe('refetchQueries', () => {
const oldWarn = console.warn;
let warned: any;
let timesWarned = 0;

beforeEach((done) => {
// clear warnings
warned = null;
timesWarned = 0;
// mock warn method
console.warn = (...args: any[]) => {
warned = args;
timesWarned++;
};
done();
});

it('should refetch the right query when a result is successfully returned', (done) => {
const mutation = gql`
mutation changeAuthorName {
Expand Down Expand Up @@ -3206,6 +3222,146 @@ describe('QueryManager', () => {
},
});
});

it('should warn but continue when an unknown query name is asked to refetch', (done) => {
const mutation = gql`
mutation changeAuthorName {
changeAuthorName(newName: "Jack Smith") {
firstName
lastName
}
}`;
const mutationData = {
changeAuthorName: {
firstName: 'Jack',
lastName: 'Smith',
},
};
const query = gql`
query getAuthors {
author {
firstName
lastName
}
}`;
const data = {
author: {
firstName: 'John',
lastName: 'Smith',
},
};
const secondReqData = {
author: {
firstName: 'Jane',
lastName: 'Johnson',
},
};
const queryManager = mockQueryManager(
{
request: { query },
result: { data },
},
{
request: { query },
result: { data: secondReqData },
},
{
request: { query: mutation },
result: { data: mutationData },
}
);
let resultsReceived = 0;
queryManager.watchQuery({ query }).subscribe({
next(result) {
if (resultsReceived === 0) {
assert.deepEqual(result.data, data);
queryManager.mutate({ mutation, refetchQueries: ['fakeQuery', 'getAuthors'] });
} else if (resultsReceived === 1) {
assert.deepEqual(result.data, secondReqData);
assert.include(warned[0], 'Warning: unknown query with name fakeQuery');
assert.equal(timesWarned, 1);
done();
}
resultsReceived++;
},
});
});

it('should ignore without warning a query name that is asked to refetch with no active subscriptions', (done) => {
const mutation = gql`
mutation changeAuthorName {
changeAuthorName(newName: "Jack Smith") {
firstName
lastName
}
}`;
const mutationData = {
changeAuthorName: {
firstName: 'Jack',
lastName: 'Smith',
},
};
const query = gql`
query getAuthors {
author {
firstName
lastName
}
}`;
const data = {
author: {
firstName: 'John',
lastName: 'Smith',
},
};
const secondReqData = {
author: {
firstName: 'Jane',
lastName: 'Johnson',
},
};
const queryManager = mockQueryManager(
{
request: { query },
result: { data },
},
{
request: { query },
result: { data: secondReqData },
},
{
request: { query: mutation },
result: { data: mutationData },
}
);

let resultsReceived = 0;
const temporarySubscription = queryManager.watchQuery({ query }).subscribe({
next(result) {
if (resultsReceived === 0) {
assert.deepEqual(result.data, data);
// unsubscribe before the mutation
temporarySubscription.unsubscribe();
queryManager.mutate({ mutation, refetchQueries: ['getAuthors'] });
} else if (resultsReceived === 1) {
// should not be subscribed
done(new Error('Returned data when it should have been unsubscribed.'));
}
resultsReceived++;
},
});
// no warning should have been fired
setTimeout(() => {
assert.equal(timesWarned, 0);
done();
}, 10);
});

afterEach((done) => {
// restore standard method
console.warn = oldWarn;
done();
});
});

describe('result transformation', () => {
Expand Down

0 comments on commit 4a018c1

Please sign in to comment.