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

React refactorings #8596

Merged
merged 65 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
a54bcb2
copy some tests from Query
brainkim Aug 5, 2021
eb972f1
refactor the partial refetch tests
brainkim Aug 5, 2021
daee4e9
more assertions in the partialRefetch stuff
brainkim Aug 6, 2021
de27ddc
add another test with partialRefetch
brainkim Aug 6, 2021
faad4fb
add a testTimeout to debug command
brainkim Aug 9, 2021
d6e7442
update useLazyQuery tests to use react testing hooks library
brainkim Aug 10, 2021
f19855f
fix some timings in useLazyQuery
brainkim Aug 11, 2021
b6445f9
Inline everything into useQuery
brainkim Jul 23, 2021
599cd40
Stop delaying useQuery calls by a microtask
brainkim Jul 23, 2021
ba37f54
fix missing paren
brainkim Jul 26, 2021
b765499
delete some unnecessary things
brainkim Jul 26, 2021
04209ff
don’t have a separate previousOptions property
brainkim Jul 26, 2021
9a67046
add an explicit return type to RenderPromises.getSSRObservable
brainkim Jul 27, 2021
2bd2d2b
crack some eggs
brainkim Jul 27, 2021
9bc6a64
inline getExecuteSsrResult
brainkim Jul 27, 2021
6be18a1
inline getExecuteResult
brainkim Jul 28, 2021
f736f16
Create a temporary useQuery1 hook
brainkim Aug 2, 2021
76dcd52
initial refactor of useQuery1
brainkim Aug 2, 2021
5526f93
Fix ssr: false test
brainkim Aug 2, 2021
888789f
add skip to the new useQuery1
brainkim Aug 3, 2021
2e75c0b
Bring partialData logic to the new useQuery1 hook
brainkim Aug 3, 2021
98e84e1
handle errors with the new useQuery1 hook
brainkim Aug 3, 2021
18197c8
do the dang void 0 stuff
brainkim Aug 3, 2021
8fa0766
fix skip test
brainkim Aug 3, 2021
2730e24
implement ssr in the new useQuery1 hook
brainkim Aug 3, 2021
4fcb5e3
delete useQuery1
brainkim Aug 5, 2021
baf31cd
fix previousResult not updating on error
brainkim Aug 5, 2021
e1c197c
remove an unnecessary setResult call in partialRefetch effect
brainkim Aug 6, 2021
fbd4e62
bring back the old errors to error behavior in useQuery
brainkim Aug 6, 2021
a5dd403
reset the result when the client changes
brainkim Aug 9, 2021
5046395
update some test timings
brainkim Aug 6, 2021
d5bd6e8
do partialRefetch directly in the render execution
brainkim Aug 10, 2021
8d0c814
update partialRefetch tests
brainkim Aug 10, 2021
24f3cab
move result.errors -> error stuff into its own code branch
brainkim Aug 10, 2021
c6691f6
implement useLazyQuery in terms of useQuery
brainkim Aug 11, 2021
fc12809
move verifyDocumentType utility to parser
brainkim Aug 11, 2021
d5754f4
smoooooosh useMutation into a single file
brainkim Aug 11, 2021
7365598
refactor useMutation to not use MutationData
brainkim Aug 11, 2021
8263a1e
refactor useSubscription to not use SubscriptionData
brainkim Aug 12, 2021
8c39483
update useSubscription tests
brainkim Aug 12, 2021
96cda70
Delete OperationData, QueryData, MutationData, SubscriptionData, useB…
brainkim Aug 12, 2021
0b54af7
reuse useApolloClient in the other hooks
brainkim Aug 12, 2021
5e75f24
remove resetQueryStoreErrors call from useQuery
brainkim Aug 12, 2021
fb87263
add a comment about some useSubscription weirdness
brainkim Aug 12, 2021
92a262f
fix useMutation test flake
brainkim Aug 13, 2021
91945a1
clean up useQuery
brainkim Aug 13, 2021
0eb8888
clean up hooks
brainkim Aug 13, 2021
726424f
Allow observableQuery methods to be called by useLazyQuery
brainkim Aug 13, 2021
b9808e2
treat standby fetch policies like skip queries
brainkim Aug 13, 2021
fc16541
call setOptions based on watchQueryOptions
brainkim Aug 13, 2021
9e523d5
add a test to make sure pollInterval triggers onCompleted
brainkim Aug 13, 2021
14cf342
Don’t refetch when fetchPolicy is standby
brainkim Aug 13, 2021
977d83d
fix useLazyQuery not refetching with the correct variables when execu…
brainkim Aug 13, 2021
91fbb4f
use the ref to reference callbacks in useSubscription
brainkim Aug 16, 2021
69a08ae
standardize naming across useMutation and useLazyQuery
brainkim Aug 16, 2021
29048b3
clarify refetch fetchPolicy code
brainkim Aug 16, 2021
d33132e
explicitly specify which methods should execute a useLazyQuery method
brainkim Aug 16, 2021
7712bfa
use itAsync
brainkim Aug 16, 2021
f1a9770
prioritize result.error calling onError over result.data calling onCo…
brainkim Aug 16, 2021
1933ff1
add some delay to possibly fix some flake in a useLazyQuery test
brainkim Aug 16, 2021
c6d41e2
rename data to previousData in useQuery
brainkim Aug 18, 2021
1834f5f
add a busted cache-and-network test
brainkim Aug 18, 2021
73569ef
add a test for #8497
brainkim Aug 18, 2021
7b95b24
tweak useMutation
brainkim Aug 18, 2021
f3e1f35
update CHANGELOG.md
brainkim Aug 18, 2021
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## Apollo Client 3.5 (unreleased)

