Skip to content

Commit 9e06f43

Browse files
authored
Support errors (rubengrill#7)
1 parent f38f9ec commit 9e06f43

File tree

12 files changed

+239
-113
lines changed

12 files changed

+239
-113
lines changed

README.md

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ See: [reference](examples/docs/src/apolloMock.js)
398398
<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./examples/docs/src/apolloMock.test.js) -->
399399
<!-- The below code snippet is automatically added from ./examples/docs/src/apolloMock.test.js -->
400400
```js
401+
import { GraphQLError } from "graphql";
402+
401403
import apolloMock from "./apolloMock";
402404
import authors from "./authors.graphql";
403405
import createAuthor from "./createAuthor.graphql";
@@ -416,7 +418,7 @@ describe("apolloMock", () => {
416418
},
417419
});
418420
419-
expect(apolloMock(authors, {}, { authors: [{}] })).toEqual({
421+
expect(apolloMock(authors, {}, { data: { authors: [{}] } })).toEqual({
420422
request: {
421423
query: authors,
422424
variables: {},
@@ -441,7 +443,7 @@ describe("apolloMock", () => {
441443
apolloMock(
442444
createAuthor,
443445
{ input: { name: "Foo", books: [{ title: "Bar" }] } },
444-
{ createAuthor: { name: "Foo", books: [{ title: "Bar" }] } }
446+
{ data: { createAuthor: { name: "Foo", books: [{ title: "Bar" }] } } }
445447
)
446448
).toEqual({
447449
request: {
@@ -479,7 +481,7 @@ describe("apolloMock", () => {
479481
const scalarValues = { Date: "2020-01-01" };
480482
481483
expect(
482-
apolloMock(authors, {}, { authors: [{}] }, { scalarValues })
484+
apolloMock(authors, {}, { data: { authors: [{}] } }, { scalarValues })
483485
).toEqual({
484486
request: {
485487
query: authors,
@@ -501,6 +503,28 @@ describe("apolloMock", () => {
501503
},
502504
});
503505
});
506+
507+
it("supports errors", () => {
508+
expect(
509+
apolloMock(authors, {}, { errors: [new GraphQLError("Already exists")] })
510+
).toEqual({
511+
request: {
512+
query: authors,
513+
variables: {},
514+
},
515+
result: {
516+
errors: [new GraphQLError("Already exists")],
517+
},
518+
});
519+
520+
expect(apolloMock(authors, {}, new Error("Network error"))).toEqual({
521+
request: {
522+
query: authors,
523+
variables: {},
524+
},
525+
error: new Error("Network error"),
526+
});
527+
});
504528
});
505529
```
506530
<!-- AUTO-GENERATED-CONTENT:END -->

examples/cra-ts/src/App.test.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import createAuthorMutation from "./createAuthor.graphql";
99

1010
test("adds author", async () => {
1111
const mocks = [
12-
apolloMock(authorsQuery, {}, { authors: [] }),
12+
apolloMock(authorsQuery, {}, { data: { authors: [] } }),
1313
apolloMock(
1414
createAuthorMutation,
1515
{ input: { name: "Foo", books: [{ title: "Bar" }] } },
16-
{ createAuthor: { name: "Foo", books: [{ title: "Bar" }] } }
16+
{ data: { createAuthor: { name: "Foo", books: [{ title: "Bar" }] } } }
1717
),
1818
apolloMock(
1919
authorsQuery,
2020
{},
21-
{ authors: [{ name: "Foo", books: [{ title: "Bar" }] }] }
21+
{ data: { authors: [{ name: "Foo", books: [{ title: "Bar" }] }] } }
2222
),
2323
];
2424

examples/cra-ts/src/App.tsx

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import createAuthorMutation from "./createAuthor.graphql";
66

77
const App = () => {
88
const { data } = useQuery(authorsQuery);
9-
const [createAuthor, { data: createAuthorData }] = useMutation(
10-
createAuthorMutation,
11-
{
12-
refetchQueries: [{ query: authorsQuery }],
13-
}
14-
);
9+
const [createAuthor, { error }] = useMutation(createAuthorMutation, {
10+
refetchQueries: [{ query: authorsQuery }],
11+
onError: () => {},
12+
});
1513

1614
return (
1715
<>
@@ -20,17 +18,16 @@ const App = () => {
2018
<li key={author.id}>{author.name}</li>
2119
))}
2220
</ul>
23-
{!createAuthorData && (
24-
<button
25-
onClick={() => {
26-
createAuthor({
27-
variables: { input: { name: "Foo", books: [{ title: "Bar" }] } },
28-
});
29-
}}
30-
>
31-
Add
32-
</button>
33-
)}
21+
{error && <div style={{ color: "red" }}>{error.message}</div>}
22+
<button
23+
onClick={() => {
24+
createAuthor({
25+
variables: { input: { name: "Foo", books: [{ title: "Bar" }] } },
26+
});
27+
}}
28+
>
29+
Add
30+
</button>
3431
</>
3532
);
3633
};

