@newmo/graphql-fake-server 0.2.0 → 0.3.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/src/cli.ts CHANGED
@@ -1,101 +1,100 @@
1
1
  #!/usr/bin/env node
2
- import fs from "node:fs/promises";
2
+ import * as fs from "node:fs/promises";
3
3
  import { parseArgs } from "node:util";
4
- import vm from "node:vm";
5
- import { ApolloServer } from '@apollo/server';
6
- import { startStandaloneServer } from '@apollo/server/standalone';
7
- import { addMocksToSchema } from '@graphql-tools/mock';
8
- import { makeExecutableSchema } from '@graphql-tools/schema';
9
- import { buildSchema } from "graphql";
10
- import { GraphQLSchema } from "graphql/index.js";
11
- import { generateCode } from "./code-generator.js";
12
- import { normalizeConfig } from "./config.js";
13
- import { getTypeInfos } from "./schema-scanner.js";
4
+ import { buildSchema } from "graphql/utilities/index.js";
5
+ import { createMock, startFakeServer } from "./index.js";
6
+ import { type LogLevel, createLogger } from "./logger.js";
14
7
 
15
8
  const HELP = `
16
- Usage: cli <file.graphql>
9
+ Usage: npx @newmo/graphql-fake-server --schema <path> [options]
10
+
11
+ Options:
12
+
13
+ --schema <path> Path to the schema file. e.g. schema.graphql
14
+ --port <port> Port to run the server on
15
+ --logLevel <logLevel> log level: debug, info, warn, error
16
+
17
17
  `;
18
18
  // cli foo.graphql