### Bug Fixes
- `useQuery` and `useLazyQuery` will now have observableQuery methods defined consistently. <br/> [@brainkim](https://github.com/brainkim) in [#8596](https://github.com/apollographql/apollo-client/pull/8596)

- Calling `useLazyQuery` methods like `startPolling` will start the query <br/>[@brainkim](https://github.com/brainkim) in [#8596](https://github.com/apollographql/apollo-client/pull/8596)

- Calling the `useLazyQuery` execution function will now behave more like `refetch`. `previousData` will be preserved. <br/>[@brainkim](https://github.com/brainkim) in [#8596](https://github.com/apollographql/apollo-client/pull/8596)

- `standby` fetchPolicies will now act like `skip: true` more consistently <br/>[@brainkim](https://github.com/brainkim) in [#8596](https://github.com/apollographql/apollo-client/pull/8596)

- Calling `refetch` on a skipped query will have no effect (https://github.com/apollographql/apollo-client/issues/8270). <br/>[@brainkim](https://github.com/brainkim) in [#8596](https://github.com/apollographql/apollo-client/pull/8596)

- Improvements to `onError` and `onCompleted` functions, preventing them from firing continuously, and working with polling <br/>[@brainkim](https://github.com/brainkim) in [#8596](https://github.com/apollographql/apollo-client/pull/8596)

## Apollo Client 3.4.8

### Bug Fixes
Expand Down
1 change: 0 additions & 1 deletion config/entryPoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ const entryPoints = [
{ dirs: ['react'] },
{ dirs: ['react', 'components'] },
{ dirs: ['react', 'context'] },
{ dirs: ['react', 'data'] },
{ dirs: ['react', 'hoc'] },
{ dirs: ['react', 'hooks'] },
{ dirs: ['react', 'parser'] },
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"resolve": "ts-node-script config/resolveModuleIds.ts",
"clean": "rimraf -r dist coverage lib temp",
"test": "jest --config ./config/jest.config.js",
"test:debug": "BABEL_ENV=server node --inspect-brk node_modules/.bin/jest --config ./config/jest.config.js --runInBand",
"test:debug": "BABEL_ENV=server node --inspect-brk node_modules/.bin/jest --config ./config/jest.config.js --runInBand --testTimeout 99999",
"test:ci": "npm run test:coverage && npm run test:memory",
"test:watch": "jest --config ./config/jest.config.js --watch",
"test:memory": "cd scripts/memory && npm i && npm test",
Expand Down
10 changes: 1 addition & 9 deletions src/__tests__/__snapshots__/exports.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,6 @@ Array [
]
`;

exports[`exports of public entry points @apollo/client/react/data 1`] = `
Array [
"MutationData",
"OperationData",
"QueryData",
"SubscriptionData",
]
`;

exports[`exports of public entry points @apollo/client/react/hoc 1`] = `
Array [
"graphql",
Expand All @@ -291,6 +282,7 @@ Array [
"DocumentType",
"operationName",
"parser",
"verifyDocumentType",
]
`;

Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import * as linkWS from "../link/ws";
import * as react from "../react";
import * as reactComponents from "../react/components";
import * as reactContext from "../react/context";
import * as reactData from "../react/data";
import * as reactHOC from "../react/hoc";
import * as reactHooks from "../react/hooks";
import * as reactParser from "../react/parser";
Expand Down Expand Up @@ -56,7 +55,6 @@ describe('exports of public entry points', () => {
check("@apollo/client/react", react);
check("@apollo/client/react/components", reactComponents);
check("@apollo/client/react/context", reactContext);
check("@apollo/client/react/data", reactData);
check("@apollo/client/react/hoc", reactHOC);
check("@apollo/client/react/hooks", reactHooks);
check("@apollo/client/react/parser", reactParser);
Expand Down
6 changes: 4 additions & 2 deletions src/core/ObservableQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,11 @@ export class ObservableQuery<
// (no-cache, network-only, or cache-and-network), override it with
// network-only to force the refetch for this fetchQuery call.
const { fetchPolicy } = this.options;
if (fetchPolicy === 'no-cache') {
if (fetchPolicy === 'standby' || fetchPolicy === 'cache-and-network') {
reobserveOptions.fetchPolicy = fetchPolicy;
} else if (fetchPolicy === 'no-cache') {
reobserveOptions.fetchPolicy = 'no-cache';
} else if (fetchPolicy !== 'cache-and-network') {
} else {
reobserveOptions.fetchPolicy = 'network-only';
}

Expand Down
2 changes: 1 addition & 1 deletion src/react/components/Query.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function Query<TData = any, TVariables = OperationVariables>(
) {
const { children, query, ...options } = props;
const result = useQuery(query, options);
return result ? children(result) : null;
return result ? children(result as any) : null;
}

export interface Query<TData, TVariables> {
Expand Down
144 changes: 83 additions & 61 deletions src/react/components/__tests__/client/Mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { ApolloClient } from '../../../../core';
import { ApolloError } from '../../../../errors';
import { DataProxy, InMemoryCache as Cache } from '../../../../cache';
import { ApolloProvider } from '../../../context';
import { MockedProvider, MockLink, mockSingleLink } from '../../../../testing';
import {
itAsync,
MockedProvider,
MockLink,
mockSingleLink,
} from '../../../../testing';
import { Query } from '../../Query';
import { Mutation } from '../../Mutation';

Expand Down Expand Up @@ -156,24 +161,28 @@ describe('General Mutation testing', () => {
expect(spy).toHaveBeenCalledWith(mocksProps[1].result);
});

it('performs a mutation', async () => {
itAsync('performs a mutation', (resolve, reject) => {
let count = 0;
const Component = () => (
<Mutation mutation={mutation}>
{(createTodo: any, result: any) => {
if (count === 0) {
expect(result.loading).toEqual(false);
expect(result.called).toEqual(false);
createTodo();
} else if (count === 1) {
expect(result.called).toEqual(true);
expect(result.loading).toEqual(true);
} else if (count === 2) {
expect(result.called).toEqual(true);
expect(result.loading).toEqual(false);
expect(result.data).toEqual(data);
try {
if (count === 0) {
expect(result.loading).toEqual(false);
expect(result.called).toEqual(false);
createTodo();
} else if (count === 1) {
expect(result.called).toEqual(true);
expect(result.loading).toEqual(true);
} else if (count === 2) {
expect(result.called).toEqual(true);
expect(result.loading).toEqual(false);
expect(result.data).toEqual(data);
}
count++;
} catch (err) {
reject(err);
}
count++;
return <div />;
}}
</Mutation>
Expand All @@ -185,7 +194,7 @@ describe('General Mutation testing', () => {
</MockedProvider>
);

await wait();
wait().then(resolve, reject);
});

it('can bind only the mutation and not rerender by props', done => {
Expand Down Expand Up @@ -922,7 +931,7 @@ describe('General Mutation testing', () => {
});
});

it('allows a refetchQueries prop as string and variables have updated', async () => {
it('allows a refetchQueries prop as string and variables have updated', async () => new Promise((resolve, reject) => {
brainkim marked this conversation as resolved.
Show resolved Hide resolved
const query = gql`
query people($first: Int) {
allPeople(first: $first) {
Expand Down Expand Up @@ -978,33 +987,42 @@ describe('General Mutation testing', () => {
{(createTodo: any, resultMutation: any) => (
<Query query={query} variables={variables}>
{(resultQuery: any) => {
if (count === 0) {
// "first: 1" loading
expect(resultQuery.loading).toBe(true);
} else if (count === 1) {
// "first: 1" loaded
expect(resultQuery.loading).toBe(false);
setTimeout(() => setVariables({ first: 2 }));
} else if (count === 2) {
// "first: 2" loading
expect(resultQuery.loading).toBe(true);
} else if (count === 3) {
// "first: 2" loaded
expect(resultQuery.loading).toBe(false);
setTimeout(() => createTodo());
} else if (count === 4) {
// mutation loading
expect(resultMutation.loading).toBe(true);
} else if (count === 5) {
// mutation loaded
expect(resultMutation.loading).toBe(false);
} else if (count === 6) {
// query refetched
expect(resultQuery.loading).toBe(false);
expect(resultMutation.loading).toBe(false);
expect(resultQuery.data).toEqual(peopleData3);
try {
if (count === 0) {
// "first: 1" loading
expect(resultQuery.loading).toBe(true);
} else if (count === 1) {
// "first: 1" loaded
expect(resultQuery.loading).toBe(false);
expect(resultQuery.data).toEqual(peopleData1);
setTimeout(() => setVariables({ first: 2 }));
} else if (count === 2) {
expect(resultQuery.loading).toBe(false);
expect(resultQuery.data).toEqual(peopleData1);
} else if (count === 3) {
// "first: 2" loading
expect(resultQuery.loading).toBe(true);
} else if (count === 4) {
// "first: 2" loaded
expect(resultQuery.loading).toBe(false);
expect(resultQuery.data).toEqual(peopleData2);
setTimeout(() => createTodo());
} else if (count === 5) {
// mutation loading
expect(resultMutation.loading).toBe(true);
} else if (count === 6) {
// mutation loaded
expect(resultMutation.loading).toBe(false);
} else if (count === 7) {
// query refetched
expect(resultQuery.loading).toBe(false);
expect(resultMutation.loading).toBe(false);
expect(resultQuery.data).toEqual(peopleData3);
}
count++;
} catch (err) {
reject(err);
}
count++;
return null;
}}
</Query>
Expand All @@ -1019,12 +1037,12 @@ describe('General Mutation testing', () => {
</MockedProvider>
);

await wait(() => {
expect(count).toBe(7);
});
});
wait(() => {
expect(count).toBe(8);
}).then(resolve, reject);
}));

it('allows refetchQueries to be passed to the mutate function', async () => {
it('allows refetchQueries to be passed to the mutate function', () => new Promise((resolve, reject) => {
const query = gql`
query getTodo {
todo {
Expand Down Expand Up @@ -1071,18 +1089,22 @@ describe('General Mutation testing', () => {
{(createTodo: any, resultMutation: any) => (
<Query query={query}>
{(resultQuery: any) => {
if (count === 0) {
setTimeout(() => createTodo({ refetchQueries }), 10);
} else if (count === 1) {
expect(resultMutation.loading).toBe(false);
expect(resultQuery.loading).toBe(false);
} else if (count === 2) {
expect(resultMutation.loading).toBe(true);
expect(resultQuery.data).toEqual(queryData);
} else if (count === 3) {
expect(resultMutation.loading).toBe(false);
try {
if (count === 0) {
setTimeout(() => createTodo({ refetchQueries }), 10);
} else if (count === 1) {
expect(resultMutation.loading).toBe(false);
expect(resultQuery.loading).toBe(false);
} else if (count === 2) {
expect(resultMutation.loading).toBe(true);
expect(resultQuery.data).toEqual(queryData);
} else if (count === 3) {
expect(resultMutation.loading).toBe(false);
}
count++;
} catch (err) {
reject(err);
}
count++;
return null;
}}
</Query>
Expand All @@ -1096,10 +1118,10 @@ describe('General Mutation testing', () => {
</MockedProvider>
);

await wait(() => {
wait(() => {
expect(count).toBe(4);
});
});
}).then(resolve, reject);
}));

it('has an update prop for updating the store after the mutation', async () => {
const update = (_proxy: DataProxy, response: ExecutionResult) => {
Expand Down
Loading