examples/cra-ts/src/index.tsx

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { MockedProvider } from "@apollo/react-testing";
2+
import { GraphQLError } from "graphql";
23
import React from "react";
34
import ReactDOM from "react-dom";
45

@@ -8,16 +9,30 @@ import authorsQuery from "./authors.graphql";
89
import createAuthorMutation from "./createAuthor.graphql";
910

1011
const mocks = [
11-
apolloMock(authorsQuery, {}, { authors: [] }),
12+
apolloMock(authorsQuery, {}, { data: { authors: [] } }),
1213
apolloMock(
1314
createAuthorMutation,
1415
{ input: { name: "Foo", books: [{ title: "Bar" }] } },
15-
{ createAuthor: { name: "Foo", books: [{ title: "Bar" }] } }
16+
{ data: { createAuthor: { name: "Foo", books: [{ title: "Bar" }] } } }
1617
),
1718
apolloMock(
1819
authorsQuery,
1920
{},
20-
{ authors: [{ name: "Foo", books: [{ title: "Bar" }] }] }
21+
{ data: { authors: [{ name: "Foo", books: [{ title: "Bar" }] }] } }
22+
),
23+
apolloMock(
24+
createAuthorMutation,
25+
{
26+
input: { name: "Foo", books: [{ title: "Bar" }] },
27+
},
28+
{ errors: [new GraphQLError("Foo already exists")] }
29+
),
30+
apolloMock(
31+
createAuthorMutation,
32+
{
33+
input: { name: "Foo", books: [{ title: "Bar" }] },
34+
},
35+
new Error("Connection problem")
2136
),
2237
];
2338

examples/cra/src/App.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import createAuthorMutation from "./createAuthor.graphql";
66

77
const App = () => {
88
const { data } = useQuery(authorsQuery);
9-
const [createAuthor, { data: createAuthorData }] = useMutation(
10-
createAuthorMutation,
11-
{
12-
refetchQueries: [{ query: authorsQuery }],
13-
}
14-
);
9+
const [createAuthor, { error }] = useMutation(createAuthorMutation, {
10+
refetchQueries: [{ query: authorsQuery }],
11+
onError: () => {},
12+
});
1513

1614
return (
1715
<>
@@ -20,17 +18,16 @@ const App = () => {
2018
<li key={author.id}>{author.name}</li>
2119
))}
2220
</ul>
23-
{!createAuthorData && (
24-
<button
25-
onClick={() => {
26-
createAuthor({
27-
variables: { input: { name: "Foo", books: [{ title: "Bar" }] } },
28-
});
29-
}}
30-
>
31-
Add
32-
</button>
33-
)}
21+
{error && <div style={{ color: "red" }}>{error.message}</div>}
22+
<button
23+
onClick={() => {
24+
createAuthor({
25+
variables: { input: { name: "Foo", books: [{ title: "Bar" }] } },
26+
});
27+
}}
28+
>
29+
Add
30+
</button>
3431
</>
3532
);
3633
};

examples/cra/src/App.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import createAuthorMutation from "./createAuthor.graphql";
99

