Skip to content

Commit b7aac32

Browse files
test: error handling example (#1734)
* test: error handling example * make tests not flaky * improve * chore(dependencies): updated changesets for modified dependencies * prettier Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 7213b29 commit b7aac32

File tree

5 files changed

+134
-64
lines changed

5 files changed

+134
-64
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'graphql-yoga': patch
3+
---
4+
5+
dependencies updates:
6+
7+
- Updated dependency [`@whatwg-node/[email protected]` ↗︎](https://www.npmjs.com/package/@whatwg-node/server/v/0.4.2) (from `0.4.1`, in `dependencies`)
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { yoga } from '../src/yoga'
2+
import { createServer, Server } from 'http'
3+
import { AddressInfo } from 'net'
4+
import { fetch } from '@whatwg-node/fetch'
5+
6+
describe('error-handling example integration', () => {
7+
let server: Server
8+
let port: number
9+
10+
beforeAll(async () => {
11+
server = createServer(yoga)
12+
await new Promise<void>((resolve) => server.listen(0, resolve))
13+
port = (server.address() as AddressInfo).port
14+
})
15+
16+
afterAll(async () => {
17+
await new Promise((resolve) => server.close(resolve))
18+
})
19+
20+
it('should get a masked error', async () => {
21+
const response = await fetch(
22+
`http://localhost:${port}/graphql?query=query{greeting}`,
23+
)
24+
const body = await response.json()
25+
26+
expect(body.errors).toBeDefined()
27+
expect(body.errors[0].message).toEqual('Unexpected error.')
28+
expect(body.data).toBeNull()
29+
})
30+
31+
it('should get a custom error', async () => {
32+
const response = await fetch(
33+
`http://localhost:${port}/graphql?query=query{user(byId: "6"){id}}`,
34+
)
35+
const body = await response.json()
36+
37+
expect(body.errors).toBeDefined()
38+
expect(body.errors).toMatchInlineSnapshot(`
39+
[
40+
{
41+
"extensions": {
42+
"code": "USER_NOT_FOUND",
43+
"someRandomExtensions": {
44+
"aaaa": 3,
45+
},
46+
},
47+
"locations": [
48+
{
49+
"column": 7,
50+
"line": 1,
51+
},
52+
],
53+
"message": "User with id '6' not found.",
54+
"path": [
55+
"user",
56+
],
57+
},
58+
]
59+
`)
60+
})
61+
})

examples/error-handling/src/index.ts

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,5 @@
1-
import { createYoga, createSchema } from 'graphql-yoga'
2-
import { fetch } from '@whatwg-node/fetch'
3-
import { GraphQLError } from 'graphql'
41
import { createServer } from 'http'
5-
6-
const users = [
7-
{
8-
id: '1',
9-
login: 'Laurin',
10-
},
11-
{
12-
id: '2',
13-
login: 'Saihaj',
14-
},
15-
{
16-
id: '3',
17-
login: 'Dotan',
18-
},
19-
]
20-
21-
// Provide your schema
22-
const yoga = createYoga({
23-
schema: createSchema({
24-
typeDefs: /* GraphQL */ `
25-
type User {
26-
id: ID!
27-
login: String!
28-
}
29-
type Query {
30-
greeting: String!
31-
user(byId: ID!): User!
32-
}
33-
`,
34-
resolvers: {
35-
Query: {
36-
greeting: async () => {
37-
// This service does not exist
38-
const greeting = await fetch('http://localhost:9876/greeting').then(
39-
(res) => res.text(),
40-
)
41-
42-
return greeting
43-
},
44-
user: async (_, args) => {
45-
const user = users.find((user) => user.id === args.byId)
46-
if (!user) {
47-
throw new GraphQLError(`User with id '${args.byId}' not found.`, {
48-
extensions: {
49-
code: 'USER_NOT_FOUND',
50-
someRandomExtensions: {
51-
aaaa: 3,
52-
},
53-
},
54-
})
55-
}
56-
57-
return user
58-
},
59-
},
60-
},
61-
}),
62-
logging: true,
63-
maskedErrors: true,
64-
})
2+
import { yoga } from './yoga'
653

664
// Start the server and explore http://localhost:4000/graphql
675
const server = createServer(yoga)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { createYoga, createSchema } from 'graphql-yoga'
2+
import { fetch } from '@whatwg-node/fetch'
3+
import { GraphQLError } from 'graphql'
4+
5+
const users = [
6+
{
7+
id: '1',
8+
login: 'Laurin',
9+
},
10+
{
11+
id: '2',
12+
login: 'Saihaj',
13+
},
14+
{
15+
id: '3',
16+
login: 'Dotan',
17+
},
18+
]
19+
20+
// Provide your schema
21+
export const yoga = createYoga({
22+
schema: createSchema({
23+
typeDefs: /* GraphQL */ `
24+
type User {
25+
id: ID!
26+
login: String!
27+
}
28+
type Query {
29+
greeting: String!
30+
user(byId: ID!): User!
31+
}
32+
`,
33+
resolvers: {
34+
Query: {
35+
greeting: async () => {
36+
// This service does not exist
37+
const greeting = await fetch('http://localhost:9876/greeting').then(
38+
(res) => res.text(),
39+
)
40+
41+
return greeting
42+
},
43+
user: async (_, args) => {
44+
const user = users.find((user) => user.id === args.byId)
45+
if (!user) {
46+
throw new GraphQLError(`User with id '${args.byId}' not found.`, {
47+
extensions: {
48+
code: 'USER_NOT_FOUND',
49+
someRandomExtensions: {
50+
aaaa: 3,
51+
},
52+
},
53+
})
54+
}
55+
56+
return user
57+
},
58+
},
59+
},
60+
}),
61+
logging: true,
62+
maskedErrors: true,
63+
})

examples/error-handling/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"moduleResolution": "node",
66
"module": "commonjs",
77
"sourceMap": true,
8-
"lib": ["esnext"]
8+
"lib": ["esnext"],
9+
"esModuleInterop": true
910
}
1011
}

0 commit comments

Comments
 (0)