Skip to content

Commit 9774e15

Browse files
authored
Backport PR #9793 from release-3.7 branch to main (#9807)
1 parent 9553b1f commit 9774e15

File tree

5 files changed

+104
-3
lines changed

5 files changed

+104
-3
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## Apollo Client 3.6.7 (2022-06-10)
2+
3+
### Bug Fixes
4+
5+
- Fix regression (introduced in v3.6.0) that caused `BatchHttpLink` to discard pending batched queries on early completion of the underlying `Observable`. <br/>
6+
[@benjamn](https://github.com/benjamn) in [#9793](https://github.com/apollographql/apollo-client/pull/9793)
7+
18
## Apollo Client 3.6.6 (2022-05-26)
29

310
### Bug Fixes

package-lock.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
"@rollup/plugin-node-resolve": "11.2.1",
100100
"@testing-library/react": "12.1.5",
101101
"@testing-library/react-hooks": "8.0.0",
102+
"@testing-library/user-event": "^14.2.0",
102103
"@types/fast-json-stable-stringify": "2.0.0",
103104
"@types/fetch-mock": "7.3.5",
104105
"@types/glob": "7.2.0",

src/link/batch/batching.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,7 @@ export class OperationBatcher {
119119
requestCopy.subscribers.size < 1) {
120120
// If this is last request from queue, remove queue entirely
121121
if (batch.delete(requestCopy) && batch.size < 1) {
122-
clearTimeout(this.scheduledBatchTimer);
123-
this.batchesByKey.delete(key);
122+
this.consumeQueue(key);
124123
// If queue was in flight, cancel it
125124
batch.subscription?.unsubscribe();
126125
}

src/react/hooks/__tests__/useMutation.test.tsx

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ import React, { useEffect } from 'react';
22
import { GraphQLError } from 'graphql';
33
import gql from 'graphql-tag';
44
import { act } from 'react-dom/test-utils';
5-
import { render, waitFor } from '@testing-library/react';
5+
import { render, waitFor, screen } from '@testing-library/react';
66
import { renderHook } from '@testing-library/react-hooks';
7+
import userEvent from '@testing-library/user-event';
8+
import fetchMock from "fetch-mock";
9+
710
import { ApolloClient, ApolloLink, ApolloQueryResult, Cache, NetworkStatus, Observable, ObservableQuery, TypedDocumentNode } from '../../../core';
811
import { InMemoryCache } from '../../../cache';
912
import { itAsync, MockedProvider, mockSingleLink, subscribeAndCount } from '../../../testing';
1013
import { ApolloProvider } from '../../context';
1114
import { useQuery } from '../useQuery';
1215
import { useMutation } from '../useMutation';
16+
import { BatchHttpLink } from '../../../link/batch-http';
1317

1418
describe('useMutation Hook', () => {
1519
interface Todo {
@@ -2075,5 +2079,74 @@ describe('useMutation Hook', () => {
20752079
expect(result.current.mutation[1].loading).toBe(false);
20762080
expect(result.current.mutation[1].called).toBe(true);
20772081
});
2082+
2083+
it("refetchQueries should work with BatchHttpLink", async () => {
2084+
const MUTATION_1 = gql`
2085+
mutation DoSomething {
2086+
doSomething {
2087+
message
2088+
}
2089+
}
2090+
`;
2091+
2092+
const QUERY_1 = gql`
2093+
query Items {
2094+
items {
2095+
id
2096+
}
2097+
}
2098+
`;
2099+
2100+
fetchMock.restore();
2101+
2102+
const responseBodies = [
2103+
{ data: { items: [{ id: 1 }, { id: 2 }] }},
2104+
{ data: { doSomething: { message: 'success' }}},
2105+
{ data: { items: [{ id: 1 }, { id: 2 }, { id: 3 }] }},
2106+
];
2107+
2108+
fetchMock.post("/graphql", (url, opts) => new Promise(resolve => {
2109+
resolve({
2110+
body: responseBodies.shift(),
2111+
});
2112+
}));
2113+
2114+
const Test = () => {
2115+
const { data } = useQuery(QUERY_1);
2116+
const [mutate] = useMutation(MUTATION_1, {
2117+
awaitRefetchQueries: true,
2118+
refetchQueries: [QUERY_1],
2119+
});
2120+
2121+
const { items = [] } = data || {};
2122+
2123+
return <>
2124+
<button onClick={() => {
2125+
return mutate();
2126+
}} type="button">
2127+
mutate
2128+
</button>
2129+
{items.map((c: any) => (
2130+
<div key={c.id}>item {c.id}</div>
2131+
))}
2132+
</>;
2133+
};
2134+
2135+
const client = new ApolloClient({
2136+
link: new BatchHttpLink({
2137+
uri: '/graphql',
2138+
batchMax: 10,
2139+
}),
2140+
cache: new InMemoryCache(),
2141+
});
2142+
2143+
render(<ApolloProvider client={client}><Test /></ApolloProvider>);
2144+
2145+
await screen.findByText('item 1');
2146+
2147+
userEvent.click(screen.getByRole('button', { name: /mutate/i }));
2148+
2149+
await screen.findByText('item 3');
2150+
});
20782151
});
20792152
});

0 commit comments

Comments
 (0)