1010
test("adds author", async () => {
1111
const mocks = [
12-
apolloMock(authorsQuery, {}, { authors: [] }),
12+
apolloMock(authorsQuery, {}, { data: { authors: [] } }),
1313
apolloMock(
1414
createAuthorMutation,
1515
{ input: { name: "Foo", books: [{ title: "Bar" }] } },
16-
{ createAuthor: { name: "Foo", books: [{ title: "Bar" }] } }
16+
{ data: { createAuthor: { name: "Foo", books: [{ title: "Bar" }] } } }
1717
),
1818
apolloMock(
1919
authorsQuery,
2020
{},
21-
{ authors: [{ name: "Foo", books: [{ title: "Bar" }] }] }
21+
{ data: { authors: [{ name: "Foo", books: [{ title: "Bar" }] }] } }
2222
),
2323
];
2424

examples/cra/src/index.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { MockedProvider } from "@apollo/react-testing";
2+
import { GraphQLError } from "graphql";
23
import React from "react";
34
import ReactDOM from "react-dom";
45

@@ -8,16 +9,30 @@ import authorsQuery from "./authors.graphql";
89
import createAuthorMutation from "./createAuthor.graphql";
910

1011
const mocks = [
11-
apolloMock(authorsQuery, {}, { authors: [] }),
12+
apolloMock(authorsQuery, {}, { data: { authors: [] } }),
1213
apolloMock(
1314
createAuthorMutation,
1415
{ input: { name: "Foo", books: [{ title: "Bar" }] } },
15-
{ createAuthor: { name: "Foo", books: [{ title: "Bar" }] } }
16+
{ data: { createAuthor: { name: "Foo", books: [{ title: "Bar" }] } } }
1617
),
1718
apolloMock(
1819
authorsQuery,
1920
{},
20-
{ authors: [{ name: "Foo", books: [{ title: "Bar" }] }] }
21+
{ data: { authors: [{ name: "Foo", books: [{ title: "Bar" }] }] } }
22+
),
23+
apolloMock(
24+
createAuthorMutation,
25+
{
26+
input: { name: "Foo", books: [{ title: "Bar" }] },
27+
},
28+
{ errors: [new GraphQLError("Foo already exists")] }
29+
),
30+
apolloMock(
31+
createAuthorMutation,
32+
{
33+
input: { name: "Foo", books: [{ title: "Bar" }] },
34+
},
35+
new Error("Connection problem")
2136
),
2237
];
2338

examples/docs/src/apolloMock.test.js

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { GraphQLError } from "graphql";
2+
13
import apolloMock from "./apolloMock";
24
import authors from "./authors.graphql";
35
import createAuthor from "./createAuthor.graphql";
@@ -16,7 +18,7 @@ describe("apolloMock", () => {
1618
},
1719
});
1820

19-
expect(apolloMock(authors, {}, { authors: [{}] })).toEqual({
21+
expect(apolloMock(authors, {}, { data: { authors: [{}] } })).toEqual({
2022
request: {
2123
query: authors,
2224
variables: {},
@@ -41,7 +43,7 @@ describe("apolloMock", () => {
4143
apolloMock(
4244
createAuthor,
4345
{ input: { name: "Foo", books: [{ title: "Bar" }] } },
44-
{ createAuthor: { name: "Foo", books: [{ title: "Bar" }] } }
46+
{ data: { createAuthor: { name: "Foo", books: [{ title: "Bar" }] } } }
4547
)
4648
).toEqual({
4749
request: {
@@ -79,7 +81,7 @@ describe("apolloMock", () => {
7981
const scalarValues = { Date: "2020-01-01" };
8082

8183
expect(
82-
apolloMock(authors, {}, { authors: [{}] }, { scalarValues })
84+
apolloMock(authors, {}, { data: { authors: [{}] } }, { scalarValues })
8385
).toEqual({
8486
request: {
8587
query: authors,
@@ -101,4 +103,26 @@ describe("apolloMock", () => {
101103
},
102104
});
103105
});
106+
107+
it("supports errors", () => {
108+
expect(
109+
apolloMock(authors, {}, { errors: [new GraphQLError("Already exists")] })
110+
).toEqual({
111+
request: {
112+
query: authors,
113+
variables: {},
114+
},
115+
result: {
116+
errors: [new GraphQLError("Already exists")],
117+
},
118+
});
119+
120+
expect(apolloMock(authors, {}, new Error("Network error"))).toEqual({
121+
request: {
122+
query: authors,
123+
variables: {},
124+
},
125+
error: new Error("Network error"),
126+
});
127+
});
104128
});

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"devDependencies": {
3939
"@apollo/react-common": "^3.1.4",
4040
"@apollo/react-hooks": "^3.1.4",
41+
"@apollo/react-testing": "^3.1.4",
4142
"@graphql-codegen/add": "^1.13.3",
4243
"@graphql-codegen/cli": "^1.13.3",
4344
"@graphql-codegen/core": "^1.13.2",
@@ -51,6 +52,7 @@
5152
"@typescript-eslint/eslint-plugin": "^2.28.0",
5253
"@typescript-eslint/parser": "^2.28.0",
5354
"apollo-boost": "^0.4.7",
55+
"apollo-link": "^1.2.14",
5456
"apollo-typed-documents": "link:.",
5557
"eslint": "^6.8.0",
5658
"eslint-config-prettier": "^6.10.1",

0 commit comments

Comments
 (0)