@newmo/graphql-fake-server 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/bin.js +7 -7
- package/dist/esm/bin.js.map +1 -1
- package/dist/esm/cli.d.ts +7 -2
- package/dist/esm/cli.d.ts.map +1 -1
- package/dist/esm/cli.js +76 -19
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/createMock.d.ts +15 -0
- package/dist/esm/createMock.d.ts.map +1 -0
- package/dist/esm/createMock.js +39 -0
- package/dist/esm/createMock.js.map +1 -0
- package/dist/esm/index.d.ts +2 -20
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -65
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/server.d.ts +77 -0
- package/dist/esm/server.d.ts.map +1 -0
- package/dist/esm/server.js +262 -0
- package/dist/esm/server.js.map +1 -0
- package/package.json +8 -10
- package/src/bin.ts +7 -7
- package/src/cli.ts +82 -27
- package/src/createMock.ts +47 -0
- package/src/index.ts +2 -87
- package/src/server.ts +348 -0
package/src/server.ts
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import { ApolloServer } from "@apollo/server";
|
|
3
|
+
import { startStandaloneServer } from "@apollo/server/standalone";
|
|
4
|
+
import { addMocksToSchema } from "@graphql-tools/mock";
|
|
5
|
+
import { makeExecutableSchema } from "@graphql-tools/schema";
|
|
6
|
+
import { serve } from "@hono/node-server";
|
|
7
|
+
//@ts-expect-error
|
|
8
|
+
import depthLimit from "graphql-depth-limit";
|
|
9
|
+
import type { GraphQLSchema } from "graphql/index.js";
|
|
10
|
+
import { buildSchema } from "graphql/utilities/index.js";
|
|
11
|
+
import { type Context, Hono } from "hono";
|
|
12
|
+
import { type MockObject, createMock } from "./createMock.js";
|
|
13
|
+
import { type LogLevel, createLogger } from "./logger.js";
|
|
14
|
+
|
|
15
|
+
export type CreateFakeServerOptions = {
|
|
16
|
+
schemaFilePath: string;
|
|
17
|
+
ports?: {
|
|
18
|
+
fakeServer: number;
|
|
19
|
+
apolloServer: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* max query depth for complexity of query
|
|
23
|
+
* Default is 3
|
|
24
|
+
*/
|
|
25
|
+
maxQueryDepth?: number;
|
|
26
|
+
/**
|
|
27
|
+
* maxFieldRecursionDepth for creating fake data
|
|
28
|
+
* Default is maxDepth + 1
|
|
29
|
+
*/
|
|
30
|
+
maxFieldRecursionDepth?: number;
|
|
31
|
+
/**
|
|
32
|
+
* max number of registered sequences
|
|
33
|
+
* Default is 1000
|
|
34
|
+
* If the number of registered sequences exceeds this number, the oldest sequence is deleted.
|
|
35
|
+
*/
|
|
36
|
+
maxRegisteredSequences?: number;
|
|
37
|
+
logLevel?: LogLevel;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
type FakeServerInternal = {
|
|
41
|
+
schema: GraphQLSchema;
|
|
42
|
+
mockObject: MockObject;
|
|
43
|
+
ports: {
|
|
44
|
+
fakeServer: number;
|
|
45
|
+
apolloServer: number;
|
|
46
|
+
};
|
|
47
|
+
maxQueryDepth: number;
|
|
48
|
+
maxFieldRecursionDepth: number;
|
|
49
|
+
maxRegisteredSequences: number;
|
|
50
|
+
logLevel: LogLevel;
|
|
51
|
+
};
|
|
52
|
+
const creteApolloServer = async (options: FakeServerInternal) => {
|
|
53
|
+
const mocks = Object.fromEntries(
|
|
54
|
+
Object.entries(options.mockObject).map(([key, value]) => {
|
|
55
|
+
return [key, () => value];
|
|
56
|
+
}),
|
|
57
|
+
);
|
|
58
|
+
return new ApolloServer({
|
|
59
|
+
schema: addMocksToSchema({
|
|
60
|
+
schema: makeExecutableSchema({
|
|
61
|
+
typeDefs: options.schema,
|
|
62
|
+
}),
|
|
63
|
+
mocks,
|
|
64
|
+
}),
|
|
65
|
+
validationRules: [depthLimit(options.maxQueryDepth)],
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
export type RegisterSequenceNetworkError = {
|
|
69
|
+
type: "network-error";
|
|
70
|
+
operationName: string;
|
|
71
|
+
responseStatusCode: number;
|
|
72
|
+
errors: Record<string, unknown>[];
|
|
73
|
+
};
|
|
74
|
+
export type RegisterSequenceOperation = {
|
|
75
|
+
type: "operation";
|
|
76
|
+
operationName: string;
|
|
77
|
+
data: Record<string, unknown>;
|
|
78
|
+
};
|
|
79
|
+
export type RegisterSequenceOptions = RegisterSequenceNetworkError | RegisterSequenceOperation;
|
|
80
|
+
export type RegisterOperationResponse =
|
|
81
|
+
| {
|
|
82
|
+
ok: true;
|
|
83
|
+
}
|
|
84
|
+
| {
|
|
85
|
+
ok: false;
|
|
86
|
+
errors: string[];
|
|
87
|
+
};
|
|
88
|
+
const validateSequenceRegistration = (data: unknown): data is RegisterSequenceOptions => {
|
|
89
|
+
if (typeof data !== "object" || data === null) return false;
|
|
90
|
+
if ("type" in data && typeof data.type === "string") {
|
|
91
|
+
if (data.type === "network-error") {
|
|
92
|
+
return (
|
|
93
|
+
"errors" in data &&
|
|
94
|
+
Array.isArray(data.errors) &&
|
|
95
|
+
"responseStatusCode" in data &&
|
|
96
|
+
typeof data.responseStatusCode === "number" &&
|
|
97
|
+
"operationName" in data &&
|
|
98
|
+
typeof data.operationName === "string"
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
if (data.type === "operation") {
|
|
102
|
+
return (
|
|
103
|
+
"data" in data &&
|
|
104
|
+
typeof data.data === "object" &&
|
|
105
|
+
"operationName" in data &&
|
|
106
|
+
typeof data.operationName === "string"
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return false;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
class LRUMap<K, V> {
|
|
114
|
+
private map = new Map<K, V>();
|
|
115
|
+
private keys: K[] = [];
|
|
116
|
+
private maxSize: number;
|
|
117
|
+
|
|
118
|
+
constructor({ maxSize }: { maxSize: number }) {
|
|
119
|
+
this.maxSize = maxSize;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
set(key: K, value: V) {
|
|
123
|
+
this.map.set(key, value);
|
|
124
|
+
this.keys.push(key);
|
|
125
|
+
if (this.keys.length > this.maxSize) {
|
|
126
|
+
const oldestKey = this.keys.shift();
|
|
127
|
+
if (oldestKey) {
|
|
128
|
+
this.map.delete(oldestKey);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
get(key: K): V | undefined {
|
|
134
|
+
return this.map.get(key);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const createRoutingServer = async ({
|
|
139
|
+
logLevel,
|
|
140
|
+
ports,
|
|
141
|
+
maxRegisteredSequences,
|
|
142
|
+
}: {
|
|
143
|
+
logLevel: LogLevel;
|
|
144
|
+
maxRegisteredSequences: number;
|
|
145
|
+
ports: {
|
|
146
|
+
fakeServer: number;
|
|
147
|
+
apolloServer: number;
|
|
148
|
+
};
|
|
149
|
+
}) => {
|
|
150
|
+
const logger = createLogger(logLevel);
|
|
151
|
+
// pass through to apollo server
|
|
152
|
+
const passToApollo = async (c: Context) => {
|
|
153
|
+
// remove prefix
|
|
154
|
+
// prefix = /app1/*, path = /app1/a/b
|
|
155
|
+
// => suffix_path = /a/b
|
|
156
|
+
// let path = new URL(c.req.raw.url).pathname
|
|
157
|
+
let path = c.req.path;
|
|
158
|
+
path = path.replace(new RegExp(`^${c.req.routePath.replace("*", "")}`), "/");
|
|
159
|
+
let url = `http://127.0.0.1:${ports.apolloServer}${path}`;
|
|
160
|
+
// add params to URL
|
|
161
|
+
if (c.req.query()) url = `${url}?${new URLSearchParams(c.req.query())}`;
|
|
162
|
+
// request
|
|
163
|
+
const rep = await fetch(url, {
|
|
164
|
+
method: c.req.method,
|
|
165
|
+
headers: c.req.raw.headers,
|
|
166
|
+
body: c.req.raw.body,
|
|
167
|
+
duplex: "half",
|
|
168
|
+
});
|
|
169
|
+
// log response with pipe
|
|
170
|
+
if (rep.status === 101) return rep;
|
|
171
|
+
return new Response(rep.body, rep);
|
|
172
|
+
};
|
|
173
|
+
const sequenceLruMap = new LRUMap<string, RegisterSequenceOptions>({
|
|
174
|
+
maxSize: maxRegisteredSequences,
|
|
175
|
+
});
|
|
176
|
+
const app = new Hono();
|
|
177
|
+
app.post("/fake", async (c) => {
|
|
178
|
+
logger.debug("/fake");
|
|
179
|
+
const sequenceId = c.req.header("sequence-id");
|
|
180
|
+
if (!sequenceId) {
|
|
181
|
+
return Response.json(
|
|
182
|
+
JSON.stringify({ ok: false, errors: ["sequence-id is required"] }),
|
|
183
|
+
{
|
|
184
|
+
status: 400,
|
|
185
|
+
},
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
const body = await c.req.json();
|
|
189
|
+
logger.debug("/fake: got fake body", {
|
|
190
|
+
sequenceId,
|
|
191
|
+
body,
|
|
192
|
+
});
|
|
193
|
+
if (!validateSequenceRegistration(body)) {
|
|
194
|
+
return Response.json(JSON.stringify({ ok: false, errors: ["invalid fake body"] }), {
|
|
195
|
+
status: 400,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
logger.debug("/fake got body type", {
|
|
199
|
+
sequenceId,
|
|
200
|
+
type: body.type,
|
|
201
|
+
});
|
|
202
|
+
sequenceLruMap.set(sequenceId, body);
|
|
203
|
+
return Response.json(JSON.stringify({ ok: true }), {
|
|
204
|
+
status: 200,
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
const fakeGraphQLQuery = async (c: Context) => {
|
|
208
|
+
/**
|
|
209
|
+
* Steps:
|
|
210
|
+
* 1. Receive a request for a GraphQL query
|
|
211
|
+
* 2. Does it contain a sequence id?
|
|
212
|
+
* - if Yes: type is network error → return an error
|
|
213
|
+
* - if No: Pass through to Apollo Server -> exit
|
|
214
|
+
* 3. Send a request to Apollo Server
|
|
215
|
+
* 4. Merge the registration data with the response from 3
|
|
216
|
+
* 5. Return the merged data
|
|
217
|
+
*/
|
|
218
|
+
const sequenceId = c.req.header("sequence-id");
|
|
219
|
+
// 2. Does it contain a sequence id?
|
|
220
|
+
if (!sequenceId) return passToApollo(c);
|
|
221
|
+
const sequence = sequenceLruMap.get(sequenceId);
|
|
222
|
+
logger.debug(`/query: sequence-id: ${sequenceId}, sequence exists: ${Boolean(sequence)}`, {
|
|
223
|
+
sequence,
|
|
224
|
+
sequenceId,
|
|
225
|
+
});
|
|
226
|
+
if (!sequence) return passToApollo(c);
|
|
227
|
+
|
|
228
|
+
const requestBody = await c.req.raw.clone().json();
|
|
229
|
+
const requestOperationName =
|
|
230
|
+
typeof requestBody === "object" &&
|
|
231
|
+
requestBody !== null &&
|
|
232
|
+
"operationName" in requestBody &&
|
|
233
|
+
requestBody.operationName;
|
|
234
|
+
logger.debug(`operationName: ${requestOperationName} sequenceId: ${sequenceId}`, {
|
|
235
|
+
sequenceId,
|
|
236
|
+
});
|
|
237
|
+
if (requestOperationName !== sequence.operationName) {
|
|
238
|
+
return Response.json(
|
|
239
|
+
JSON.stringify({
|
|
240
|
+
errors: [
|
|
241
|
+
`operationName does not match. operationName: ${requestOperationName} sequenceId: ${sequenceId}`,
|
|
242
|
+
],
|
|
243
|
+
}),
|
|
244
|
+
{
|
|
245
|
+
status: 400,
|
|
246
|
+
},
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
if (sequence.type === "network-error") {
|
|
250
|
+
return new Response(JSON.stringify(sequence.errors), {
|
|
251
|
+
status: sequence.responseStatusCode,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
// 3. Send a request to Apollo Server
|
|
255
|
+
logger.debug("request to apollo-server", {
|
|
256
|
+
sequenceId,
|
|
257
|
+
});
|
|
258
|
+
const rep = await fetch(`http://127.0.0.1:${ports.apolloServer}/graphql`, {
|
|
259
|
+
method: c.req.method,
|
|
260
|
+
headers: c.req.raw.headers,
|
|
261
|
+
body: c.req.raw.body,
|
|
262
|
+
duplex: "half",
|
|
263
|
+
});
|
|
264
|
+
logger.debug("/query: response from apollo-server", {
|
|
265
|
+
sequenceId,
|
|
266
|
+
rep,
|
|
267
|
+
});
|
|
268
|
+
if (rep.status === 101) return rep;
|
|
269
|
+
// 4. Does the request contain a sequence id?
|
|
270
|
+
const responseBody = await rep.json();
|
|
271
|
+
// 5. Merge the registration data with the response from 2
|
|
272
|
+
const data = sequence.data;
|
|
273
|
+
logger.debug(`/query: merge sequence-id: ${sequenceId}`, {
|
|
274
|
+
data,
|
|
275
|
+
responseBody,
|
|
276
|
+
});
|
|
277
|
+
const merged = {
|
|
278
|
+
//@ts-expect-error
|
|
279
|
+
...responseBody.data,
|
|
280
|
+
...data,
|
|
281
|
+
};
|
|
282
|
+
return Response.json(
|
|
283
|
+
{
|
|
284
|
+
data: merged,
|
|
285
|
+
},
|
|
286
|
+
rep,
|
|
287
|
+
);
|
|
288
|
+
};
|
|
289
|
+
app.use("/graphql", fakeGraphQLQuery);
|
|
290
|
+
app.use("/query", fakeGraphQLQuery);
|
|
291
|
+
app.all("*", passToApollo);
|
|
292
|
+
return app;
|
|
293
|
+
};
|
|
294
|
+
export const createFakeServer = async (options: CreateFakeServerOptions) => {
|
|
295
|
+
const schema = buildSchema(await fs.readFile(options.schemaFilePath, "utf-8"));
|
|
296
|
+
const mockObject = await createMock({
|
|
297
|
+
schema,
|
|
298
|
+
logLevel: options.logLevel,
|
|
299
|
+
maxFieldRecursionDepth: options.maxFieldRecursionDepth,
|
|
300
|
+
});
|
|
301
|
+
const ports = {
|
|
302
|
+
fakeServer: options.ports?.fakeServer ?? 4000,
|
|
303
|
+
apolloServer: options.ports?.apolloServer ?? 4001,
|
|
304
|
+
};
|
|
305
|
+
const maxQueryDepth = options.maxQueryDepth ?? 3;
|
|
306
|
+
const maxFieldRecursionDepth = options.maxFieldRecursionDepth ?? maxQueryDepth + 1;
|
|
307
|
+
const maxRegisteredSequences = options.maxRegisteredSequences ?? 1000;
|
|
308
|
+
return createFakeServerInternal({
|
|
309
|
+
ports,
|
|
310
|
+
schema,
|
|
311
|
+
mockObject,
|
|
312
|
+
maxQueryDepth,
|
|
313
|
+
maxFieldRecursionDepth,
|
|
314
|
+
maxRegisteredSequences,
|
|
315
|
+
logLevel: options.logLevel ?? "info",
|
|
316
|
+
});
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
export const createFakeServerInternal = async (options: FakeServerInternal) => {
|
|
320
|
+
const apolloServer = await creteApolloServer(options);
|
|
321
|
+
const routingServer = await createRoutingServer({
|
|
322
|
+
logLevel: options.logLevel,
|
|
323
|
+
ports: options.ports,
|
|
324
|
+
maxRegisteredSequences: options.maxRegisteredSequences,
|
|
325
|
+
});
|
|
326
|
+
let routerServer: ReturnType<typeof serve> | null = null;
|
|
327
|
+
return {
|
|
328
|
+
start: async () => {
|
|
329
|
+
const { url } = await startStandaloneServer(apolloServer, {
|
|
330
|
+
listen: { port: options.ports.apolloServer },
|
|
331
|
+
});
|
|
332
|
+
routerServer = serve({
|
|
333
|
+
fetch: routingServer.fetch,
|
|
334
|
+
port: options.ports.fakeServer,
|
|
335
|
+
});
|
|
336
|
+
return {
|
|
337
|
+
urls: {
|
|
338
|
+
fakeServer: `http://127.0.0.1:${options.ports.fakeServer}`,
|
|
339
|
+
apolloServer: `http://127.0.0.1:${options.ports.apolloServer}`,
|
|
340
|
+
},
|
|
341
|
+
};
|
|
342
|
+
},
|
|
343
|
+
stop: () => {
|
|
344
|
+
apolloServer.stop();
|
|
345
|
+
routerServer?.close();
|
|
346
|
+
},
|
|
347
|
+
};
|
|
348
|
+
};
|