This repository has been archived by the owner on May 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.node.js
128 lines (112 loc) · 3.02 KB
/
server.node.js
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
123
124
125
126
127
128
// @flow
import {getSimulator} from 'fusion-test-utils';
import App from 'fusion-core';
import tape from 'tape-cup';
import {addMockFunctionsToSchema, makeExecutableSchema} from 'graphql-tools';
import ApolloServer from '../index';
import {
ApolloServerEndpointToken,
ApolloServerFormatFunctionToken,
} from '../tokens';
import {GraphQLSchemaToken} from 'fusion-apollo';
import type {Context} from 'fusion-core';
const typeDefs = `
type Query {
getHello: Test
}
type Test {
name: String
}
`;
const query = {
operationName: null,
variables: {},
query: `query Test { getHello { name }}`,
};
export const schema = makeExecutableSchema({
typeDefs,
});
addMockFunctionsToSchema({schema});
tape('handler should serve queries on the specified endpoint', async t => {
const app = new App('el', el => el);
app.register(ApolloServer);
app.register(ApolloServerEndpointToken, '/graphql');
app.register(GraphQLSchemaToken, schema);
const simulator = getSimulator(app);
const ctx: Context = await simulator.request('/graphql', {
body: query,
method: 'POST',
});
t.equal(ctx.status, 200);
t.equal(JSON.parse(String(ctx.body)).data.getHello.name, 'Hello World');
t.end();
});
tape('Downstream Error handling should work', async t => {
const app = new App('el', el => el);
app.middleware((ctx, next) => {
throw new Error('FAIL');
});
app.register(ApolloServer);
app.register(ApolloServerEndpointToken, '/graphql');
app.register(GraphQLSchemaToken, schema);
const simulator = getSimulator(app);
try {
await simulator.request('/graphql', {
body: query,
method: 'POST',
});
t.fail('Should not reach this');
} catch (e) {
t.ok(e, 'has error');
t.end();
}
});
tape('Upstream Error handling should work', async t => {
const app = new App('el', el => el);
app.register(ApolloServer);
app.register(ApolloServerEndpointToken, '/graphql');
app.register(GraphQLSchemaToken, schema);
app.middleware((ctx, next) => {
throw new Error('FAIL');
});
const simulator = getSimulator(app);
try {
await simulator.request('/graphql', {
body: query,
method: 'POST',
});
t.fail('Should not reach this');
} catch (e) {
t.ok(e, 'has error');
t.end();
}
});
tape('Format error', async t => {
const schema = makeExecutableSchema({
typeDefs,
});
addMockFunctionsToSchema({
schema,
mocks: {
Test: () => {
throw new Error('resolver error');
},
},
});
const app = new App('el', el => el);
app.register(ApolloServer);
app.register(ApolloServerEndpointToken, '/graphql');
const formatError = error => {
return {text: 'formatted error'};
};
app.register(GraphQLSchemaToken, schema);
app.register(ApolloServerFormatFunctionToken, formatError);
const simulator = getSimulator(app);
const ctx: Context = await simulator.request('/graphql', {
body: query,
method: 'POST',
});
const response = JSON.parse(String(ctx.body));
t.deepEqual(response.errors[0], {text: 'formatted error'});
t.end();
});