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

Fix BatchHttpLink regression that silently discarded some pending queries #9793

Merged
merged 6 commits into from
Jun 7, 2022
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
25 changes: 23 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apollo/client",
"version": "3.7.0-beta.1",
"version": "3.7.0-beta.2",
"description": "A fully-featured caching GraphQL client.",
"private": true,
"keywords": [
Expand Down Expand Up @@ -56,7 +56,7 @@
{
"name": "apollo-client",
"path": "./dist/apollo-client.min.cjs",
"maxSize": "29.8kB"
"maxSize": "29.9kB"
}
],
"engines": {
Expand Down Expand Up @@ -99,6 +99,7 @@
"@rollup/plugin-node-resolve": "11.2.1",
"@testing-library/react": "12.1.5",
"@testing-library/react-hooks": "8.0.0",
"@testing-library/user-event": "^14.2.0",
"@types/fast-json-stable-stringify": "2.0.0",
"@types/fetch-mock": "7.3.5",
"@types/glob": "7.2.0",
Expand Down
3 changes: 1 addition & 2 deletions src/link/batch/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ export class OperationBatcher {
requestCopy.subscribers.size < 1) {
// If this is last request from queue, remove queue entirely
if (batch.delete(requestCopy) && batch.size < 1) {
clearTimeout(this.scheduledBatchTimer);
this.batchesByKey.delete(key);
this.consumeQueue(key);
// If queue was in flight, cancel it
batch.subscription?.unsubscribe();
}
Expand Down
75 changes: 74 additions & 1 deletion src/react/hooks/__tests__/useMutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@ import React, { useEffect } from 'react';
import { GraphQLError } from 'graphql';
import gql from 'graphql-tag';
import { act } from 'react-dom/test-utils';
import { render, waitFor } from '@testing-library/react';
import { render, waitFor, screen } from '@testing-library/react';
import { renderHook } from '@testing-library/react-hooks';
import userEvent from '@testing-library/user-event';
import fetchMock from "fetch-mock";

import { ApolloClient, ApolloLink, ApolloQueryResult, Cache, NetworkStatus, Observable, ObservableQuery, TypedDocumentNode } from '../../../core';
import { InMemoryCache } from '../../../cache';
import { itAsync, MockedProvider, mockSingleLink, subscribeAndCount } from '../../../testing';
import { ApolloProvider } from '../../context';
import { useQuery } from '../useQuery';
import { useMutation } from '../useMutation';
import { BatchHttpLink } from '../../../link/batch-http';

describe('useMutation Hook', () => {
interface Todo {
Expand Down Expand Up @@ -2075,5 +2079,74 @@ describe('useMutation Hook', () => {
expect(result.current.mutation[1].loading).toBe(false);
expect(result.current.mutation[1].called).toBe(true);
});

it("refetchQueries should work with BatchHttpLink", async () => {
const MUTATION_1 = gql`
mutation DoSomething {
doSomething {
message
}
}
`;

const QUERY_1 = gql`
query Items {
items {
id
}
}
`;

fetchMock.restore();

const responseBodies = [
{ data: { items: [{ id: 1 }, { id: 2 }] }},
{ data: { doSomething: { message: 'success' }}},
{ data: { items: [{ id: 1 }, { id: 2 }, { id: 3 }] }},
];

fetchMock.post("/graphql", (url, opts) => new Promise(resolve => {
resolve({
body: responseBodies.shift(),
});
}));

const Test = () => {
const { data } = useQuery(QUERY_1);
const [mutate] = useMutation(MUTATION_1, {
awaitRefetchQueries: true,
refetchQueries: [QUERY_1],
});

const { items = [] } = data || {};

return <>
<button onClick={() => {
return mutate();
}} type="button">
mutate
</button>
{items.map((c: any) => (
<div key={c.id}>item {c.id}</div>
))}
</>;
};

const client = new ApolloClient({
link: new BatchHttpLink({
uri: '/graphql',
batchMax: 10,
}),
cache: new InMemoryCache(),
});

render(<ApolloProvider client={client}><Test /></ApolloProvider>);

await screen.findByText('item 1');

userEvent.click(screen.getByRole('button', { name: /mutate/i }));

await screen.findByText('item 3');
Copy link
Member Author

@benjamn benjamn Jun 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was heavily inspired by the one provided by @skrivle in #9773.

This screen API is new to me. I'll have to try that elsewhere!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad I was able to help! Testing Library is great, definitely worth checking it out!

});
});
});