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

Stop blocking duplicate renders based on skip settings #6587

Merged
merged 2 commits into from
Jul 13, 2020
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@
- Expand `ApolloError` typings to include `ServerError` and `ServerParseError`. <br/>
[@dmarkow](https://github.com/dmarkow) in [#6319](https://github.com/apollographql/apollo-client/pull/6319)

- Fast responses received over the link chain will no longer conflict with `skip` settings. <br/>
[@hwillson](https://github.com/hwillson) in [#6587](https://github.com/apollographql/apollo-client/pull/6587)

## Apollo Client 2.6.8

### Apollo Client (2.6.8)
Expand Down
12 changes: 0 additions & 12 deletions src/react/data/QueryData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,6 @@ export class QueryData<TData, TVariables> extends OperationData {
return;
}

// If we skipped previously, `previousResult.data` is set to undefined.
// When this subscription is run after skipping, Apollo Client sends
// the last query result data alongside the `loading` true state. This
// means the previous skipped `data` of undefined and the incoming
// data won't match, which would normally mean we want to trigger a
// render to show the new data. In this case however we're already
// showing the loading state, and want to avoid triggering an
// additional and unnecessary render showing the same loading state.
if (this.previousOptions.skip) {
return;
}

onNewData();
},
error: error => {
Expand Down
41 changes: 41 additions & 0 deletions src/react/hooks/__tests__/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2069,4 +2069,45 @@ describe('useQuery Hook', () => {
}).then(resolve, reject);
});
});

describe('Skipping', () => {
itAsync('should skip running a query when `skip` is `true`', (resolve, reject) => {
let renderCount = 0;

const Component = () => {
const [skip, setSkip] = useState(true);
const { loading, data } = useQuery(CAR_QUERY, { skip });

switch (++renderCount) {
case 1:
expect(loading).toBeFalsy();
expect(data).toBeUndefined();
setTimeout(() => setSkip(false));
break;
case 2:
expect(loading).toBeTruthy();
expect(data).toBeUndefined();
break;
case 3:
expect(loading).toBeFalsy();
expect(data).toEqual(CAR_RESULT_DATA);
break;
default:
reject("too many renders");
}

return null;
};

render(
<MockedProvider mocks={CAR_MOCKS}>
<Component />
</MockedProvider>
);

return wait(() => {
expect(renderCount).toBe(3);
}).then(resolve, reject);
});
});
});