@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/dist/esm/bin.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { run } from "./cli.js";
|
|
3
3
|
const ret = await run();
|
|
4
|
-
if (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
if (ret.stdout) {
|
|
5
|
+
console.log(ret.stdout);
|
|
6
|
+
}
|
|
7
|
+
if (ret.stderr) {
|
|
8
|
+
console.error(ret.stderr);
|
|
9
|
+
}
|
|
10
|
+
if (!ret.doNotExit) {
|
|
11
11
|
process.exit(ret.exitCode);
|
|
12
12
|
}
|
|
13
13
|
//# sourceMappingURL=bin.js.map
|
package/dist/esm/bin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,MAAM,GAAG,GAAG,MAAM,GAAG,EAAE,CAAC;AACxB,IAAI,
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,MAAM,GAAG,GAAG,MAAM,GAAG,EAAE,CAAC;AACxB,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;IACb,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AAC5B,CAAC;AACD,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;IACb,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AAC9B,CAAC;AACD,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,CAAC;IACjB,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;AAC/B,CAAC"}
|
package/dist/esm/cli.d.ts
CHANGED
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
export declare const cli: {
|
|
3
3
|
values: {
|
|
4
4
|
schema: string | undefined;
|
|
5
|
-
|
|
5
|
+
mainPort: string | undefined;
|
|
6
|
+
apolloPort: string | undefined;
|
|
7
|
+
maxRegisteredSequences: string | undefined;
|
|
8
|
+
maxQueryDepth: string | undefined;
|
|
9
|
+
maxFieldRecursionDepth: string | undefined;
|
|
6
10
|
logLevel: string | undefined;
|
|
7
11
|
};
|
|
8
12
|
positionals: [];
|
|
@@ -11,5 +15,6 @@ export declare const run: ({ values, }?: typeof cli) => Promise<{
|
|
|
11
15
|
stdout: string;
|
|
12
16
|
stderr: string | Error;
|
|
13
17
|
exitCode: number;
|
|
14
|
-
|
|
18
|
+
doNotExit?: boolean;
|
|
19
|
+
}>;
|
|
15
20
|
//# sourceMappingURL=cli.d.ts.map
|
package/dist/esm/cli.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":";
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":";AAgBA,eAAO,MAAM,GAAG;;;;;;;;;;;CAuCd,CAAC;AACH,eAAO,MAAM,GAAG,iBAEb,UAAU,KAAS,QAAQ;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,EAAE,OAAO,CAAC;CACvB,CA2FA,CAAC"}
|
package/dist/esm/cli.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import * as fs from "node:fs/promises";
|
|
3
2
|
import { parseArgs } from "node:util";
|
|
4
|
-
import {
|
|
5
|
-
import { createMock, startFakeServer } from "./index.js";
|
|
3
|
+
import { createFakeServer } from "./index.js";
|
|
6
4
|
import { createLogger } from "./logger.js";
|
|
7
5
|
const HELP = `
|
|
8
6
|
Usage: npx @newmo/graphql-fake-server --schema <path> [options]
|
|
@@ -23,12 +21,31 @@ export const cli = parseArgs({
|
|
|
23
21
|
type: "string",
|
|
24
22
|
description: "Path to the schema file. e.g. schema.graphql",
|
|
25
23
|
},
|
|
26
|
-
|
|
27
|
-
port: {
|
|
24
|
+
mainPort: {
|
|
28
25
|
type: "string",
|
|
29
26
|
description: "Port to run the server on",
|
|
30
27
|
default: "4000",
|
|
31
28
|
},
|
|
29
|
+
apolloPort: {
|
|
30
|
+
type: "string",
|
|
31
|
+
description: "Port to run the server on",
|
|
32
|
+
default: "4002",
|
|
33
|
+
},
|
|
34
|
+
maxRegisteredSequences: {
|
|
35
|
+
type: "string",
|
|
36
|
+
description: "Max number of registered sequences.",
|
|
37
|
+
default: "1000",
|
|
38
|
+
},
|
|
39
|
+
maxQueryDepth: {
|
|
40
|
+
type: "string",
|
|
41
|
+
description: "max query depth for complexity of query",
|
|
42
|
+
default: "3",
|
|
43
|
+
},
|
|
44
|
+
maxFieldRecursionDepth: {
|
|
45
|
+
type: "string",
|
|
46
|
+
description: "maxFieldRecursionDepth for creating fake data",
|
|
47
|
+
default: "4",
|
|
48
|
+
},
|
|
32
49
|
logLevel: {
|
|
33
50
|
type: "string",
|
|
34
51
|
description: "log level: debug, info, warn, error",
|
|
@@ -55,28 +72,68 @@ export const run = async ({ values, } = cli) => {
|
|
|
55
72
|
exitCode: 1,
|
|
56
73
|
};
|
|
57
74
|
}
|
|
58
|
-
const
|
|
59
|
-
|
|
75
|
+
const mainPort = values.mainPort ? Number.parseInt(values.mainPort, 10) : Number.NaN;
|
|
76
|
+
const apolloPort = values.apolloPort ? Number.parseInt(values.apolloPort, 10) : Number.NaN;
|
|
77
|
+
if (Number.isNaN(mainPort) || Number.isNaN(apolloPort)) {
|
|
78
|
+
logger.info(HELP);
|
|
79
|
+
return {
|
|
80
|
+
stdout: "",
|
|
81
|
+
stderr: "port must be a number",
|
|
82
|
+
exitCode: 1,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
const maxFieldRecursionDepth = values.maxFieldRecursionDepth
|
|
86
|
+
? Number.parseInt(values.maxFieldRecursionDepth, 10)
|
|
87
|
+
: Number.NaN;
|
|
88
|
+
if (Number.isNaN(maxFieldRecursionDepth)) {
|
|
60
89
|
logger.info(HELP);
|
|
61
90
|
return {
|
|
62
91
|
stdout: "",
|
|
63
|
-
stderr: "
|
|
92
|
+
stderr: "maxFieldRecursionDepth must be a number",
|
|
93
|
+
exitCode: 1,
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const maxQueryDepth = values.maxQueryDepth
|
|
97
|
+
? Number.parseInt(values.maxQueryDepth, 10)
|
|
98
|
+
: Number.NaN;
|
|
99
|
+
if (Number.isNaN(maxQueryDepth)) {
|
|
100
|
+
logger.info(HELP);
|
|
101
|
+
return {
|
|
102
|
+
stdout: "",
|
|
103
|
+
stderr: "maxQueryDepth must be a number",
|
|
104
|
+
exitCode: 1,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
const maxRegisteredSequences = values.maxRegisteredSequences
|
|
108
|
+
? Number.parseInt(values.maxRegisteredSequences, 10)
|
|
109
|
+
: Number.NaN;
|
|
110
|
+
if (Number.isNaN(maxRegisteredSequences)) {
|
|
111
|
+
logger.info(HELP);
|
|
112
|
+
return {
|
|
113
|
+
stdout: "",
|
|
114
|
+
stderr: "maxRegisteredSequences must be a number",
|
|
64
115
|
exitCode: 1,
|
|
65
116
|
};
|
|
66
117
|
}
|
|
67
118
|
try {
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
119
|
+
const server = await createFakeServer({
|
|
120
|
+
schemaFilePath: schemaPath,
|
|
121
|
+
maxRegisteredSequences,
|
|
122
|
+
maxFieldRecursionDepth,
|
|
123
|
+
maxQueryDepth,
|
|
124
|
+
ports: {
|
|
125
|
+
fakeServer: mainPort,
|
|
126
|
+
apolloServer: apolloPort,
|
|
127
|
+
},
|
|
77
128
|
});
|
|
78
|
-
|
|
79
|
-
|
|
129
|
+
const { urls } = await server.start();
|
|
130
|
+
logger.info(`🚀 GraphQL Fake Server listening at: ${urls.fakeServer}`);
|
|
131
|
+
return {
|
|
132
|
+
stdout: "",
|
|
133
|
+
stderr: "",
|
|
134
|
+
exitCode: 0,
|
|
135
|
+
doNotExit: true,
|
|
136
|
+
};
|
|
80
137
|
}
|
|
81
138
|
catch (error) {
|
|
82
139
|
logger.error(error);
|
package/dist/esm/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRo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CAAC,CAAC;QACH,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;QACtC,MAAM,CAAC,IAAI,CAAC,wCAAwC,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC;QACvE,OAAO;YACH,MAAM,EAAE,EAAE;YACV,MAAM,EAAE,EAAE;YACV,QAAQ,EAAE,CAAC;YACX,SAAS,EAAE,IAAI;SAClB,CAAC;IACN,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACpB,OAAO;YACH,MAAM,EAAE,EAAE;YACV,MAAM,EAAE,IAAI,KAAK,CAAC,wBAAwB,EAAE;gBACxC,KAAK,EAAE,KAAK;aACf,CAAC;YACF,QAAQ,EAAE,CAAC;SACd,CAAC;IACN,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { GraphQLSchema } from "graphql/index.js";
|
|
2
|
+
import { type LogLevel } from "./logger.js";
|
|
3
|
+
export type MockObject = Record<string, unknown>;
|
|
4
|
+
export type GenerateMockOptions = {
|
|
5
|
+
schema: GraphQLSchema;
|
|
6
|
+
maxFieldRecursionDepth?: number | undefined;
|
|
7
|
+
logLevel?: LogLevel | undefined;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Create mock object from schema
|
|
11
|
+
* It supports @example directive
|
|
12
|
+
* @param options
|
|
13
|
+
*/
|
|
14
|
+
export declare const createMock: (options: GenerateMockOptions) => Promise<MockObject>;
|
|
15
|
+
//# sourceMappingURL=createMock.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createMock.d.ts","sourceRoot":"","sources":["../../src/createMock.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,KAAK,QAAQ,EAAgB,MAAM,aAAa,CAAC;AAC1D,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACjD,MAAM,MAAM,mBAAmB,GAAG;IAC9B,MAAM,EAAE,aAAa,CAAC;IACtB,sBAAsB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5C,QAAQ,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAC;CACnC,CAAC;AAIF;;;;GAIG;AACH,eAAO,MAAM,UAAU,YAAmB,mBAAmB,KAAG,QAAQ,UAAU,CA4BjF,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import vm from "node:vm";
|
|
2
|
+
import { generateCode, getTypeInfos, normalizeConfig } from "@newmo/graphql-fake-core";
|
|
3
|
+
import { createLogger } from "./logger.js";
|
|
4
|
+
const cloneAsJSON = (obj) => {
|
|
5
|
+
return JSON.parse(JSON.stringify(obj));
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Create mock object from schema
|
|
9
|
+
* It supports @example directive
|
|
10
|
+
* @param options
|
|
11
|
+
*/
|
|
12
|
+
export const createMock = async (options) => {
|
|
13
|
+
const logger = createLogger(options.logLevel);
|
|
14
|
+
try {
|
|
15
|
+
const normalizedConfig = normalizeConfig({
|
|
16
|
+
maxFieldRecursionDepth: options.maxFieldRecursionDepth ?? 3,
|
|
17
|
+
});
|
|
18
|
+
const typeInfos = getTypeInfos(normalizedConfig, options.schema);
|
|
19
|
+
const code = generateCode({
|
|
20
|
+
...normalizedConfig,
|
|
21
|
+
outputType: "commonjs",
|
|
22
|
+
}, typeInfos);
|
|
23
|
+
logger.debug("Generated code:");
|
|
24
|
+
logger.debug(code);
|
|
25
|
+
// execute code in vm and get all exports
|
|
26
|
+
const exports = {};
|
|
27
|
+
vm.runInNewContext(code, { exports });
|
|
28
|
+
// Apollo Server does not support Function type in mock object
|
|
29
|
+
const plainObject = cloneAsJSON(exports);
|
|
30
|
+
logger.debug("Exports:");
|
|
31
|
+
logger.debug(JSON.stringify(plainObject, null, 2));
|
|
32
|
+
return plainObject;
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
logger.error(error);
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=createMock.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createMock.js","sourceRoot":"","sources":["../../src/createMock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAEvF,OAAO,EAAiB,YAAY,EAAE,MAAM,aAAa,CAAC;AAO1D,MAAM,WAAW,GAAG,CAAC,GAAY,EAAE,EAAE;IACjC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC;AAC3C,CAAC,CAAC;AACF;;;;GAIG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAAE,OAA4B,EAAuB,EAAE;IAClF,MAAM,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC9C,IAAI,CAAC;QACD,MAAM,gBAAgB,GAAG,eAAe,CAAC;YACrC,sBAAsB,EAAE,OAAO,CAAC,sBAAsB,IAAI,CAAC;SAC9D,CAAC,CAAC;QACH,MAAM,SAAS,GAAG,YAAY,CAAC,gBAAgB,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QACjE,MAAM,IAAI,GAAG,YAAY,CACrB;YACI,GAAG,gBAAgB;YACnB,UAAU,EAAE,UAAU;SACzB,EACD,SAAS,CACZ,CAAC;QACF,MAAM,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;QAChC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QACnB,yCAAyC;QACzC,MAAM,OAAO,GAAG,EAAE,CAAC;QACnB,EAAE,CAAC,eAAe,CAAC,IAAI,EAAE,EAAE,OAAO,EAAE,CAAC,CAAC;QACtC,8DAA8D;QAC9D,MAAM,WAAW,GAAG,WAAW,CAAC,OAAO,CAAC,CAAC;QACzC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACzB,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;QACnD,OAAO,WAAW,CAAC;IACvB,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACpB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACpB,CAAC;AACL,CAAC,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,21 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export type MockObject = Record<string, unknown>;
|
|
4
|
-
export type StartFakeServerOptions = {
|
|
5
|
-
schema: GraphQLSchema;
|
|
6
|
-
mockObject: MockObject;
|
|
7
|
-
port?: number;
|
|
8
|
-
logLevel?: LogLevel;
|
|
9
|
-
};
|
|
10
|
-
export declare const startFakeServer: ({ schema, mockObject, port, logLevel, }: StartFakeServerOptions) => Promise<() => void>;
|
|
11
|
-
export type GenerateMockOptions = {
|
|
12
|
-
schema: GraphQLSchema;
|
|
13
|
-
logLevel?: LogLevel;
|
|
14
|
-
};
|
|
15
|
-
/**
|
|
16
|
-
* Create mock object from schema
|
|
17
|
-
* It supports @example directive
|
|
18
|
-
* @param options
|
|
19
|
-
*/
|
|
20
|
-
export declare const createMock: (options: GenerateMockOptions) => Promise<MockObject>;
|
|
1
|
+
export { createFakeServer } from "./server.js";
|
|
2
|
+
export type { CreateFakeServerOptions } from "./server.js";
|
|
21
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/esm/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/esm/index.js
CHANGED
|
@@ -1,66 +1,2 @@
|
|
|
1
|
-
|
|
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 { generateCode, getTypeInfos, normalizeConfig } from "@newmo/graphql-fake-core";
|
|
7
|
-
//@ts-expect-error
|
|
8
|
-
import depthLimit from "graphql-depth-limit";
|
|
9
|
-
import { createLogger } from "./logger.js";
|
|
10
|
-
export const startFakeServer = async ({ schema, mockObject, port, logLevel, }) => {
|
|
11
|
-
const logger = createLogger(logLevel);
|
|
12
|
-
const mocks = Object.fromEntries(Object.entries(mockObject).map(([key, value]) => {
|
|
13
|
-
return [key, () => value];
|
|
14
|
-
}));
|
|
15
|
-
const server = new ApolloServer({
|
|
16
|
-
schema: addMocksToSchema({
|
|
17
|
-
schema: makeExecutableSchema({
|
|
18
|
-
typeDefs: schema,
|
|
19
|
-
}),
|
|
20
|
-
mocks,
|
|
21
|
-
}),
|
|
22
|
-
validationRules: [depthLimit(3)],
|
|
23
|
-
});
|
|
24
|
-
const { url } = await startStandaloneServer(server, { listen: { port: port } });
|
|
25
|
-
logger.info(`🚀 Server listening at: ${url}`);
|
|
26
|
-
return () => {
|
|
27
|
-
// close
|
|
28
|
-
server.stop();
|
|
29
|
-
};
|
|
30
|
-
};
|
|
31
|
-
const cloneAsJSON = (obj) => {
|
|
32
|
-
return JSON.parse(JSON.stringify(obj));
|
|
33
|
-
};
|
|
34
|
-
/**
|
|
35
|
-
* Create mock object from schema
|
|
36
|
-
* It supports @example directive
|
|
37
|
-
* @param options
|
|
38
|
-
*/
|
|
39
|
-
export const createMock = async (options) => {
|
|
40
|
-
const logger = createLogger(options.logLevel);
|
|
41
|
-
try {
|
|
42
|
-
const normalizedConfig = normalizeConfig({
|
|
43
|
-
maxFieldRecursionDepth: 4,
|
|
44
|
-
});
|
|
45
|
-
const typeInfos = getTypeInfos(normalizedConfig, options.schema);
|
|
46
|
-
const code = generateCode({
|
|
47
|
-
...normalizedConfig,
|
|
48
|
-
outputType: "commonjs",
|
|
49
|
-
}, typeInfos);
|
|
50
|
-
logger.debug("Generated code:");
|
|
51
|
-
logger.debug(code);
|
|
52
|
-
// execute code in vm and get all exports
|
|
53
|
-
const exports = {};
|
|
54
|
-
vm.runInNewContext(code, { exports });
|
|
55
|
-
// Apollo Server does not support Function type in mock object
|
|
56
|
-
const plainObject = cloneAsJSON(exports);
|
|
57
|
-
logger.debug("Exports:");
|
|
58
|
-
logger.debug(JSON.stringify(plainObject, null, 2));
|
|
59
|
-
return plainObject;
|
|
60
|
-
}
|
|
61
|
-
catch (error) {
|
|
62
|
-
logger.error(error);
|
|
63
|
-
process.exit(1);
|
|
64
|
-
}
|
|
65
|
-
};
|
|
1
|
+
export { createFakeServer } from "./server.js";
|
|
66
2
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { GraphQLSchema } from "graphql/index.js";
|
|
2
|
+
import { type MockObject } from "./createMock.js";
|
|
3
|
+
import { type LogLevel } from "./logger.js";
|
|
4
|
+
export type CreateFakeServerOptions = {
|
|
5
|
+
schemaFilePath: string;
|
|
6
|
+
ports?: {
|
|
7
|
+
fakeServer: number;
|
|
8
|
+
apolloServer: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* max query depth for complexity of query
|
|
12
|
+
* Default is 3
|
|
13
|
+
*/
|
|
14
|
+
maxQueryDepth?: number;
|
|
15
|
+
/**
|
|
16
|
+
* maxFieldRecursionDepth for creating fake data
|
|
17
|
+
* Default is maxDepth + 1
|
|
18
|
+
*/
|
|
19
|
+
maxFieldRecursionDepth?: number;
|
|
20
|
+
/**
|
|
21
|
+
* max number of registered sequences
|
|
22
|
+
* Default is 1000
|
|
23
|
+
* If the number of registered sequences exceeds this number, the oldest sequence is deleted.
|
|
24
|
+
*/
|
|
25
|
+
maxRegisteredSequences?: number;
|
|
26
|
+
logLevel?: LogLevel;
|
|
27
|
+
};
|
|
28
|
+
type FakeServerInternal = {
|
|
29
|
+
schema: GraphQLSchema;
|
|
30
|
+
mockObject: MockObject;
|
|
31
|
+
ports: {
|
|
32
|
+
fakeServer: number;
|
|
33
|
+
apolloServer: number;
|
|
34
|
+
};
|
|
35
|
+
maxQueryDepth: number;
|
|
36
|
+
maxFieldRecursionDepth: number;
|
|
37
|
+
maxRegisteredSequences: number;
|
|
38
|
+
logLevel: LogLevel;
|
|
39
|
+
};
|
|
40
|
+
export type RegisterSequenceNetworkError = {
|
|
41
|
+
type: "network-error";
|
|
42
|
+
operationName: string;
|
|
43
|
+
responseStatusCode: number;
|
|
44
|
+
errors: Record<string, unknown>[];
|
|
45
|
+
};
|
|
46
|
+
export type RegisterSequenceOperation = {
|
|
47
|
+
type: "operation";
|
|
48
|
+
operationName: string;
|
|
49
|
+
data: Record<string, unknown>;
|
|
50
|
+
};
|
|
51
|
+
export type RegisterSequenceOptions = RegisterSequenceNetworkError | RegisterSequenceOperation;
|
|
52
|
+
export type RegisterOperationResponse = {
|
|
53
|
+
ok: true;
|
|
54
|
+
} | {
|
|
55
|
+
ok: false;
|
|
56
|
+
errors: string[];
|
|
57
|
+
};
|
|
58
|
+
export declare const createFakeServer: (options: CreateFakeServerOptions) => Promise<{
|
|
59
|
+
start: () => Promise<{
|
|
60
|
+
urls: {
|
|
61
|
+
fakeServer: string;
|
|
62
|
+
apolloServer: string;
|
|
63
|
+
};
|
|
64
|
+
}>;
|
|
65
|
+
stop: () => void;
|
|
66
|
+
}>;
|
|
67
|
+
export declare const createFakeServerInternal: (options: FakeServerInternal) => Promise<{
|
|
68
|
+
start: () => Promise<{
|
|
69
|
+
urls: {
|
|
70
|
+
fakeServer: string;
|
|
71
|
+
apolloServer: string;
|
|
72
|
+
};
|
|
73
|
+
}>;
|
|
74
|
+
stop: () => void;
|
|
75
|
+
}>;
|
|
76
|
+
export {};
|
|
77
|
+
//# sourceMappingURL=server.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAGtD,OAAO,EAAE,KAAK,UAAU,EAAc,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAE,KAAK,QAAQ,EAAgB,MAAM,aAAa,CAAC;AAE1D,MAAM,MAAM,uBAAuB,GAAG;IAClC,cAAc,EAAE,MAAM,CAAC;IACvB,KAAK,CAAC,EAAE;QACJ,UAAU,EAAE,MAAM,CAAC;QACnB,YAAY,EAAE,MAAM,CAAC;KACxB,CAAC;IACF;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,MAAM,CAAC;IAChC,QAAQ,CAAC,EAAE,QAAQ,CAAC;CACvB,CAAC;AAEF,KAAK,kBAAkB,GAAG;IACtB,MAAM,EAAE,aAAa,CAAC;IACtB,UAAU,EAAE,U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