@newmo/graphql-fake-server 0.9.7 → 0.10.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/README.md +48 -0
- package/dist/esm/cli.d.ts +1 -5
- package/dist/esm/cli.d.ts.map +1 -1
- package/dist/esm/cli.js +21 -82
- package/dist/esm/cli.js.map +1 -1
- package/dist/esm/config.d.ts +63 -0
- package/dist/esm/config.d.ts.map +1 -0
- package/dist/esm/config.js +58 -0
- package/dist/esm/config.js.map +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/server.d.ts +2 -22
- package/dist/esm/server.d.ts.map +1 -1
- package/dist/esm/server.js +6 -11
- package/dist/esm/server.js.map +1 -1
- package/package.json +3 -3
- package/src/cli.ts +23 -83
- package/src/config.ts +120 -0
- package/src/index.ts +5 -0
- package/src/server.ts +16 -33
package/README.md
CHANGED
|
@@ -12,6 +12,54 @@ See <https://github.com/newmo-oss/graphql-fake-server>
|
|
|
12
12
|
- Support `/fake` API
|
|
13
13
|
- Support `/graphql` API
|
|
14
14
|
|
|
15
|
+
## Config
|
|
16
|
+
|
|
17
|
+
Please See [src/config.ts](src/config.ts)
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
/**
|
|
21
|
+
* Configuration for the fake server.
|
|
22
|
+
*/
|
|
23
|
+
export type FakeServerConfig = {
|
|
24
|
+
/**
|
|
25
|
+
* The path to the GraphQL schema file.
|
|
26
|
+
*/
|
|
27
|
+
schemaFilePath: string;
|
|
28
|
+
ports?: {
|
|
29
|
+
/**
|
|
30
|
+
* Fake Server port.
|
|
31
|
+
* Default is 4000.
|
|
32
|
+
*/
|
|
33
|
+
fakeServer?: number | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Apollo Server port.
|
|
36
|
+
* It provides the GraphQL Playground.
|
|
37
|
+
* Default is 4002.
|
|
38
|
+
*/
|
|
39
|
+
apolloServer?: number | undefined;
|
|
40
|
+
} | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* The maximum number of registered sequences.
|
|
43
|
+
* Default is 1000.
|
|
44
|
+
*/
|
|
45
|
+
maxRegisteredSequences?: number | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* The maximum number of depth of field recursion.
|
|
48
|
+
* Default is 3.
|
|
49
|
+
*/
|
|
50
|
+
maxFieldRecursionDepth?: RawConfig["maxFieldRecursionDepth"] | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* The maximum number of depth of complexity of query
|
|
53
|
+
* Default is 4
|
|
54
|
+
*/
|
|
55
|
+
maxQueryDepth?: number | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* Default values for scalar types.
|
|
58
|
+
*/
|
|
59
|
+
defaultValues?: RawConfig["defaultValues"] | undefined;
|
|
60
|
+
};
|
|
61
|
+
```
|
|
62
|
+
|
|
15
63
|
## Tests
|
|
16
64
|
|
|
17
65
|
```sh
|
package/dist/esm/cli.d.ts
CHANGED
|
@@ -2,11 +2,7 @@
|
|
|
2
2
|
export declare const cli: {
|
|
3
3
|
values: {
|
|
4
4
|
schema: string | undefined;
|
|
5
|
-
|
|
6
|
-
apolloPort: string | undefined;
|
|
7
|
-
maxRegisteredSequences: string | undefined;
|
|
8
|
-
maxQueryDepth: string | undefined;
|
|
9
|
-
maxFieldRecursionDepth: string | undefined;
|
|
5
|
+
config: string | undefined;
|
|
10
6
|
logLevel: string | undefined;
|
|
11
7
|
};
|
|
12
8
|
positionals: [];
|
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":";AAwBA,eAAO,MAAM,GAAG;;;;;;;CAiBd,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,CA8CA,CAAC"}
|
package/dist/esm/cli.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { parseArgs } from "node:util";
|
|
3
|
+
import { loadConfig, normalizeFakeServerConfig } from "./config.js";
|
|
3
4
|
import { createFakeServer } from "./index.js";
|
|
4
5
|
import { createLogger } from "./logger.js";
|
|
5
6
|
const HELP = `
|
|
@@ -7,44 +8,29 @@ Usage: npx @newmo/graphql-fake-server --schema <path> [options]
|
|
|
7
8
|
|
|
8
9
|
Options:
|
|
9
10
|
|
|
10
|
-
--schema <path>
|
|
11
|
-
--
|
|
11
|
+
--schema <path> Path to a schema file
|
|
12
|
+
--config <path> Path to a config file
|
|
12
13
|
--logLevel <logLevel> log level: debug, info, warn, error
|
|
13
14
|
|
|
15
|
+
Examples:
|
|
16
|
+
|
|
17
|
+
# Provide a schema file - use default config
|
|
18
|
+
npx @newmo/graphql-fake-server --schema api.graphql
|
|
19
|
+
# Use a config file
|
|
20
|
+
npx @newmo/graphql-fake-server --config graphql-fake-server.config.js
|
|
21
|
+
|
|
14
22
|
`;
|
|
15
23
|
// cli foo.graphql
|
|
16
24
|
export const cli = parseArgs({
|
|
17
25
|
args: process.argv.slice(2),
|
|
18
26
|
options: {
|
|
19
|
-
// --schema
|
|
20
27
|
schema: {
|
|
21
28
|
type: "string",
|
|
22
|
-
description: "Path to
|
|
23
|
-
},
|
|
24
|
-
mainPort: {
|
|
25
|
-
type: "string",
|
|
26
|
-
description: "Port to run the server on",
|
|
27
|
-
default: "4000",
|
|
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",
|
|
29
|
+
description: "Path to a schema file",
|
|
43
30
|
},
|
|
44
|
-
|
|
31
|
+
config: {
|
|
45
32
|
type: "string",
|
|
46
|
-
description: "
|
|
47
|
-
default: "4",
|
|
33
|
+
description: "Path to a config file",
|
|
48
34
|
},
|
|
49
35
|
logLevel: {
|
|
50
36
|
type: "string",
|
|
@@ -63,8 +49,8 @@ export const run = async ({ values, } = cli) => {
|
|
|
63
49
|
};
|
|
64
50
|
}
|
|
65
51
|
const logger = createLogger(logLevel);
|
|
66
|
-
const
|
|
67
|
-
if (!
|
|
52
|
+
const schemaFilePath = values.schema;
|
|
53
|
+
if (!schemaFilePath) {
|
|
68
54
|
logger.info(HELP);
|
|
69
55
|
return {
|
|
70
56
|
stdout: "",
|
|
@@ -72,61 +58,14 @@ export const run = async ({ values, } = cli) => {
|
|
|
72
58
|
exitCode: 1,
|
|
73
59
|
};
|
|
74
60
|
}
|
|
75
|
-
const
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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)) {
|
|
89
|
-
logger.info(HELP);
|
|
90
|
-
return {
|
|
91
|
-
stdout: "",
|
|
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",
|
|
115
|
-
exitCode: 1,
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
try {
|
|
119
|
-
const server = await createFakeServer({
|
|
120
|
-
schemaFilePath: schemaPath,
|
|
121
|
-
maxRegisteredSequences,
|
|
122
|
-
maxFieldRecursionDepth,
|
|
123
|
-
maxQueryDepth,
|
|
61
|
+
const config = values.config
|
|
62
|
+
? await loadConfig(values.config)
|
|
63
|
+
: normalizeFakeServerConfig({
|
|
64
|
+
schemaFilePath,
|
|
124
65
|
logLevel,
|
|
125
|
-
ports: {
|
|
126
|
-
fakeServer: mainPort,
|
|
127
|
-
apolloServer: apolloPort,
|
|
128
|
-
},
|
|
129
66
|
});
|
|
67
|
+
try {
|
|
68
|
+
const server = await createFakeServer(config);
|
|
130
69
|
const { urls } = await server.start();
|
|
131
70
|
logger.info(`🚀 GraphQL Fake Server listening at: ${urls.fakeServer}`);
|
|
132
71
|
return {
|
package/dist/esm/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","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RawConfig } from "@newmo/graphql-fake-core";
|
|
2
|
+
import type { LogLevel } from "./logger.js";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for the fake server.
|
|
5
|
+
*/
|
|
6
|
+
export type FakeServerConfig = {
|
|
7
|
+
/**
|
|
8
|
+
* The path to the GraphQL schema file.
|
|
9
|
+
*/
|
|
10
|
+
schemaFilePath: string;
|
|
11
|
+
ports?: {
|
|
12
|
+
/**
|
|
13
|
+
* Fake Server port.
|
|
14
|
+
* Default is 4000.
|
|
15
|
+
*/
|
|
16
|
+
fakeServer?: number | undefined;
|
|
17
|
+
/**
|
|
18
|
+
* Apollo Server port.
|
|
19
|
+
* It provides the GraphQL Playground.
|
|
20
|
+
* Default is 4002.
|
|
21
|
+
*/
|
|
22
|
+
apolloServer?: number | undefined;
|
|
23
|
+
} | undefined;
|
|
24
|
+
/**
|
|
25
|
+
* The maximum number of registered sequences.
|
|
26
|
+
* Default is 1000.
|
|
27
|
+
*/
|
|
28
|
+
maxRegisteredSequences?: number | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* The maximum number of depth of field recursion.
|
|
31
|
+
* Default is 3.
|
|
32
|
+
*/
|
|
33
|
+
maxFieldRecursionDepth?: RawConfig["maxFieldRecursionDepth"] | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* The maximum number of depth of complexity of query
|
|
36
|
+
* Default is 4
|
|
37
|
+
*/
|
|
38
|
+
maxQueryDepth?: number | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Default values for scalar types.
|
|
41
|
+
*/
|
|
42
|
+
defaultValues?: RawConfig["defaultValues"] | undefined;
|
|
43
|
+
/**
|
|
44
|
+
* Log level.
|
|
45
|
+
*/
|
|
46
|
+
logLevel?: LogLevel | undefined;
|
|
47
|
+
};
|
|
48
|
+
export type RequiredFakeServerConfig = {
|
|
49
|
+
schemaFilePath: string;
|
|
50
|
+
ports: {
|
|
51
|
+
fakeServer: number;
|
|
52
|
+
apolloServer: number;
|
|
53
|
+
};
|
|
54
|
+
maxRegisteredSequences: number;
|
|
55
|
+
maxFieldRecursionDepth: number;
|
|
56
|
+
maxQueryDepth: number;
|
|
57
|
+
defaultValues: RawConfig["defaultValues"];
|
|
58
|
+
logLevel: LogLevel;
|
|
59
|
+
};
|
|
60
|
+
export declare const normalizeFakeServerConfig: (config: FakeServerConfig) => RequiredFakeServerConfig;
|
|
61
|
+
export declare const validateFakeServerConfig: (config: FakeServerConfig) => void;
|
|
62
|
+
export declare const loadConfig: (configPath: string) => Promise<RequiredFakeServerConfig>;
|
|
63
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAC1D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG;IAC3B;;OAEG;IACH,cAAc,EAAE,MAAM,CAAC;IACvB,KAAK,CAAC,EACA;QACI;;;WAGG;QACH,UAAU,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;QAChC;;;;WAIG;QACH,YAAY,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;KACrC,GACD,SAAS,CAAC;IAChB;;;OAGG;IACH,sBAAsB,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC5C;;;OAGG;IACH,sBAAsB,CAAC,EAAE,SAAS,CAAC,wBAAwB,CAAC,GAAG,SAAS,CAAC;IACzE;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IACnC;;OAEG;IACH,aAAa,CAAC,EAAE,SAAS,CAAC,eAAe,CAAC,GAAG,SAAS,CAAC;IACvD;;OAEG;IACH,QAAQ,CAAC,EAAE,QAAQ,GAAG,SAAS,CAAC;CACnC,CAAC;AACF,MAAM,MAAM,wBAAwB,GAAG;IACnC,cAAc,EAAE,MAAM,CAAC;IACvB,KAAK,EAAE;QACH,UAAU,EAAE,MAAM,CAAC;QACnB,YAAY,EAAE,MAAM,CAAC;KACxB,CAAC;IACF,sBAAsB,EAAE,MAAM,CAAC;IAC/B,sBAAsB,EAAE,MAAM,CAAC;IAC/B,aAAa,EAAE,MAAM,CAAC;IACtB,aAAa,EAAE,SAAS,CAAC,eAAe,CAAC,CAAC;IAC1C,QAAQ,EAAE,QAAQ,CAAC;CACtB,CAAC;AACF,eAAO,MAAM,yBAAyB,WAAY,gBAAgB,KAAG,wBAapE,CAAC;AACF,eAAO,MAAM,wBAAwB,WAAY,gBAAgB,KAAG,IAoCnE,CAAC;AAEF,eAAO,MAAM,UAAU,eAAsB,MAAM,KAAG,QAAQ,wBAAwB,CAKrF,CAAC"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export const normalizeFakeServerConfig = (config) => {
|
|
2
|
+
return {
|
|
3
|
+
schemaFilePath: config.schemaFilePath,
|
|
4
|
+
ports: {
|
|
5
|
+
fakeServer: config.ports?.fakeServer ?? 4000,
|
|
6
|
+
apolloServer: config.ports?.apolloServer ?? 4002,
|
|
7
|
+
},
|
|
8
|
+
maxRegisteredSequences: config.maxRegisteredSequences ?? 1000,
|
|
9
|
+
maxFieldRecursionDepth: config.maxFieldRecursionDepth ?? 3,
|
|
10
|
+
maxQueryDepth: config.maxQueryDepth ?? 4,
|
|
11
|
+
defaultValues: config.defaultValues ?? {},
|
|
12
|
+
logLevel: config.logLevel ?? "info",
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export const validateFakeServerConfig = (config) => {
|
|
16
|
+
if (!config.schemaFilePath) {
|
|
17
|
+
throw new Error("The schemaFilePath is required.");
|
|
18
|
+
}
|
|
19
|
+
if (typeof config.schemaFilePath !== "string") {
|
|
20
|
+
throw new Error("The schemaPath must be a string.");
|
|
21
|
+
}
|
|
22
|
+
if (config.ports) {
|
|
23
|
+
if (typeof config.ports !== "object") {
|
|
24
|
+
throw new Error("The ports must be an object.");
|
|
25
|
+
}
|
|
26
|
+
if (config.ports.fakeServer && typeof config.ports.fakeServer !== "number") {
|
|
27
|
+
throw new Error("The fakeServer port must be a number.");
|
|
28
|
+
}
|
|
29
|
+
if (config.ports.apolloServer && typeof config.ports.apolloServer !== "number") {
|
|
30
|
+
throw new Error("The apolloServer port must be a number.");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
if (config.maxRegisteredSequences && typeof config.maxRegisteredSequences !== "number") {
|
|
34
|
+
throw new Error("The maxRegisteredSequences must be a number.");
|
|
35
|
+
}
|
|
36
|
+
if (config.maxFieldRecursionDepth && typeof config.maxFieldRecursionDepth !== "number") {
|
|
37
|
+
throw new Error("The maxFieldRecursionDepth must be a number.");
|
|
38
|
+
}
|
|
39
|
+
if (config.maxQueryDepth && typeof config.maxQueryDepth !== "number") {
|
|
40
|
+
throw new Error("The maxQueryDepth must be a number.");
|
|
41
|
+
}
|
|
42
|
+
if (config.defaultValues) {
|
|
43
|
+
if (typeof config.defaultValues !== "object") {
|
|
44
|
+
throw new Error("The defaultValues must be an object.");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
// ["debug", "info", "warn", "error"].includes(logLevel)
|
|
48
|
+
if (config.logLevel && !["debug", "info", "warn", "error"].includes(config.logLevel)) {
|
|
49
|
+
throw new Error("The logLevel must be one of 'debug', 'info', 'warn', 'error'.");
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
export const loadConfig = async (configPath) => {
|
|
53
|
+
const { default: config } = await import(configPath);
|
|
54
|
+
const normalizedConfig = normalizeFakeServerConfig(config);
|
|
55
|
+
validateFakeServerConfig(normalizedConfig);
|
|
56
|
+
return config;
|
|
57
|
+
};
|
|
58
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AA8DA,MAAM,CAAC,MAAM,y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AC3C,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { createFakeServer } from "./server.js";
|
|
2
2
|
export type { CreateFakeServerOptions } from "./server.js";
|
|
3
|
+
export type { FakeServerConfig, RequiredFakeServerConfig, } from "./config.js";
|
|
4
|
+
export { normalizeFakeServerConfig, validateFakeServerConfig } from "./config.js";
|
|
3
5
|
//# 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":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,YAAY,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC"}
|
|
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;AAC3D,YAAY,EACR,gBAAgB,EAChB,wBAAwB,GAC3B,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/esm/index.js
CHANGED
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,gBAAgB,EAAE,MAAM,aAAa,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAM/C,OAAO,EAAE,yBAAyB,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/esm/server.d.ts
CHANGED
|
@@ -1,28 +1,8 @@
|
|
|
1
1
|
import { type MockObject } from "@newmo/graphql-fake-core";
|
|
2
2
|
import type { GraphQLSchema } from "graphql/index.js";
|
|
3
|
+
import type { RequiredFakeServerConfig } from "./config.js";
|
|
3
4
|
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;
|
|
5
|
+
export type CreateFakeServerOptions = RequiredFakeServerConfig & {
|
|
26
6
|
logLevel?: LogLevel;
|
|
27
7
|
};
|
|
28
8
|
type FakeServerInternal = {
|
package/dist/esm/server.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,UAAU,EAAc,MAAM,0BAA0B,CAAC;AAGvE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAItD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,UAAU,EAAc,MAAM,0BAA0B,CAAC;AAG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AEF,eAAO,MAAM,wBAAwB,YAAmB,kBAAkB;;;;;;;;EA6BzE,CAAC"}
|
package/dist/esm/server.js
CHANGED
|
@@ -238,11 +238,13 @@ const createRoutingServer = async ({ logLevel, ports, maxRegisteredSequences, })
|
|
|
238
238
|
return app;
|
|
239
239
|
};
|
|
240
240
|
export const createFakeServer = async (options) => {
|
|
241
|
-
const
|
|
242
|
-
const
|
|
241
|
+
const { logLevel, maxFieldRecursionDepth, maxQueryDepth, maxRegisteredSequences, ports, schemaFilePath, defaultValues, } = options;
|
|
242
|
+
const logger = createLogger(logLevel);
|
|
243
|
+
const schema = buildSchema(await fs.readFile(schemaFilePath, "utf-8"));
|
|
243
244
|
const mockResult = await createMock({
|
|
244
245
|
schema,
|
|
245
|
-
maxFieldRecursionDepth
|
|
246
|
+
maxFieldRecursionDepth,
|
|
247
|
+
defaultValues,
|
|
246
248
|
});
|
|
247
249
|
if (!mockResult.ok) {
|
|
248
250
|
logger.error("Failed to create mock data", mockResult);
|
|
@@ -252,13 +254,6 @@ export const createFakeServer = async (options) => {
|
|
|
252
254
|
}
|
|
253
255
|
logger.debug("created mock code", mockResult.code);
|
|
254
256
|
logger.debug("created mock data", mockResult.mock);
|
|
255
|
-
const ports = {
|
|
256
|
-
fakeServer: options.ports?.fakeServer ?? 4000,
|
|
257
|
-
apolloServer: options.ports?.apolloServer ?? 4001,
|
|
258
|
-
};
|
|
259
|
-
const maxQueryDepth = options.maxQueryDepth ?? 3;
|
|
260
|
-
const maxFieldRecursionDepth = options.maxFieldRecursionDepth ?? maxQueryDepth + 1;
|
|
261
|
-
const maxRegisteredSequences = options.maxRegisteredSequences ?? 1000;
|
|
262
257
|
return createFakeServerInternal({
|
|
263
258
|
ports,
|
|
264
259
|
schema,
|
|
@@ -266,7 +261,7 @@ export const createFakeServer = async (options) => {
|
|
|
266
261
|
maxQueryDepth,
|
|
267
262
|
maxFieldRecursionDepth,
|
|
268
263
|
maxRegisteredSequences,
|
|
269
|
-
logLevel:
|
|
264
|
+
logLevel: logLevel ?? "info",
|
|
270
265
|
});
|
|
271
266
|
};
|
|
272
267
|
export const createFakeServerInternal = async (options) => {
|
package/dist/esm/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC1C,OAAO,EAAmB,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACvE,kBAAkB;AAClB,OAAO,UAAU,MAAM,qBAAqB,CAAC;AAE7C,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAgB,IAAI,EAAE,MAAM,MAAM,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../../src/server.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAC1C,OAAO,EAAmB,UAAU,EAAE,MAAM,0BAA0B,CAAC;AACvE,kBAAkB;AAClB,OAAO,UAAU,MAAM,qBAA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AAE,OAAO,CAAC,KAAK,CAAC,UAAU;aACjC,CAAC,CAAC;YACH,OAAO;gBACH,IAAI,EAAE;oBACF,UAAU,EAAE,oBAAoB,OAAO,CAAC,KAAK,CAAC,UAAU,EAAE;oBAC1D,YAAY,EAAE,oBAAoB,OAAO,CAAC,KAAK,CAAC,YAAY,EAAE;iBACjE;aACJ,CAAC;QACN,CAAC;QACD,IAAI,EAAE,GAAG,EAAE;YACP,YAAY,CAAC,IAAI,EAAE,CAAC;YACpB,YAAY,EAAE,KAAK,EAAE,CAAC;QAC1B,CAAC;KACJ,CAAC;AACN,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@newmo/graphql-fake-server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "GraphQL fake server for testing",
|
|
6
6
|
"keywords": [
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
"@graphql-tools/mock": "^9.0.2",
|
|
52
52
|
"@graphql-tools/schema": "^10.0.3",
|
|
53
53
|
"@hono/node-server": "^1.11.1",
|
|
54
|
-
"@newmo/graphql-fake-core": "0.
|
|
54
|
+
"@newmo/graphql-fake-core": "0.10.0",
|
|
55
55
|
"graphql-depth-limit": "^1.1.0",
|
|
56
56
|
"hono": "^4.3.2"
|
|
57
57
|
},
|
|
@@ -77,5 +77,5 @@
|
|
|
77
77
|
"access": "public",
|
|
78
78
|
"registry": "https://registry.npmjs.org/"
|
|
79
79
|
},
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "96211dee245e0aa305b35cb1d4312cb7484ef4e8"
|
|
81
81
|
}
|
package/src/cli.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { parseArgs } from "node:util";
|
|
3
|
+
import { loadConfig, normalizeFakeServerConfig } from "./config.js";
|
|
3
4
|
import { createFakeServer } from "./index.js";
|
|
4
5
|
import { type LogLevel, createLogger } from "./logger.js";
|
|
5
6
|
|
|
@@ -8,44 +9,29 @@ Usage: npx @newmo/graphql-fake-server --schema <path> [options]
|
|
|
8
9
|
|
|
9
10
|
Options:
|
|
10
11
|
|
|
11
|
-
--schema <path>
|
|
12
|
-
--
|
|
12
|
+
--schema <path> Path to a schema file
|
|
13
|
+
--config <path> Path to a config file
|
|
13
14
|
--logLevel <logLevel> log level: debug, info, warn, error
|
|
14
15
|
|
|
16
|
+
Examples:
|
|
17
|
+
|
|
18
|
+
# Provide a schema file - use default config
|
|
19
|
+
npx @newmo/graphql-fake-server --schema api.graphql
|
|
20
|
+
# Use a config file
|
|
21
|
+
npx @newmo/graphql-fake-server --config graphql-fake-server.config.js
|
|
22
|
+
|
|
15
23
|
`;
|
|
16
24
|
// cli foo.graphql
|
|
17
25
|
export const cli = parseArgs({
|
|
18
26
|
args: process.argv.slice(2),
|
|
19
27
|
options: {
|
|
20
|
-
// --schema
|
|
21
28
|
schema: {
|
|
22
29
|
type: "string",
|
|
23
|
-
description: "Path to
|
|
24
|
-
},
|
|
25
|
-
mainPort: {
|
|
26
|
-
type: "string",
|
|
27
|
-
description: "Port to run the server on",
|
|
28
|
-
default: "4000",
|
|
29
|
-
},
|
|
30
|
-
apolloPort: {
|
|
31
|
-
type: "string",
|
|
32
|
-
description: "Port to run the server on",
|
|
33
|
-
default: "4002",
|
|
34
|
-
},
|
|
35
|
-
maxRegisteredSequences: {
|
|
36
|
-
type: "string",
|
|
37
|
-
description: "Max number of registered sequences.",
|
|
38
|
-
default: "1000",
|
|
39
|
-
},
|
|
40
|
-
maxQueryDepth: {
|
|
41
|
-
type: "string",
|
|
42
|
-
description: "max query depth for complexity of query",
|
|
43
|
-
default: "3",
|
|
30
|
+
description: "Path to a schema file",
|
|
44
31
|
},
|
|
45
|
-
|
|
32
|
+
config: {
|
|
46
33
|
type: "string",
|
|
47
|
-
description: "
|
|
48
|
-
default: "4",
|
|
34
|
+
description: "Path to a config file",
|
|
49
35
|
},
|
|
50
36
|
logLevel: {
|
|
51
37
|
type: "string",
|
|
@@ -71,8 +57,8 @@ export const run = async ({
|
|
|
71
57
|
};
|
|
72
58
|
}
|
|
73
59
|
const logger = createLogger(logLevel);
|
|
74
|
-
const
|
|
75
|
-
if (!
|
|
60
|
+
const schemaFilePath = values.schema;
|
|
61
|
+
if (!schemaFilePath) {
|
|
76
62
|
logger.info(HELP);
|
|
77
63
|
return {
|
|
78
64
|
stdout: "",
|
|
@@ -80,61 +66,15 @@ export const run = async ({
|
|
|
80
66
|
exitCode: 1,
|
|
81
67
|
};
|
|
82
68
|
}
|
|
83
|
-
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
exitCode: 1,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
const maxFieldRecursionDepth = values.maxFieldRecursionDepth
|
|
94
|
-
? Number.parseInt(values.maxFieldRecursionDepth, 10)
|
|
95
|
-
: Number.NaN;
|
|
96
|
-
if (Number.isNaN(maxFieldRecursionDepth)) {
|
|
97
|
-
logger.info(HELP);
|
|
98
|
-
return {
|
|
99
|
-
stdout: "",
|
|
100
|
-
stderr: "maxFieldRecursionDepth must be a number",
|
|
101
|
-
exitCode: 1,
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
const maxQueryDepth = values.maxQueryDepth
|
|
105
|
-
? Number.parseInt(values.maxQueryDepth, 10)
|
|
106
|
-
: Number.NaN;
|
|
107
|
-
if (Number.isNaN(maxQueryDepth)) {
|
|
108
|
-
logger.info(HELP);
|
|
109
|
-
return {
|
|
110
|
-
stdout: "",
|
|
111
|
-
stderr: "maxQueryDepth must be a number",
|
|
112
|
-
exitCode: 1,
|
|
113
|
-
};
|
|
114
|
-
}
|
|
115
|
-
const maxRegisteredSequences = values.maxRegisteredSequences
|
|
116
|
-
? Number.parseInt(values.maxRegisteredSequences, 10)
|
|
117
|
-
: Number.NaN;
|
|
118
|
-
if (Number.isNaN(maxRegisteredSequences)) {
|
|
119
|
-
logger.info(HELP);
|
|
120
|
-
return {
|
|
121
|
-
stdout: "",
|
|
122
|
-
stderr: "maxRegisteredSequences must be a number",
|
|
123
|
-
exitCode: 1,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
69
|
+
|
|
70
|
+
const config = values.config
|
|
71
|
+
? await loadConfig(values.config)
|
|
72
|
+
: normalizeFakeServerConfig({
|
|
73
|
+
schemaFilePath,
|
|
74
|
+
logLevel,
|
|
75
|
+
});
|
|
126
76
|
try {
|
|
127
|
-
const server = await createFakeServer(
|
|
128
|
-
schemaFilePath: schemaPath,
|
|
129
|
-
maxRegisteredSequences,
|
|
130
|
-
maxFieldRecursionDepth,
|
|
131
|
-
maxQueryDepth,
|
|
132
|
-
logLevel,
|
|
133
|
-
ports: {
|
|
134
|
-
fakeServer: mainPort,
|
|
135
|
-
apolloServer: apolloPort,
|
|
136
|
-
},
|
|
137
|
-
});
|
|
77
|
+
const server = await createFakeServer(config);
|
|
138
78
|
const { urls } = await server.start();
|
|
139
79
|
logger.info(`🚀 GraphQL Fake Server listening at: ${urls.fakeServer}`);
|
|
140
80
|
return {
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { RawConfig } from "@newmo/graphql-fake-core";
|
|
2
|
+
import type { LogLevel } from "./logger.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Configuration for the fake server.
|
|
6
|
+
*/
|
|
7
|
+
export type FakeServerConfig = {
|
|
8
|
+
/**
|
|
9
|
+
* The path to the GraphQL schema file.
|
|
10
|
+
*/
|
|
11
|
+
schemaFilePath: string;
|
|
12
|
+
ports?:
|
|
13
|
+
| {
|
|
14
|
+
/**
|
|
15
|
+
* Fake Server port.
|
|
16
|
+
* Default is 4000.
|
|
17
|
+
*/
|
|
18
|
+
fakeServer?: number | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Apollo Server port.
|
|
21
|
+
* It provides the GraphQL Playground.
|
|
22
|
+
* Default is 4002.
|
|
23
|
+
*/
|
|
24
|
+
apolloServer?: number | undefined;
|
|
25
|
+
}
|
|
26
|
+
| undefined;
|
|
27
|
+
/**
|
|
28
|
+
* The maximum number of registered sequences.
|
|
29
|
+
* Default is 1000.
|
|
30
|
+
*/
|
|
31
|
+
maxRegisteredSequences?: number | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* The maximum number of depth of field recursion.
|
|
34
|
+
* Default is 3.
|
|
35
|
+
*/
|
|
36
|
+
maxFieldRecursionDepth?: RawConfig["maxFieldRecursionDepth"] | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* The maximum number of depth of complexity of query
|
|
39
|
+
* Default is 4
|
|
40
|
+
*/
|
|
41
|
+
maxQueryDepth?: number | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Default values for scalar types.
|
|
44
|
+
*/
|
|
45
|
+
defaultValues?: RawConfig["defaultValues"] | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Log level.
|
|
48
|
+
*/
|
|
49
|
+
logLevel?: LogLevel | undefined;
|
|
50
|
+
};
|
|
51
|
+
export type RequiredFakeServerConfig = {
|
|
52
|
+
schemaFilePath: string;
|
|
53
|
+
ports: {
|
|
54
|
+
fakeServer: number;
|
|
55
|
+
apolloServer: number;
|
|
56
|
+
};
|
|
57
|
+
maxRegisteredSequences: number;
|
|
58
|
+
maxFieldRecursionDepth: number;
|
|
59
|
+
maxQueryDepth: number;
|
|
60
|
+
defaultValues: RawConfig["defaultValues"];
|
|
61
|
+
logLevel: LogLevel;
|
|
62
|
+
};
|
|
63
|
+
export const normalizeFakeServerConfig = (config: FakeServerConfig): RequiredFakeServerConfig => {
|
|
64
|
+
return {
|
|
65
|
+
schemaFilePath: config.schemaFilePath,
|
|
66
|
+
ports: {
|
|
67
|
+
fakeServer: config.ports?.fakeServer ?? 4000,
|
|
68
|
+
apolloServer: config.ports?.apolloServer ?? 4002,
|
|
69
|
+
},
|
|
70
|
+
maxRegisteredSequences: config.maxRegisteredSequences ?? 1000,
|
|
71
|
+
maxFieldRecursionDepth: config.maxFieldRecursionDepth ?? 3,
|
|
72
|
+
maxQueryDepth: config.maxQueryDepth ?? 4,
|
|
73
|
+
defaultValues: config.defaultValues ?? {},
|
|
74
|
+
logLevel: config.logLevel ?? "info",
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
export const validateFakeServerConfig = (config: FakeServerConfig): void => {
|
|
78
|
+
if (!config.schemaFilePath) {
|
|
79
|
+
throw new Error("The schemaFilePath is required.");
|
|
80
|
+
}
|
|
81
|
+
if (typeof config.schemaFilePath !== "string") {
|
|
82
|
+
throw new Error("The schemaPath must be a string.");
|
|
83
|
+
}
|
|
84
|
+
if (config.ports) {
|
|
85
|
+
if (typeof config.ports !== "object") {
|
|
86
|
+
throw new Error("The ports must be an object.");
|
|
87
|
+
}
|
|
88
|
+
if (config.ports.fakeServer && typeof config.ports.fakeServer !== "number") {
|
|
89
|
+
throw new Error("The fakeServer port must be a number.");
|
|
90
|
+
}
|
|
91
|
+
if (config.ports.apolloServer && typeof config.ports.apolloServer !== "number") {
|
|
92
|
+
throw new Error("The apolloServer port must be a number.");
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (config.maxRegisteredSequences && typeof config.maxRegisteredSequences !== "number") {
|
|
96
|
+
throw new Error("The maxRegisteredSequences must be a number.");
|
|
97
|
+
}
|
|
98
|
+
if (config.maxFieldRecursionDepth && typeof config.maxFieldRecursionDepth !== "number") {
|
|
99
|
+
throw new Error("The maxFieldRecursionDepth must be a number.");
|
|
100
|
+
}
|
|
101
|
+
if (config.maxQueryDepth && typeof config.maxQueryDepth !== "number") {
|
|
102
|
+
throw new Error("The maxQueryDepth must be a number.");
|
|
103
|
+
}
|
|
104
|
+
if (config.defaultValues) {
|
|
105
|
+
if (typeof config.defaultValues !== "object") {
|
|
106
|
+
throw new Error("The defaultValues must be an object.");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// ["debug", "info", "warn", "error"].includes(logLevel)
|
|
110
|
+
if (config.logLevel && !["debug", "info", "warn", "error"].includes(config.logLevel)) {
|
|
111
|
+
throw new Error("The logLevel must be one of 'debug', 'info', 'warn', 'error'.");
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export const loadConfig = async (configPath: string): Promise<RequiredFakeServerConfig> => {
|
|
116
|
+
const { default: config } = await import(configPath);
|
|
117
|
+
const normalizedConfig = normalizeFakeServerConfig(config);
|
|
118
|
+
validateFakeServerConfig(normalizedConfig);
|
|
119
|
+
return config;
|
|
120
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,7 @@
|
|
|
1
1
|
export { createFakeServer } from "./server.js";
|
|
2
2
|
export type { CreateFakeServerOptions } from "./server.js";
|
|
3
|
+
export type {
|
|
4
|
+
FakeServerConfig,
|
|
5
|
+
RequiredFakeServerConfig,
|
|
6
|
+
} from "./config.js";
|
|
7
|
+
export { normalizeFakeServerConfig, validateFakeServerConfig } from "./config.js";
|
package/src/server.ts
CHANGED
|
@@ -11,30 +11,10 @@ import type { GraphQLSchema } from "graphql/index.js";
|
|
|
11
11
|
import { buildSchema } from "graphql/utilities/index.js";
|
|
12
12
|
import { type Context, Hono } from "hono";
|
|
13
13
|
import { cors } from "hono/cors";
|
|
14
|
+
import type { RequiredFakeServerConfig } from "./config.js";
|
|
14
15
|
import { type LogLevel, createLogger } from "./logger.js";
|
|
15
16
|
|
|
16
|
-
export type CreateFakeServerOptions = {
|
|
17
|
-
schemaFilePath: string;
|
|
18
|
-
ports?: {
|
|
19
|
-
fakeServer: number;
|
|
20
|
-
apolloServer: number;
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* max query depth for complexity of query
|
|
24
|
-
* Default is 3
|
|
25
|
-
*/
|
|
26
|
-
maxQueryDepth?: number;
|
|
27
|
-
/**
|
|
28
|
-
* maxFieldRecursionDepth for creating fake data
|
|
29
|
-
* Default is maxDepth + 1
|
|
30
|
-
*/
|
|
31
|
-
maxFieldRecursionDepth?: number;
|
|
32
|
-
/**
|
|
33
|
-
* max number of registered sequences
|
|
34
|
-
* Default is 1000
|
|
35
|
-
* If the number of registered sequences exceeds this number, the oldest sequence is deleted.
|
|
36
|
-
*/
|
|
37
|
-
maxRegisteredSequences?: number;
|
|
17
|
+
export type CreateFakeServerOptions = RequiredFakeServerConfig & {
|
|
38
18
|
logLevel?: LogLevel;
|
|
39
19
|
};
|
|
40
20
|
|
|
@@ -339,11 +319,21 @@ const createRoutingServer = async ({
|
|
|
339
319
|
return app;
|
|
340
320
|
};
|
|
341
321
|
export const createFakeServer = async (options: CreateFakeServerOptions) => {
|
|
342
|
-
const
|
|
343
|
-
|
|
322
|
+
const {
|
|
323
|
+
logLevel,
|
|
324
|
+
maxFieldRecursionDepth,
|
|
325
|
+
maxQueryDepth,
|
|
326
|
+
maxRegisteredSequences,
|
|
327
|
+
ports,
|
|
328
|
+
schemaFilePath,
|
|
329
|
+
defaultValues,
|
|
330
|
+
} = options;
|
|
331
|
+
const logger = createLogger(logLevel);
|
|
332
|
+
const schema = buildSchema(await fs.readFile(schemaFilePath, "utf-8"));
|
|
344
333
|
const mockResult = await createMock({
|
|
345
334
|
schema,
|
|
346
|
-
maxFieldRecursionDepth
|
|
335
|
+
maxFieldRecursionDepth,
|
|
336
|
+
defaultValues,
|
|
347
337
|
});
|
|
348
338
|
if (!mockResult.ok) {
|
|
349
339
|
logger.error("Failed to create mock data", mockResult);
|
|
@@ -353,13 +343,6 @@ export const createFakeServer = async (options: CreateFakeServerOptions) => {
|
|
|
353
343
|
}
|
|
354
344
|
logger.debug("created mock code", mockResult.code);
|
|
355
345
|
logger.debug("created mock data", mockResult.mock);
|
|
356
|
-
const ports = {
|
|
357
|
-
fakeServer: options.ports?.fakeServer ?? 4000,
|
|
358
|
-
apolloServer: options.ports?.apolloServer ?? 4001,
|
|
359
|
-
};
|
|
360
|
-
const maxQueryDepth = options.maxQueryDepth ?? 3;
|
|
361
|
-
const maxFieldRecursionDepth = options.maxFieldRecursionDepth ?? maxQueryDepth + 1;
|
|
362
|
-
const maxRegisteredSequences = options.maxRegisteredSequences ?? 1000;
|
|
363
346
|
return createFakeServerInternal({
|
|
364
347
|
ports,
|
|
365
348
|
schema,
|
|
@@ -367,7 +350,7 @@ export const createFakeServer = async (options: CreateFakeServerOptions) => {
|
|
|
367
350
|
maxQueryDepth,
|
|
368
351
|
maxFieldRecursionDepth,
|
|
369
352
|
maxRegisteredSequences,
|
|
370
|
-
logLevel:
|
|
353
|
+
logLevel: logLevel ?? "info",
|
|
371
354
|
});
|
|
372
355
|
};
|
|
373
356
|
|