Skip to content

Commit bb08d38

Browse files
authored
Merge pull request apollographql#118 from apollographql/apollo-client-3
Upgrade to Apollo Client 3.0 (@apollo/client)
2 parents 0a1a1e2 + a1ecd61 commit bb08d38

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+17598
-20552
lines changed

final/client/__generated__/globalTypes.ts

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

final/client/package-lock.json

+4,701-6,528
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

final/client/package.json

+3-8
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33
"version": "0.1.0",
44
"private": true,
55
"dependencies": {
6-
"@apollo/react-hooks": "3.0.0",
6+
"@apollo/client": "^3.0.2",
77
"@reach/router": "^1.2.1",
88
"@types/node": "^12.12.14",
99
"@types/reach__router": "^1.2.6",
1010
"@types/react": "^16.9.15",
1111
"@types/react-dom": "^16.9.4",
12-
"apollo-cache-inmemory": "^1.6.2",
13-
"apollo-client": "^2.6.3",
14-
"apollo-link-http": "^1.5.15",
1512
"emotion": "^9.2.12",
1613
"graphql": "^14.4.2",
17-
"graphql-tag": "^2.10.1",
1814
"polished": "^3.4.1",
19-
"react": "^16.9.0-alpha.0",
20-
"react-dom": "^16.9.0-alpha.0",
15+
"react": "^16.12.0",
16+
"react-dom": "^16.12.0",
2117
"react-emotion": "^9.2.12",
2218
"react-scripts": "^3.4.1",
2319
"typescript": "^3.7.3"
@@ -40,7 +36,6 @@
4036
"not op_mini all"
4137
],
4238
"devDependencies": {
43-
"@apollo/react-testing": "3.0.0",
4439
"@testing-library/jest-dom": "^4.0.0",
4540
"@testing-library/react": "^8.0.7",
4641
"@types/jest": "^24.0.23",

final/client/src/cache.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { InMemoryCache, Reference } from '@apollo/client';
2+
3+
export const cache: InMemoryCache = new InMemoryCache({
4+
typePolicies: {
5+
Query: {
6+
fields: {
7+
isLoggedIn() {
8+
return isLoggedInVar();
9+
},
10+
cartItems() {
11+
return cartItemsVar();
12+
},
13+
launches: {
14+
merge(existing, incoming) {
15+
let launches: Reference[] = [];
16+
if (existing && existing.launches) {
17+
launches = launches.concat(existing.launches);
18+
}
19+
if (incoming && incoming.launches) {
20+
launches = launches.concat(incoming.launches);
21+
}
22+
return {
23+
...incoming,
24+
launches,
25+
};
26+
}
27+
}
28+
}
29+
}
30+
}
31+
});
32+
33+
export const isLoggedInVar =
34+
cache.makeVar<boolean>(!!localStorage.getItem('token'));
35+
export const cartItemsVar = cache.makeVar<string[]>([]);

final/client/src/components/login-form.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { colors, unit } from '../styles';
1111
import * as LoginTypes from '../pages/__generated__/login';
1212

1313
interface LoginFormProps {
14-
login: (a: { variables: LoginTypes.loginVariables }) => void;
14+
login: (a: { variables: LoginTypes.LoginVariables }) => void;
1515
}
1616

1717
interface LoginFormState {

final/client/src/containers/__generated__/BookTrips.ts

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

final/client/src/containers/__generated__/GetLaunch.ts

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

final/client/src/containers/__generated__/cancel.ts

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,8 @@
11
import React from 'react';
2-
import { InMemoryCache } from 'apollo-cache-inmemory';
32

4-
import {
5-
renderApollo,
6-
cleanup,
7-
fireEvent,
8-
waitForElement,
9-
} from '../../test-utils';
10-
import ActionButton, {
11-
GET_LAUNCH_DETAILS,
12-
TOGGLE_CART,
13-
} from '../action-button';
14-
import { GET_CART_ITEMS } from '../../pages/cart';
15-
16-
const mockLaunch = {
17-
__typename: 'Launch',
18-
id: 1,
19-
isBooked: true,
20-
rocket: {
21-
__typename: 'Rocket',
22-
id: 1,
23-
name: 'tester',
24-
type: 'test',
25-
},
26-
mission: {
27-
__typename: 'Mission',
28-
id: 1,
29-
name: 'test mission',
30-
missionPatch: '/',
31-
},
32-
site: 'earth',
33-
isInCart: false,
34-
};
3+
import { renderApollo, cleanup } from '../../test-utils';
4+
import ActionButton from '../action-button';
5+
import { cartItemsVar } from '../../cache';
356

367
describe('action button', () => {
378
// automatically unmount and cleanup DOM after the test is finished.
@@ -47,72 +18,13 @@ describe('action button', () => {
4718
getByText(/add to cart/i);
4819

4920
// rerender with different props to same container
50-
renderApollo(<ActionButton isInCart={true} />, { container });
21+
cartItemsVar(['1']);
22+
renderApollo(<ActionButton id="1" />, { container });
5123
getByText(/remove from cart/i);
24+
cartItemsVar([]);
5225

5326
// rerender with different props to same container
5427
renderApollo(<ActionButton isBooked={true} />, { container });
5528
getByText(/cancel this trip/i);
5629
});
57-
58-
/**
59-
* This test is a bit tricky, since the button doesn't _render_
60-
* anything based on the response from the mutation.
61-
*
62-
* We test this by only mocking one mutation at a time. If the component
63-
* tried to execute any mutation not mocked, it would throw an
64-
* error
65-
*/
66-
it('fires correct mutation with variables', async () => {
67-
const cache = new InMemoryCache();
68-
cache.writeQuery({
69-
query: GET_CART_ITEMS,
70-
data: { cartItems: [1] },
71-
});
72-
73-
// if we only provide 1 mock, any other queries would cause error
74-
let mocks = [
75-
{
76-
request: { query: TOGGLE_CART, variables: { launchId: 1 } },
77-
result: { data: { addOrRemoveFromCart: true } },
78-
},
79-
{
80-
request: { query: GET_LAUNCH_DETAILS, variables: { launchId: 1 } },
81-
result: { data: { launch: mockLaunch } },
82-
},
83-
];
84-
85-
const { getByTestId, container, debug } = renderApollo(
86-
<ActionButton id={'1'} isBooked={false} />,
87-
{
88-
mocks,
89-
cache,
90-
resolvers: {}
91-
},
92-
);
93-
fireEvent.click(getByTestId('action-button'));
94-
await waitForElement(() => getByTestId('action-button'));
95-
96-
// mocks = [
97-
// {
98-
// request: {
99-
// query: CANCEL_TRIP,
100-
// variables: { launchId: 1 },
101-
// },
102-
// result: {
103-
// data: {
104-
// cancelTrip: {
105-
// success: true,
106-
// message: '',
107-
// launches: [{ id: 1, isBooked: false }],
108-
// },
109-
// },
110-
// },
111-
// },
112-
// ];
113-
114-
// renderApollo(<ActionButton id={1} isBooked={true} />, { mocks, container });
115-
// fireEvent.click(getByTestId('action-button'));
116-
// await waitForElement(() => getByTestId('action-button'));
117-
});
11830
});

final/client/src/containers/__tests__/book-trips.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('book trips', () => {
3535
it('completes mutation and shows message', async () => {
3636
let mocks = [
3737
{
38-
request: { query: BOOK_TRIPS, variables: { launchIds: [1] } },
38+
request: { query: BOOK_TRIPS, variables: { launchIds: ['1'] } },
3939
result: {
4040
data: {
4141
bookTrips: [{ success: true, message: 'success!', launches: [] }],
@@ -44,7 +44,7 @@ describe('book trips', () => {
4444
},
4545
{
4646
// we need this query for refetchQueries
47-
request: { query: GET_LAUNCH, variables: { launchId: 1 } },
47+
request: { query: GET_LAUNCH, variables: { launchId: '1' } },
4848
result: { data: { launch: mockLaunch } },
4949
},
5050
];

final/client/src/containers/__tests__/cart-item.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('cart item', () => {
2828
it('queries item and renders without error', () => {
2929
let mocks = [
3030
{
31-
request: { query: GET_LAUNCH, variables: { launchId: 1 } },
31+
request: { query: GET_LAUNCH, variables: { launchId: '1' } },
3232
result: { data: { launch: mockLaunch } },
3333
},
3434
];
@@ -63,4 +63,4 @@ describe('cart item', () => {
6363

6464
waitForElement(() => getByText(/error: aw shucks/i));
6565
});
66-
});
66+
});
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React from 'react';
2-
import { InMemoryCache } from 'apollo-cache-inmemory';
32
import LogoutButton from '../logout-button';
4-
import gql from 'graphql-tag';
53

64
import { renderApollo, cleanup, fireEvent } from '../../test-utils';
5+
import { cache, isLoggedInVar } from '../../cache';
76

87
describe('logout button', () => {
98
// automatically unmount and cleanup DOM after the test is finished.
@@ -14,24 +13,13 @@ describe('logout button', () => {
1413
});
1514

1615
it('complete logout', async () => {
17-
const cache = new InMemoryCache();
18-
cache.writeData({ data: { isLoggedIn: true } });
16+
isLoggedInVar(true);
1917
localStorage.setItem('token', 'testTokenValue');
18+
localStorage.setItem('userId', 'abc123');
2019
const { getByTestId } = renderApollo(<LogoutButton />, { cache });
21-
2220
fireEvent.click(getByTestId('logout-button'));
23-
24-
// check to make sure the cache's contents have been updated
25-
const response: any = cache.readQuery({
26-
query: gql`
27-
query IsUserLoggedIn {
28-
isLoggedIn @client
29-
}
30-
`
31-
});
32-
const { isLoggedIn } = response;
33-
34-
expect(isLoggedIn).toBeFalsy();
21+
expect(isLoggedInVar()).toBeFalsy();
3522
expect(localStorage.getItem('token')).toBeNull();
23+
expect(localStorage.getItem('userId')).toBeNull();
3624
});
3725
});

0 commit comments

Comments
 (0)