Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Revert the changes made in #3497
Browse files Browse the repository at this point in the history
often than necessary.

Fixes: #3505
  • Loading branch information
hwillson committed Oct 15, 2019
1 parent 4163f28 commit 0901f4a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 70 deletions.
5 changes: 5 additions & 0 deletions Changelog.md
@@ -1,5 +1,10 @@
# Change log

## 3.1.3 (2019-10-15)

- Revert the changes made in [#3497](https://github.com/apollographql/react-apollo/pull/3497), which have lead to problems with `onCompleted` being called more often than necessary. <br/>
[@hwillson](https://github.com/hwillson) in [#TODO](https://github.com/apollographql/react-apollo/pull/TODO)

## 3.1.2 (2019-10-01)

### Bug Fixes
Expand Down
60 changes: 1 addition & 59 deletions packages/hooks/src/__tests__/useLazyQuery.test.tsx
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React from 'react';
import { DocumentNode } from 'graphql';
import gql from 'graphql-tag';
import { MockedProvider } from '@apollo/react-testing';
Expand Down Expand Up @@ -389,62 +389,4 @@ describe('useLazyQuery Hook', () => {
});
}
);

it('should only call onCompleted once per query run', async () => {
let renderCount = 0;
let onCompletedCount = 0;
const Component = () => {
const [_, setCounter] = useState(0);
const [execute, { loading, data }] = useLazyQuery(CAR_QUERY, {
onCompleted() {
onCompletedCount += 1;
}
});

switch (renderCount) {
case 0:
expect(loading).toEqual(false);
setTimeout(() => {
execute();
});
break;
case 1:
expect(loading).toEqual(true);
break;
case 2:
expect(loading).toEqual(false);
expect(data).toEqual(CAR_RESULT_DATA);
setTimeout(() => {
execute({ variables: { someProp: 'someValue' } });
});
break;
case 3:
expect(loading).toEqual(false);
expect(data).toEqual(CAR_RESULT_DATA);
// Force a render to help make sure onCompleted isn't called again
// since the query isn't re-run.
setCounter(1);
break;
case 4:
expect(loading).toEqual(false);
expect(data).toEqual(CAR_RESULT_DATA);
break;
default: // Do nothing
}

renderCount += 1;
return null;
};

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

await wait(() => {
expect(onCompletedCount).toBe(2);
expect(renderCount).toBe(5);
});
});
});
17 changes: 6 additions & 11 deletions packages/hooks/src/utils/useBaseQuery.ts
Expand Up @@ -51,17 +51,12 @@ export function useBaseQuery<TData = any, TVariables = OperationVariables>(
? (result as QueryTuple<TData, TVariables>)[1]
: (result as QueryResult<TData, TVariables>);

useEffect(
() => queryData.afterExecute({ lazy }),
lazy
? undefined
: [
queryResult.loading,
queryResult.networkStatus,
queryResult.error,
queryResult.data
]
);
useEffect(() => queryData.afterExecute({ lazy }), [
queryResult.loading,
queryResult.networkStatus,
queryResult.error,
queryResult.data
]);

useEffect(() => {
return () => queryData.cleanup();
Expand Down

0 comments on commit 0901f4a

Please sign in to comment.