Skip to content

Commit

Permalink
allow unknown query in refetchQueries, warn but continue
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonphillips committed Sep 27, 2016
1 parent 089a991 commit 00a4f8a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
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) {
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
70 changes: 70 additions & 0 deletions test/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3206,6 +3206,76 @@ describe('QueryManager', () => {
},
});
});

it('should warn but continue when an unknown query name is asked to refetch', (done) => {
const oldWarn = console.warn;
let warned: any;
console.warn = (...args: any[]) => {
warned = args;
};

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');
console.warn = oldWarn;
done();
}
resultsReceived++;
},
});
});
});

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

0 comments on commit 00a4f8a

Please sign in to comment.