-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathrepro.spec.ts
167 lines (148 loc) · 3.04 KB
/
repro.spec.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { buildSchema, GraphQLError } from 'graphql';
import { useSofa } from '../src';
import { createSchema } from 'graphql-yoga';
function generateObjectType(
name: string,
fieldSet: Record<string, string>
): string {
const fields: string[] = [];
for (const fieldName in fieldSet) {
const fieldType = fieldSet[fieldName];
fields.push(generateField(fieldName, fieldType));
}
return `
type ${name} {
${fields}
}
`;
}
function generateField(name: string, type: string) {
return `${name}: ${type}`;
}
function generateMany(
type: string,
prefix: string,
count: number
): Record<string, string> {
const name = prefix + type;
const fieldSet: Record<string, string> = {};
while (count--) {
fieldSet[`${name}_${count}`] = type;
}
return fieldSet;
}
export function generate({
mutations,
queries,
subscriptions,
}: {
mutations: number;
queries: number;
subscriptions: number;
}): string {
const typeA = `
type FooA {
id: ID!
name: String!
createdAt: Float!
foo: FooB
}
`;
const typeB = `
type FooB {
id: ID!
name: String!
createdAt: Float!
foo: FooC
}
`;
const typeC = `
type FooC {
id: ID!
name: String!
createdAt: Float!
}
`;
const type = `
type Foo {
id: ID!
name: String!
createdAt: Float!
foo: FooA
}
`;
const results: string[] = [type, typeA, typeB, typeC];
if (queries) {
results.push(
generateObjectType('Query', generateMany('Foo', 'get', queries))
);
}
if (mutations) {
results.push(
generateObjectType('Mutation', generateMany('Foo', 'create', mutations))
);
}
if (subscriptions) {
results.push(
generateObjectType(
'Subscription',
generateMany('Foo', 'listen', subscriptions)
)
);
}
return results.join('\n');
}
test('memory issue', async () => {
const schema = buildSchema(
generate({
queries: 1000,
mutations: 1000,
subscriptions: 1000,
})
);
useSofa({ basePath: '/api', schema });
});
test('error extensions', async () => {
const router = useSofa({
basePath: '/api',
schema: createSchema({
typeDefs: /* GraphQL */ `
type Query {
me: Account
}
type Account {
id: ID!
name: String!
}
`,
resolvers: {
Query: {
me: () => {
throw new GraphQLError('account not found', {
extensions: {
code: 'ACCOUNT_NOT_FOUND',
http: { status: 404 },
},
});
},
},
},
}),
});
for (let i = 0; i < 10; i++) {
const res = await router.fetch('http://localhost/api/me');
expect(res.status).toBe(404);
const json = await res.json();
expect(json).toEqual({
errors: [
{
message: 'account not found',
extensions: {
code: 'ACCOUNT_NOT_FOUND',
},
path: ['me'],
},
],
});
}
});