Skip to content

Commit ea0d95c

Browse files
nilgradisnikNil Gradisnik
and
Nil Gradisnik
authored
[Apollo] Update packages to latest (#413)
Co-authored-by: Nil Gradisnik <[email protected]>
1 parent fe3ed97 commit ea0d95c

26 files changed

+197
-313
lines changed

Diff for: packages/apollo/README.md

-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ Provides out of the box support for GraphQL, powered by [Apollo](https://www.apo
66
yarn add @airbnb/lunar-apollo
77
```
88

9-
This package relies on GraphQL related packages to also be installed.
10-
11-
```bash static
12-
yarn add graphql graphql-tag
13-
```
14-
159
## Setup
1610

1711
Initialize the package to create an Apollo client. The following option settings may be passed to

Diff for: packages/apollo/package.json

+4-15
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,18 @@
1919
},
2020
"peerDependencies": {
2121
"@airbnb/lunar": "^3.0.0",
22-
"graphql": "^14.1.0",
23-
"graphql-tag": "^2.10.0",
24-
"react": "^16.8.0",
25-
"react-apollo": "^3.0.0"
22+
"graphql": "^15.5.0",
23+
"react": "^16.8.0"
2624
},
2725
"devDependencies": {
2826
"@airbnb/lunar-test-utils": "^3.0.2",
29-
"@apollo/react-testing": "^3.1.3",
30-
"graphql": "^14.6.0",
31-
"graphql-tag": "^2.10.3",
27+
"graphql": "^15.5.0",
3228
"react": "^16.13.0",
33-
"react-apollo": "^3.1.3",
3429
"react-test-renderer": "^16.13.1"
3530
},
3631
"dependencies": {
32+
"@apollo/client": "^3.3.12",
3733
"@types/lodash": "*",
38-
"apollo-cache": "^1.3.4",
39-
"apollo-cache-inmemory": "^1.6.5",
40-
"apollo-client": "^2.6.8",
41-
"apollo-link": "^1.2.13",
42-
"apollo-link-error": "^1.1.12",
43-
"apollo-link-http": "^1.5.16",
44-
"apollo-utilities": "^1.3.3",
4534
"lodash": "^4.17.15"
4635
}
4736
}

Diff for: packages/apollo/src/components/Mutation/index.tsx

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import React from 'react';
2+
import { MutationResult, MutationFunction, OperationVariables, ApolloError } from '@apollo/client';
23
import {
34
Mutation as BaseMutation,
45
MutationComponentOptions,
5-
MutationResult,
6-
MutationFunction,
7-
OperationVariables,
8-
} from 'react-apollo';
9-
import { ApolloError } from 'apollo-client';
6+
} from '@apollo/client/react/components';
107
import ErrorMessage from '@airbnb/lunar/lib/components/ErrorMessage';
118
import Loader from '@airbnb/lunar/lib/components/Loader';
129
import renderElementOrFunction, {

Diff for: packages/apollo/src/components/Mutation/story.tsx

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import React from 'react';
2-
import gql from 'graphql-tag';
2+
import { gql, MutationFunction } from '@apollo/client';
33
import Button from '@airbnb/lunar/lib/components/Button';
44
import Shimmer from '@airbnb/lunar/lib/components/Shimmer';
55
import ErrorMessage from '@airbnb/lunar/lib/components/ErrorMessage';
6-
import { MutationFunction } from 'react-apollo';
7-
import { MockedProvider } from '@apollo/react-testing';
6+
import { MockedProvider } from '@apollo/client/testing';
87
import Mutation from '.';
98

109
const MUTATION = gql`
@@ -109,7 +108,11 @@ customLoadingComponent.story = {
109108
export function customErrorComponent() {
110109
return (
111110
<MockedProvider mocks={[errorMock]} addTypename={false}>
112-
<Mutation mutation={MUTATION} error={(error) => <ErrorMessage error={error} />}>
111+
<Mutation
112+
mutation={MUTATION}
113+
variables={variables}
114+
error={(error) => <ErrorMessage error={error} />}
115+
>
113116
{(updateUser) => <UpdateButton onUpdate={updateUser} />}
114117
</Mutation>
115118
</MockedProvider>

Diff for: packages/apollo/src/components/Provider/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { ApolloProvider } from 'react-apollo';
2+
import { ApolloProvider } from '@apollo/client';
33
import Apollo from '../..';
44

55
export type ProviderProps = {

Diff for: packages/apollo/src/components/Query/index.tsx

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import React from 'react';
2-
import {
3-
Query as BaseQuery,
4-
QueryComponentOptions,
5-
QueryResult,
6-
OperationVariables,
7-
} from 'react-apollo';
8-
import { ApolloError } from 'apollo-client';
2+
import { QueryResult, OperationVariables, ApolloError } from '@apollo/client';
3+
import { Query as BaseQuery, QueryComponentOptions } from '@apollo/client/react/components';
94
import ErrorMessage from '@airbnb/lunar/lib/components/ErrorMessage';
105
import Loader from '@airbnb/lunar/lib/components/Loader';
116
import renderElementOrFunction, {

Diff for: packages/apollo/src/components/Query/story.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22
import Shimmer from '@airbnb/lunar/lib/components/Shimmer';
33
import ErrorMessage from '@airbnb/lunar/lib/components/ErrorMessage';
4-
import { MockedProvider } from '@apollo/react-testing';
5-
import gql from 'graphql-tag';
4+
import { MockedProvider } from '@apollo/client/testing';
5+
import { gql } from '@apollo/client';
66
import Query from '.';
77

88
const QUERY = gql`

Diff for: packages/apollo/src/hooks/useMutation.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { useMutation } from 'react-apollo';
1+
import { useMutation } from '@apollo/client';
22

33
export default useMutation;

Diff for: packages/apollo/src/hooks/useQuery.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { useQuery } from 'react-apollo';
1+
import { useQuery } from '@apollo/client';
22

33
export default useQuery;

Diff for: packages/apollo/src/hooks/useSubscription.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { useSubscription } from 'react-apollo';
1+
import { useSubscription } from '@apollo/client';
22

33
export default useSubscription;

Diff for: packages/apollo/src/index.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import Core from '@airbnb/lunar';
2-
import { ApolloClient, ApolloClientOptions } from 'apollo-client';
3-
import { InMemoryCache, InMemoryCacheConfig } from 'apollo-cache-inmemory';
4-
import { ApolloLink } from 'apollo-link';
5-
import { onError } from 'apollo-link-error';
6-
import { HttpLink } from 'apollo-link-http';
2+
import {
3+
ApolloClient,
4+
ApolloClientOptions,
5+
InMemoryCache,
6+
InMemoryCacheConfig,
7+
ApolloLink,
8+
HttpLink,
9+
} from '@apollo/client';
10+
import { onError } from '@apollo/client/link/error';
711
import Mutation from './components/Mutation';
812
import Query from './components/Query';
913
import Provider from './components/Provider';
1014
// @ts-ignore
1115
import pkg from '../package.json';
1216

13-
export * from 'apollo-client';
14-
export * from 'apollo-cache-inmemory';
17+
export * from '@apollo/client';
1518

1619
export { onError, HttpLink, Mutation, Query, Provider };
1720

Diff for: packages/apollo/src/updaters/addToList.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import { DocumentNode } from 'graphql';
2-
import get from 'lodash/get';
3-
import set from 'lodash/set';
4-
import { DataProxy } from 'apollo-cache';
5-
import { MutationFetchResult } from 'react-apollo';
2+
import { get, set } from 'lodash/fp';
3+
import { DataProxy, MutationResult } from '@apollo/client';
64
import prepareQuery from '../utils/prepareQuery';
75
import getQueryName from '../utils/getQueryName';
86

97
export default function addToList<Result, Vars = {}>(
10-
docOrQuery: DocumentNode | DataProxy.Query<Vars>,
8+
docOrQuery: DocumentNode | DataProxy.Query<Vars, Result>,
119
listPath: string,
1210
mutationPath: string,
1311
) {
14-
const query = prepareQuery<Vars>(docOrQuery);
12+
const query = prepareQuery<Result, Vars>(docOrQuery);
1513

16-
return (cache: DataProxy, mutationResult: MutationFetchResult<Result>) => {
14+
return (cache: DataProxy, mutationResult: MutationResult<Result>) => {
1715
const queryResult = cache.readQuery<Result>(query);
18-
const nextResult = { ...queryResult };
19-
const list = get(queryResult, listPath);
16+
const list = get(listPath, queryResult);
2017

2118
if (typeof list === 'undefined' || !Array.isArray(list)) {
2219
if (__DEV__) {
@@ -26,7 +23,7 @@ export default function addToList<Result, Vars = {}>(
2623
}
2724
}
2825

29-
const data = get(mutationResult.data, mutationPath);
26+
const data = get(mutationPath, mutationResult);
3027

3128
if (typeof data === 'undefined') {
3229
if (__DEV__) {
@@ -36,7 +33,7 @@ export default function addToList<Result, Vars = {}>(
3633
}
3734
}
3835

39-
set(nextResult, listPath, [...list, data]);
36+
const nextResult = set(listPath, [...list, data], { ...queryResult });
4037

4138
cache.writeQuery({
4239
...query,

Diff for: packages/apollo/src/updaters/removeFromList.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
import { DocumentNode } from 'graphql';
2-
import get from 'lodash/get';
3-
import set from 'lodash/set';
4-
import { DataProxy } from 'apollo-cache';
2+
import { get, set } from 'lodash/fp';
3+
import { DataProxy, ApolloCache, StoreObject } from '@apollo/client';
54
import prepareQuery from '../utils/prepareQuery';
65
import getQueryName from '../utils/getQueryName';
76

87
export default function removeFromList<Result, Vars = {}>(
9-
docOrQuery: DocumentNode | DataProxy.Query<Vars>,
8+
docOrQuery: DocumentNode | DataProxy.Query<Vars, Result>,
109
listPath: string,
1110
id: string | number,
1211
idName: string = 'id',
1312
) {
14-
const query = prepareQuery<Vars>(docOrQuery);
13+
const query = prepareQuery<Result, Vars>(docOrQuery);
1514

16-
return (cache: DataProxy) => {
15+
return (cache: ApolloCache<Result>) => {
1716
const queryResult = cache.readQuery<Result>(query);
18-
const nextResult = { ...queryResult };
19-
const list = get(queryResult, listPath);
17+
const list = get(listPath, queryResult);
2018

2119
if (typeof list === 'undefined' || !Array.isArray(list)) {
2220
if (__DEV__) {
@@ -26,10 +24,19 @@ export default function removeFromList<Result, Vars = {}>(
2624
}
2725
}
2826

29-
set(
30-
nextResult,
27+
const rootItem = listPath.split('.')[0];
28+
// @ts-ignore
29+
const resultRoot = queryResult[rootItem] as StoreObject;
30+
// Evict cache before writeQuery: https://github.com/apollographql/apollo-client/issues/6451
31+
cache.evict({
32+
id: cache.identify(resultRoot),
33+
broadcast: false,
34+
});
35+
36+
const nextResult = set(
3137
listPath,
3238
list.filter((item) => (item as { [key: string]: unknown })[idName] !== id),
39+
{ ...queryResult },
3340
);
3441

3542
cache.writeQuery({

Diff for: packages/apollo/src/utils/prepareQuery.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { DocumentNode } from 'graphql';
2-
import { DataProxy } from 'apollo-cache';
2+
import { DataProxy } from '@apollo/client';
33

4-
export default function prepareQuery<Vars = {}>(
5-
docOrQuery: DocumentNode | DataProxy.Query<Vars>,
6-
): DataProxy.Query<Vars> {
4+
export default function prepareQuery<Result, Vars = {}>(
5+
docOrQuery: DocumentNode | DataProxy.Query<Vars, Result>,
6+
): DataProxy.Query<Vars, Result> {
77
const query = docOrQuery;
88

99
if ((query as DocumentNode).kind) {
10-
return { query } as DataProxy.Query<Vars>;
10+
return { query } as DataProxy.Query<Vars, Result>;
1111
}
1212

13-
return query as DataProxy.Query<Vars>;
13+
return query as DataProxy.Query<Vars, Result>;
1414
}

Diff for: packages/apollo/test/components/Mutation.test.tsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
2-
import { MutationResult, MutationFunction } from 'react-apollo';
2+
import { gql, MutationResult, MutationFunction } from '@apollo/client';
33
import { mount } from 'enzyme';
4-
import gql from 'graphql-tag';
54
import { WrappingComponent } from '@airbnb/lunar-test-utils';
65
import Loader from '@airbnb/lunar/lib/components/Loader';
76
import ErrorMessage from '@airbnb/lunar/lib/components/ErrorMessage';
8-
import { MockedProvider, MockedResponse, wait } from '@apollo/react-testing';
7+
import { MockedProvider, MockedResponse } from '@apollo/client/testing';
98
import Mutation from '../../src/components/Mutation';
9+
import { wait } from '../utils';
1010

1111
const MUTATION = gql`
1212
mutation updateSomething($id: Int!, $name: String!) {
@@ -31,8 +31,13 @@ function ApolloComponent({
3131
);
3232
}
3333

34+
type Variables = {
35+
id: number;
36+
name: string;
37+
};
38+
3439
describe('Mutation', () => {
35-
const childHandler = (mutate: MutationFunction<unknown, {}>) => (
40+
const childHandler = (mutate: MutationFunction<unknown, Variables>) => (
3641
<button type="button" onClick={() => mutate({ variables: { id: 123, name: 'Something' } })}>
3742
Submit
3843
</button>

Diff for: packages/apollo/test/components/Provider.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import { mountWithStyles, WrappingComponent } from '@airbnb/lunar-test-utils';
3-
import { ApolloProvider } from 'react-apollo';
3+
import { ApolloProvider } from '@apollo/client';
44
import Apollo from '../../src';
55
import Provider from '../../src/components/Provider';
66

Diff for: packages/apollo/test/components/Query.test.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import React from 'react';
22
import { mount } from 'enzyme';
3-
import gql from 'graphql-tag';
3+
import { gql } from '@apollo/client';
44
import { WrappingComponent } from '@airbnb/lunar-test-utils';
55
import Loader from '@airbnb/lunar/lib/components/Loader';
66
import ErrorMessage from '@airbnb/lunar/lib/components/ErrorMessage';
7-
import { MockedProvider, MockedResponse, wait } from '@apollo/react-testing';
7+
import { MockedProvider, MockedResponse } from '@apollo/client/testing';
88
import Query from '../../src/components/Query';
9+
import { wait } from '../utils';
910

1011
const QUERY = gql`
1112
query getSomething {

Diff for: packages/apollo/test/index.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ApolloClient from 'apollo-client';
1+
import { ApolloClient } from '@apollo/client';
22
import Apollo, { Settings, HttpLink } from '../src';
33

44
describe('Apollo', () => {

Diff for: packages/apollo/test/updaters/__snapshots__/addToList.test.ts.snap

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,4 @@ exports[`addToList() errors if mutation data does not exist 1`] = `"Cannot find
44

55
exports[`addToList() errors if property name is not an array 1`] = `"\\"getSomething\\" list \\"something.name\\" is not an array."`;
66

7-
exports[`addToList() errors if query does not exist in cache 1`] = `
8-
"Can't find field things on object {
9-
\\"something\\": {
10-
\\"type\\": \\"id\\",
11-
\\"generated\\": false,
12-
\\"id\\": \\"something:123\\",
13-
\\"typename\\": \\"something\\"
14-
}
15-
}."
16-
`;
7+
exports[`addToList() errors if query does not exist in cache 1`] = `"\\"otherQuery\\" list \\"something.things\\" is not an array."`;

Diff for: packages/apollo/test/updaters/__snapshots__/removeFromList.test.ts.snap

+1-10
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,4 @@
22

33
exports[`removeFromList() errors if property name is not an array 1`] = `"\\"getSomething\\" list \\"something.name\\" is not an array."`;
44

5-
exports[`removeFromList() errors if query does not exist in cache 1`] = `
6-
"Can't find field things on object {
7-
\\"something\\": {
8-
\\"type\\": \\"id\\",
9-
\\"generated\\": false,
10-
\\"id\\": \\"something:123\\",
11-
\\"typename\\": \\"something\\"
12-
}
13-
}."
14-
`;
5+
exports[`removeFromList() errors if query does not exist in cache 1`] = `"\\"otherQuery\\" list \\"things\\" is not an array."`;

0 commit comments

Comments
 (0)