19
- const { positionals, values } = parseArgs({
20
- args: process.argv.slice(2), allowPositionals: true,
19
+ export const cli = parseArgs({
20
+ args: process.argv.slice(2),
21
21
  options: {
22
+ // --schema
23
+ schema: {
24
+ type: "string",
25
+ description: "Path to the schema file. e.g. schema.graphql",
26
+ },
22
27
  // --port
23
28
  port: {
24
29
  type: "string",
25
30
  description: "Port to run the server on",
26
31
  default: "4000",
27
32
  },
28
- verbose: {
29
- type: "boolean",
30
- description: "Verbose output",
31
- default: false
32
- }
33
- }
33
+ logLevel: {
34
+ type: "string",
35
+ description: "log level: debug, info, warn, error",
36
+ default: "info",
37
+ },
38
+ },
34
39
  });
35
- if (!positionals.length) {
36
- console.info(HELP);
37
- process.exit(1);
38
- }
39
- const [filePath] = positionals;
40
- if (!filePath) {
41
- console.error(HELP);
42
- process.exit(1);
43
- }
44
- const port = values.port ? Number.parseInt(values.port, 10) : NaN;
45
- if (Number.isNaN(port)) {
46
- console.error("--port must be a number");
47
- process.exit(1);
48
- }
49
- const startFakeServer = async ({
50
- schema,
51
- mockObject
52
- }: {
53
- schema: GraphQLSchema;
54
- mockObject: Record<string, {}>
55
- }) => {
56
-
57
- const mocks = Object.fromEntries(Object.entries(mockObject).map(([key, value]) => {
58
- return [key, () => value];
59
- })
60
- )
61
-
62
- const server = new ApolloServer({
63
- schema: addMocksToSchema({
64
- schema: makeExecutableSchema({
65
- typeDefs: schema
66
- }),
67
- mocks,
68
- }),
69
- });
70
-
71
- const { url } = await startStandaloneServer(server, { listen: { port: port } });
72
-
73
- console.log(`🚀 Server listening at: ${url}`);
74
- }
75
- try {
76
- const schema = buildSchema(await fs.readFile(filePath, "utf-8"));
77
- const normalizedConfig = normalizeConfig({
78
- typesFile: "types.ts",
79
- });
80
- const typeInfos = getTypeInfos(normalizedConfig, schema);
81
- const code = generateCode(normalizedConfig, typeInfos);
82
- if (values.verbose) {
83
- console.info("Generated code:");
84
- console.info(code);
40
+ export const run = async ({
41
+ values,
42
+ }: typeof cli = cli): Promise<
43
+ | {
44
+ stdout: string;
45
+ stderr: string | Error;
46
+ exitCode: number;
47
+ }
48
+ | (() => void)
49
+ > => {
50
+ const logLevel = values.logLevel as LogLevel | undefined;
51
+ if (!logLevel || !["debug", "info", "warn", "error"].includes(logLevel)) {
52
+ return {
53
+ stdout: "",
54
+ stderr: "--logLevel must be one of debug, info, warn, error",
55
+ exitCode: 1,
56
+ };
57
+ }
58
+ const logger = createLogger(logLevel);
59
+ const schemaPath = values.schema;
60
+ if (!schemaPath) {
61
+ logger.info(HELP);
62
+ return {
63
+ stdout: "",
64
+ stderr: "--schema is required",
65
+ exitCode: 1,
66
+ };
85
67
  }
86
- // execute code in vm and get all exports
87
- const exports = {};
88
- vm.runInNewContext(code, { exports });
89
- if (values.verbose) {
90
- console.info("Exports:");
91
- console.info(exports);
68
+ const port = values.port ? Number.parseInt(values.port, 10) : Number.NaN;
69
+ if (Number.isNaN(port)) {
70
+ logger.info(HELP);
71
+ return {
72
+ stdout: "",
73
+ stderr: "--port must be a number",
74
+ exitCode: 1,
75
+ };
76
+ }
77
+ try {
78
+ const schema = buildSchema(await fs.readFile(schemaPath, "utf-8"));
79
+ const mockObject = await createMock({
80
+ schema,
81
+ logLevel: logLevel,
82
+ });
83
+ const closeServer = await startFakeServer({
84
+ mockObject,
85
+ port,
86
+ schema,
87
+ });
88
+ // TODO: more readable output?
89
+ return closeServer;
90
+ } catch (error) {
91
+ logger.error(error);
92
+ return {
93
+ stdout: "",
94
+ stderr: new Error("Failed to start server", {
95
+ cause: error,
96
+ }),
97
+ exitCode: 1,
98
+ };
92
99
  }
93
- await startFakeServer({
94
- schema,
95
- mockObject: exports
96
- });
97
- // write to file
98
- } catch (error) {
99
- console.error(error);
100
- process.exit(1);
101
- }
100
+ };
package/src/index.ts CHANGED
@@ -1,15 +1,87 @@
1
- // MEMO: The tests for this module are covered by `e2e/*.e2e.ts`.
1
+ import vm from "node:vm";
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
+ import type { GraphQLSchema } from "graphql";
8
+ //@ts-expect-error
9
+ import depthLimit from "graphql-depth-limit";
10
+ import { type LogLevel, createLogger } from "./logger.js";
2
11
 
3
- import { type PluginFunction } from '@graphql-codegen/plugin-helpers';
4
- import { generateCode } from './code-generator.js';
5
- import { normalizeConfig, validateConfig } from './config.js';
6
- import { getTypeInfos } from './schema-scanner.js';
7
-
8
- export const plugin: PluginFunction = (schema, _documents, config, _info) => {
9
- validateConfig(config);
10
-
11
- const normalizedConfig = normalizeConfig(config);
12
- const typeInfos = getTypeInfos(normalizedConfig, schema);
13
- const code = generateCode(normalizedConfig, typeInfos);
14
- return code;
12
+ export type MockObject = Record<string, unknown>;
13
+ export type StartFakeServerOptions = {
14
+ schema: GraphQLSchema;
15
+ mockObject: MockObject;
16
+ port?: number;
17
+ logLevel?: LogLevel;
18
+ };
19
+ export const startFakeServer = async ({
20
+ schema,
21
+ mockObject,
22
+ port,
23
+ logLevel,
24
+ }: StartFakeServerOptions) => {
25
+ const logger = createLogger(logLevel);
26
+ const mocks = Object.fromEntries(
27
+ Object.entries(mockObject).map(([key, value]) => {
28
+ return [key, () => value];
29
+ }),
30
+ );
31
+ const server = new ApolloServer({
32
+ schema: addMocksToSchema({
33
+ schema: makeExecutableSchema({
34
+ typeDefs: schema,
35
+ }),
36
+ mocks,
37
+ }),
38
+ validationRules: [depthLimit(3)],
39
+ });
40
+ const { url } = await startStandaloneServer(server, { listen: { port: port } });
41
+ logger.info(`🚀 Server listening at: ${url}`);
42
+ return () => {
43
+ // close
44
+ server.stop();
45
+ };
46
+ };
47
+ export type GenerateMockOptions = {
48
+ schema: GraphQLSchema;
49
+ logLevel?: LogLevel;
50
+ };
51
+ const cloneAsJSON = (obj: unknown) => {
52
+ return JSON.parse(JSON.stringify(obj));
53
+ };
54
+ /**
55
+ * Create mock object from schema
56
+ * It supports @example directive
57
+ * @param options
58
+ */
59
+ export const createMock = async (options: GenerateMockOptions): Promise<MockObject> => {
60
+ const logger = createLogger(options.logLevel);
61
+ try {
62
+ const normalizedConfig = normalizeConfig({
63
+ maxFieldRecursionDepth: 4,
64
+ });
65
+ const typeInfos = getTypeInfos(normalizedConfig, options.schema);
66
+ const code = generateCode(
67
+ {
68
+ ...normalizedConfig,
69
+ outputType: "commonjs",
70
+ },
71
+ typeInfos,
72
+ );
73
+ logger.debug("Generated code:");
74
+ logger.debug(code);
75
+ // execute code in vm and get all exports
76
+ const exports = {};
77
+ vm.runInNewContext(code, { exports });
78
+ // Apollo Server does not support Function type in mock object
79
+ const plainObject = cloneAsJSON(exports);
80
+ logger.debug("Exports:");
81
+ logger.debug(JSON.stringify(plainObject, null, 2));
82
+ return plainObject;
83
+ } catch (error) {
84
+ logger.error(error);
85
+ process.exit(1);
86
+ }
15
87
  };
package/src/logger.ts ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * if logLevel is debug, log all logs
3
+ * if logLevel is info, log info, warn, error
4
+ * if logLevel is warn, log warn, error
5
+ * if logLevel is error, log error
6
+ */
7
+ export type LogLevel = "debug" | "info" | "warn" | "error";
8
+
9
+ export const createLogger = (logLevel: LogLevel = "info") => {
10
+ return {
11
+ debug: (...args: unknown[]) => {
12
+ if (logLevel === "debug") {
13
+ console.debug(...args);
14
+ }
15
+ },
16
+ info: (...args: unknown[]) => {
17
+ if (logLevel === "debug" || logLevel === "info") {
18
+ console.info(...args);
19
+ }
20
+ },
21
+ warn: (...args: unknown[]) => {
22
+ if (logLevel === "debug" || logLevel === "info" || logLevel === "warn") {
23
+ console.warn(...args);
24
+ }
25
+ },
26
+ error: (...args: unknown[]) => {
27
+ console.error(...args);
28
+ },
29
+ };
30
+ };
@@ -1,4 +0,0 @@
1
- import { Config } from './config.js';
2
- import { TypeInfo } from './schema-scanner.js';
3
- export declare function generateCode(config: Config, typeInfos: TypeInfo[]): string;
4
- //# sourceMappingURL=code-generator.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"code-generator.d.ts","sourceRoot":"","sources":["../../src/code-generator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAoC,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAoEjF,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,QAAQ,EAAE,GAAG,MAAM,CAY1E"}
@@ -1,75 +0,0 @@
1
- function generatePreludeCode(config, typeInfos) {
2
- const joinedTypeNames = typeInfos
3
- .filter(({ type }) => type === 'object')
4
- .map(({ name }) => ` ${name}`)
5
- .join(',\n');
6
- const code = `
7
- import type {
8
- ${joinedTypeNames},
9
- } from '${config.typesFile}';
10
- `.trim();
11
- return `${code}\n`;
12
- }
13
- const handleExample = (exampleDirective) => {
14
- if ("value" in exampleDirective) {
15
- return JSON.stringify(exampleDirective.value);
16
- }
17
- else if ("expression" in exampleDirective) {
18
- return exampleDirective.expression;
19
- }
20
- throw new Error(`Invalid example directive${JSON.stringify(exampleDirective)}`);
21
- };
22
- function generateApolloFakeServer(config, typeInfo) {
23
- const header = `import { ApolloServer } from '@apollo/server';
24
- import { startStandaloneServer } from '@apollo/server/standalone';
25
- import { addMocksToSchema } from '@graphql-tools/mock';
26
- import { makeExecutableSchema } from '@graphql-tools/schema';
27
- `;
28
- const body = `
29
- const server = new ApolloServer({
30
- schema: addMocksToSchema({
31
- schema: makeExecutableSchema({ typeDefs }),
32
- mocks,
33
- }),
34
- });
35
- const { url } = await startStandaloneServer(server, { listen: { port: 4000 } });
36
- console.log(\`🚀 Server listening at: \${url}\`);
37
- `;
38
- return {
39
- header,
40
- body
41
- };
42
- }
43
- function generateExampleCode(config, typeInfo) {
44
- const { name } = typeInfo;
45
- const indent = ' ';
46
- return `
47
- /**
48
- * Default ${name} model using @example directive.
49
- */
50
- const ${name} = {
51
- ${typeInfo.fields.flatMap((field) => {
52
- const example = field.example;
53
- if (example) {
54
- return [`${indent}${field.name}: ${handleExample(example)}`];
55
- }
56
- return [];
57
- }).join(',\n')}
58
- };
59
- exports.${name} = ${name};
60
- `.trimStart();
61
- }
62
- export function generateCode(config, typeInfos) {
63
- let code = '';
64
- // code += generatePreludeCode(config, typeInfos);
65
- // code += apolloFakeServer.header;
66
- // code += '\n';
67
- for (const typeInfo of typeInfos) {
68
- if (typeInfo.type === 'object') {
69
- code += generateExampleCode(config, typeInfo);
70
- code += '\n';
71
- }
72
- }
73
- return code;
74
- }
75
- //# sourceMappingURL=code-generator.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"code-generator.js","sourceRoot":"","sources":["../../src/code-generator.ts"],"names":[],"mappings":"AAIA,SAAS,mBAAmB,CAAC,MAAc,EAAE,SAAqB;IAC9D,MAAM,eAAe,GAAG,SAAS;SAC5B,MAAM,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,KAAK,QAAQ,CAAC;SACvC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,KAAK,IAAI,EAAE,CAAC;SAC9B,IAAI,CAAC,KAAK,CAAC,CAAC;IACjB,MAAM,IAAI,GAAG;;EAEf,eAAe;UACP,MAAM,CAAC,SAAS;CACzB,CAAC,IAAI,EAAE,CAAC;IACL,OAAO,GAAG,IAAI,IAAI,CAAC;AACvB,CAAC;AAED,MAAM,aAAa,GAAG,CAAC,gBAAkC,EAAU,EAAE;IACjE,IAAI,OAAO,IAAI,gBAAgB,EAAE,CAAC;QAC9B,OAAO,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;IAClD,CAAC;SAAM,IAAI,YAAY,IAAI,gBAAgB,EAAE,CAAC;QAC1C,OAAO,gBAAgB,CAAC,UAAU,CAAC;IACvC,CAAC;IACD,MAAM,IAAI,KAAK,CAAC,4BAA4B,IAAI,CAAC,SAAS,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC;AACpF,CAAC,CAAA;AAED,SAAS,wBAAwB,CAAC,MAAc,EAAE,QAAwB;IACtE,MAAM,MAAM,GAAG;;;;CAIlB,CAAC;IAEE,MAAM,IAAI,GAAG;;;;;;;;;CAShB,CAAC;IACE,OAAO;QACH,MAAM;QACN,IAAI;KACP,CAAA;AACL,CAAC;AAED,SAAS,mBAAmB,CAAC,MAAc,EAAE,QAAwB;IACjE,MAAM,EAAE,IAAI,EAAE,GAAG,QAAQ,CAAC;IAC1B,MAAM,MAAM,GAAG,IAAI,CAAC;IACpB,OAAO;;aAEE,IAAI;;QAET,IAAI;EACV,QAAQ,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QAC5B,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;QAC9B,IAAI,OAAO,EAAE,CAAC;YACV,OAAO,CAAC,GAAG,MAAM,GAAG,KAAK,CAAC,IAAI,KAAK,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACjE,CAAC;QACD,OAAO,EAAE,CAAC;IACd,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;;UAER,IAAI,MAAM,IAAI;CACvB,CAAC,SAAS,EAAE,CAAC;AACd,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,MAAc,EAAE,SAAqB;IAC9D,IAAI,IAAI,GAAG,EAAE,CAAC;IACd,kDAAkD;IAClD,mCAAmC;IACnC,gBAAgB;IAChB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QAC/B,IAAI,QAAQ,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;YAC7B,IAAI,IAAI,mBAAmB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;YAC9C,IAAI,IAAI,IAAI,CAAC;QACjB,CAAC;IACL,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC"}
@@ -1,46 +0,0 @@
1
- import { ConvertFn, RawTypesConfig } from '@graphql-codegen/visitor-plugin-common';
2
- export type RawConfig = {
3
- typesFile: string;
4
- skipTypename?: RawTypesConfig['skipTypename'];
5
- skipIsAbstractType?: boolean | undefined;
6
- nonOptionalDefaultFields?: boolean | undefined;
7
- namingConvention?: RawTypesConfig['namingConvention'];
8
- typesPrefix?: RawTypesConfig['typesPrefix'];
9
- typesSuffix?: RawTypesConfig['typesSuffix'];
10
- defaultValues?: {
11
- String?: string;
12
- Int?: number;
13
- Float?: number;
14
- Boolean?: boolean;
15
- ID?: string;
16
- listLength?: number;
17
- };
18
- };
19
- export declare const DefaultValues: {
20
- String: string;
21
- Int: number;
22
- Float: number;
23
- Boolean: boolean;
24
- ID: string;
25
- listLength: number;
26
- };
27
- export type Config = {
28
- typesFile: string;
29
- skipTypename: Exclude<RawTypesConfig['skipTypename'], undefined>;
30
- skipIsAbstractType: boolean;
31
- nonOptionalDefaultFields: boolean;
32
- typesPrefix: Exclude<RawTypesConfig['typesPrefix'], undefined>;
33
- typesSuffix: Exclude<RawTypesConfig['typesSuffix'], undefined>;
34
- convert: ConvertFn;
35
- defaultValues: {
36
- String: string;
37
- Int: number;
38
- Float: number;
39
- Boolean: boolean;
40
- ID: string;
41
- listLength: number;
42
- };
43
- };
44
- export declare function validateConfig(rawConfig: unknown): asserts rawConfig is RawConfig;
45
- export declare function normalizeConfig(rawConfig: RawConfig): Config;
46
- //# sourceMappingURL=config.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,cAAc,EAAkB,MAAM,wCAAwC,CAAC;AAEnG,MAAM,MAAM,SAAS,GAAG;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,CAAC,EAAE,cAAc,CAAC,cAAc,CAAC,CAAC;IAC9C,kBAAkB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IACzC,wBAAwB,CAAC,EAAE,OAAO,GAAG,SAAS,CAAC;IAC/C,gBAAgB,CAAC,EAAE,cAAc,CAAC,kBAAkB,CAAC,CAAC;IACtD,WAAW,CAAC,EAAE,cAAc,CAAC,aAAa,CAAC,CAAC;IAC5C,WAAW,CAAC,EAAE,cAAc,CAAC,aAAa,CAAC,CAAC;IAE5C,aAAa,CAAC,EAAE;QACZ,MAAM,CAAC,EAAE,MAAM,CAAA;QACf,GAAG,CAAC,EAAE,MAAM,CAAA;QACZ,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,OAAO,CAAC,EAAE,OAAO,CAAA;QACjB,EAAE,CAAC,EAAE,MAAM,CAAA;QACX,UAAU,CAAC,EAAE,MAAM,CAAA;KACtB,CAAA;CACJ,CAAC;AAEF,eAAO,MAAM,aAAa;;;;;;;CAOzB,CAAA;AACD,MAAM,MAAM,MAAM,GAAG;IACjB,SAAS,EAAE,MAAM,CAAC;IAClB,YAAY,EAAE,OAAO,CAAC,cAAc,CAAC,cAAc,CAAC,EAAE,SAAS,CAAC,CAAC;IACjE,kBAAkB,EAAE,OAAO,CAAC;IAC5B,wBAAwB,EAAE,OAAO,CAAC;IAClC,WAAW,EAAE,OAAO,CAAC,cAAc,CAAC,aAAa,CAAC,EAAE,SAAS,CAAC,CAAC;IAC/D,WAAW,EAAE,OAAO,CAAC,cAAc,CAAC,aAAa,CAAC,EAAE,SAAS,CAAC,CAAC;IAC/D,OAAO,EAAE,SAAS,CAAC;IAEnB,aAAa,EAAE;QACX,MAAM,EAAE,MAAM,CAAA;QACd,GAAG,EAAE,MAAM,CAAA;QACX,KAAK,EAAE,MAAM,CAAA;QACb,OAAO,EAAE,OAAO,CAAA;QAChB,EAAE,EAAE,MAAM,CAAC;QACX,UAAU,EAAE,MAAM,CAAA;KACrB,CAAA;CACJ,CAAC;AAEF,wBAAgB,cAAc,CAAC,SAAS,EAAE,OAAO,GAAG,OAAO,CAAC,SAAS,IAAI,SAAS,CAyBjF;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,SAAS,GAAG,MAAM,CAoB5D"}
@@ -1,57 +0,0 @@
1
- import { convertFactory } from '@graphql-codegen/visitor-plugin-common';
2
- export const DefaultValues = {
3
- String: "string",
4
- Int: 42,
5
- Float: 4.2,
6
- Boolean: true,
7
- ID: "xxxx-xxxx-xxxx-xxxx",
8
- listLength: 3
9
- };
10
- export function validateConfig(rawConfig) {
11
- if (typeof rawConfig !== 'object' || rawConfig === null) {
12
- throw new Error('`options` must be an object');
13
- }
14
- if (!('typesFile' in rawConfig)) {
15
- throw new Error('`option.typesFile` is required');
16
- }
17
- if (typeof rawConfig['typesFile'] !== 'string') {
18
- throw new Error('`options.typesFile` must be a string');
19
- }
20
- if ('skipTypename' in rawConfig && typeof rawConfig['skipTypename'] !== 'boolean') {
21
- throw new Error('`options.skipTypename` must be a boolean');
22
- }
23
- if ('skipIsAbstractType' in rawConfig && typeof rawConfig['skipIsAbstractType'] !== 'boolean') {
24
- throw new Error('`options.skipIsAbstractType` must be a boolean');
25
- }
26
- if ('nonOptionalDefaultFields' in rawConfig && typeof rawConfig['nonOptionalDefaultFields'] !== 'boolean') {
27
- throw new Error('`options.nonOptionalDefaultFields` must be a boolean');
28
- }
29
- if ('typesPrefix' in rawConfig && typeof rawConfig['typesPrefix'] !== 'string') {
30
- throw new Error('`options.typesPrefix` must be a string');
31
- }
32
- if ('typesSuffix' in rawConfig && typeof rawConfig['typesSuffix'] !== 'string') {
33
- throw new Error('`options.typesSuffix` must be a string');
34
- }
35
- }
36
- export function normalizeConfig(rawConfig) {
37
- return {
38
- typesFile: rawConfig.typesFile,
39
- skipTypename: rawConfig.skipTypename ?? false,
40
- skipIsAbstractType: rawConfig.skipIsAbstractType ?? true,
41
- nonOptionalDefaultFields: rawConfig.nonOptionalDefaultFields ?? false,
42
- typesPrefix: rawConfig.typesPrefix ?? '',
43
- typesSuffix: rawConfig.typesSuffix ?? '',
44
- convert: rawConfig.namingConvention
45
- ? convertFactory({ namingConvention: rawConfig.namingConvention })
46
- : convertFactory({}),
47
- defaultValues: {
48
- String: rawConfig.defaultValues?.String ?? DefaultValues.String,
49
- Int: rawConfig.defaultValues?.Int ?? DefaultValues.Int,
50
- Float: rawConfig.defaultValues?.Float ?? DefaultValues.Float,
51
- Boolean: rawConfig.defaultValues?.Boolean ?? DefaultValues.Boolean,
52
- ID: rawConfig.defaultValues?.ID ?? DefaultValues.ID,
53
- listLength: rawConfig.defaultValues?.listLength ?? DefaultValues.listLength
54
- }
55
- };
56
- }
57
- //# sourceMappingURL=config.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"config.js","sourceRoot":"","sources":["../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,CAAC"}
@@ -1,32 +0,0 @@
1
- import { GraphQLSchema } from 'graphql';
2
- import { Config } from './config.js';
3
- type ValuePrimitive = string | number | boolean | null;
4
- type ValueArray = ValuePrimitive[];
5
- type ValueObject = Record<string, ValuePrimitive | ValueArray>;
6
- export type ExampleDirectiveValue = {
7
- value: ValuePrimitive | ValueArray | ValueObject;
8
- };
9
- export type ExampleDirectionExpresion = {
10
- expression: string;
11
- };
12
- export type ExampleDirective = ExampleDirectiveValue | ExampleDirectionExpresion;
13
- type FieldInfo = {
14
- name: string;
15
- example?: ExampleDirective | undefined;
16
- };
17
- export type ObjectTypeInfo = {
18
- type: 'object';
19
- name: string;
20
- fields: FieldInfo[];
21
- };
22
- export type AbstractTypeInfo = {
23
- type: 'abstract';
24
- name: string;
25
- possibleTypes: string[];
26
- comment?: string | undefined;
27
- example?: ExampleDirective | undefined;
28
- };
29
- export type TypeInfo = ObjectTypeInfo | AbstractTypeInfo;
30
- export declare function getTypeInfos(config: Config, schema: GraphQLSchema): TypeInfo[];
31
- export {};
32
- //# sourceMappingURL=schema-scanner.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"schema-scanner.d.ts","sourceRoot":"","sources":["../../src/schema-scanner.ts"],"names":[],"mappings":"AACA,OAAO,EAIH,aAAa,EAWhB,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AA8CrC,KAAK,cAAc,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AACvD,KAAK,UAAU,GAAG,cAAc,EAAE,CAAC;AACnC,KAAK,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,cAAc,GAAG,UAAU,CAAC,CAAC;AAC/D,MAAM,MAAM,qBAAqB,GAAG;IAChC,KAAK,EAAE,cAAc,GAAG,UAAU,GAAG,WAAW,CAAA;CACnD,CAAC;AACF,MAAM,MAAM,yBAAyB,GAAG;IACpC,UAAU,EAAE,MAAM,CAAA;CACrB,C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