Skip to content

Commit

Permalink
fix(polling #6): remove undocumented behaviour that causes queries wi…
Browse files Browse the repository at this point in the history
…th poll intervals to always make an initial network request on component mount
  • Loading branch information
Martin Denk committed Jul 21, 2021
1 parent e66bc96 commit 3a274fc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 35 deletions.
19 changes: 2 additions & 17 deletions lib/useQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,6 @@ var getQueryAst = function getQueryAst(queryAst, client, options) {
return options.pollInterval || options.fetchPolicy === 'cache-only' || options.reducedQuery === false ? queryAst : (0, _reducedQueries.makeReducedQueryAst)(client.cache, queryAst, options.variables);
};

var getFetchPolicy = function getFetchPolicy(reducedQueryAst, options) {
if (!reducedQueryAst) {
// If all the requested data is already in the cache, we can skip this query.
return 'cache-only';
} // Always fetch data from the server on component mount when polling is enabled.
// Polling indicates that fresh data is more important than caching, so prefer an extra
// request on mount rather than waiting the duration of the poll interval for the first poll
// request.


return options.pollInterval ? 'cache-and-network' : options.fetchPolicy;
};

var _default = function _default(query) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var cacheDataRef = (0, _react.useRef)();
Expand Down Expand Up @@ -87,10 +74,8 @@ var _default = function _default(query) {
// This toggles `loading` every time a polling request starts and completes. We need this
// for the effect hook to work.
notifyOnNetworkStatusChange: !!options.pollInterval,
fetchPolicy: getFetchPolicy(reducedQueryAst, options),
// This prevents polling queries to refetch data from the server each time the cache is
// mutated.
nextFetchPolicy: options.pollInterval ? 'cache-first' : options.nextFetchPolicy,
// If all the requested data is already in the cache, we can skip this query.
fetchPolicy: reducedQueryAst ? options.fetchPolicy : 'cache-only',
onCompleted: function onCompleted() {
// The reduced query is kept in state to avoid making another request if a request is
// already in flight and the cache contents change in the meantime. Once the request is
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "apollo-augmented-hooks",
"version": "1.11.1",
"version": "1.11.2",
"description": "Drop-in replacements for @apollo/client's useQuery, useMutation and useSubscription hooks with reduced overhead and additional functionality.",
"main": "lib/index.js",
"scripts": {
Expand Down
19 changes: 2 additions & 17 deletions src/useQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,6 @@ const getQueryAst = (queryAst, client, options) => (
: makeReducedQueryAst(client.cache, queryAst, options.variables)
);

const getFetchPolicy = (reducedQueryAst, options) => {
if (!reducedQueryAst) {
// If all the requested data is already in the cache, we can skip this query.
return 'cache-only';
}

// Always fetch data from the server on component mount when polling is enabled.
// Polling indicates that fresh data is more important than caching, so prefer an extra
// request on mount rather than waiting the duration of the poll interval for the first poll
// request.
return options.pollInterval ? 'cache-and-network' : options.fetchPolicy;
};

export default (query, options = {}) => {
const cacheDataRef = useRef();
const client = apolloClient();
Expand Down Expand Up @@ -60,10 +47,8 @@ export default (query, options = {}) => {
// This toggles `loading` every time a polling request starts and completes. We need this
// for the effect hook to work.
notifyOnNetworkStatusChange: !!options.pollInterval,
fetchPolicy: getFetchPolicy(reducedQueryAst, options),
// This prevents polling queries to refetch data from the server each time the cache is
// mutated.
nextFetchPolicy: options.pollInterval ? 'cache-first' : options.nextFetchPolicy,
// If all the requested data is already in the cache, we can skip this query.
fetchPolicy: reducedQueryAst ? options.fetchPolicy : 'cache-only',
onCompleted: () => {
// The reduced query is kept in state to avoid making another request if a request is
// already in flight and the cache contents change in the meantime. Once the request is
Expand Down

0 comments on commit 3a274fc

Please sign in to comment.