-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathhandler.ts
122 lines (109 loc) · 3.36 KB
/
handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { jest } from '@jest/globals';
import { GraphQLError } from 'graphql';
import fetch from 'node-fetch';
import { Request } from '../handler';
import { startTServer } from './utils/tserver';
it.each(['schema', 'context', 'onSubscribe', 'onOperation'])(
'should use the response returned from %s',
async (option) => {
const server = startTServer({
[option]: () => {
return [null, { status: 418 }];
},
});
const url = new URL(server.url);
url.searchParams.set('query', '{ __typename }');
const res = await fetch(url.toString());
expect(res.status).toBe(418);
},
);
it('should report graphql errors returned from onSubscribe', async () => {
const server = startTServer({
onSubscribe: () => {
return [new GraphQLError('Woah!')];
},
});
const url = new URL(server.url);
url.searchParams.set('query', '{ __typename }');
const res = await fetch(url.toString());
expect(res.json()).resolves.toEqual({ errors: [{ message: 'Woah!' }] });
});
it('should respond with result returned from onSubscribe', async () => {
const onOperationFn = jest.fn(() => {
// noop
});
const server = startTServer({
onSubscribe: () => {
return { data: { __typename: 'Query' } };
},
onOperation: onOperationFn,
});
const url = new URL(server.url);
url.searchParams.set('query', '{ __typename }');
const res = await fetch(url.toString());
expect(res.status).toBe(200);
expect(res.json()).resolves.toEqual({ data: { __typename: 'Query' } });
expect(onOperationFn).not.toBeCalled(); // early result, operation did not happen
});
it.each(['schema', 'context', 'onSubscribe', 'onOperation'])(
'should provide the request context to %s',
async (option) => {
const optionFn = jest.fn<(req: Request<unknown, unknown>) => void>();
const context = {};
const server = startTServer({
changeRequest: (req) => ({
...req,
context,
}),
[option]: optionFn,
});
const url = new URL(server.url);
url.searchParams.set('query', '{ __typename }');
await fetch(url.toString());
expect(optionFn.mock.calls[0][0]?.context).toBe(context);
},
);
it('should respond with error if execution result is iterable', async () => {
const server = startTServer({
// @ts-expect-error live queries for example
execute: () => {
return {
[Symbol.asyncIterator]() {
return this;
},
};
},
});
const url = new URL(server.url);
url.searchParams.set('query', '{ __typename }');
const result = await fetch(url.toString());
expect(result.json()).resolves.toEqual({
errors: [
{
message: 'Subscriptions are not supported',
},
],
});
});
it('should correctly serialise execution result errors', async () => {
const server = startTServer();
const url = new URL(server.url);
url.searchParams.set('query', 'query ($num: Int) { num(num: $num) }');
url.searchParams.set('variables', JSON.stringify({ num: 'foo' }));
const result = await fetch(url.toString());
expect(result.json()).resolves.toMatchInlineSnapshot(`
{
"errors": [
{
"locations": [
{
"column": 8,
"line": 1,
},
],
"message": "Variable "$num" got invalid value "foo"; Int cannot represent non-integer value: "foo"",
},
],
}
`);
});