@newmo/graphql-fake-server 0.1.1
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/LICENSE +22 -0
- package/README.md +39 -0
- package/dist/esm/cli.d.ts +3 -0
- package/dist/esm/cli.d.ts.map +1 -0
- package/dist/esm/cli.js +90 -0
- package/dist/esm/cli.js.map +1 -0
- package/dist/esm/code-generator.d.ts +4 -0
- package/dist/esm/code-generator.d.ts.map +1 -0
- package/dist/esm/code-generator.js +75 -0
- package/dist/esm/code-generator.js.map +1 -0
- package/dist/esm/config.d.ts +46 -0
- package/dist/esm/config.d.ts.map +1 -0
- package/dist/esm/config.js +57 -0
- package/dist/esm/config.js.map +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/schema-scanner.d.ts +32 -0
- package/dist/esm/schema-scanner.d.ts.map +1 -0
- package/dist/esm/schema-scanner.js +221 -0
- package/dist/esm/schema-scanner.js.map +1 -0
- package/package.json +98 -0
- package/src/cli.ts +101 -0
- package/src/code-generator.ts +82 -0
- package/src/config.ts +96 -0
- package/src/index.ts +15 -0
- package/src/schema-scanner.ts +297 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 mizdra
|
|
4
|
+
Copyright (c) 2022 Yosuke Kurami
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
in the Software without restriction, including without limitation the rights
|
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
furnished to do so, subject to the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# @newmo/graphql-fake-server
|
|
2
|
+
|
|
3
|
+
GraphQL Fake Server.
|
|
4
|
+
|
|
5
|
+
## Motivation
|
|
6
|
+
|
|
7
|
+
- Static Path
|
|
8
|
+
- Support Declarative Fake via `@example` directive.
|
|
9
|
+
- [ ] Dynamic Path
|
|
10
|
+
- Support Framework-Agnostic Fake for testing via HTTP
|
|
11
|
+
|
|
12
|
+
## Installation
|
|
13
|
+
|
|
14
|
+
- [ ] Describe the installation process
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
- [ ] Write usage instructions
|
|
19
|
+
|
|
20
|
+
## Tests
|
|
21
|
+
|
|
22
|
+
- [ ] Write How to Tests
|
|
23
|
+
|
|
24
|
+
## Contributing
|
|
25
|
+
|
|
26
|
+
1. Fork it!
|
|
27
|
+
2. Create your feature branch: `git checkout -b my-new-feature`
|
|
28
|
+
3. Commit your changes: `git commit -am 'Add some feature'`
|
|
29
|
+
4. Push to the branch: `git push origin my-new-feature`
|
|
30
|
+
5. Submit a pull request :D
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
MIT
|
|
35
|
+
|
|
36
|
+
## Credits
|
|
37
|
+
|
|
38
|
+
- [mizdra/graphql-codegen-typescript-fabbrica: GraphQL Code Generator Plugin to define fake data factory.](https://github.com/mizdra/graphql-codegen-typescript-fabbrica)
|
|
39
|
+
- [graphql-kit/graphql-faker: 🎲 Mock or extend your GraphQL API with faked data. No coding required.](https://github.com/graphql-kit/graphql-faker)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":""}
|
package/dist/esm/cli.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import fs from "node:fs/promises";
|
|
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 { generateCode } from "./code-generator.js";
|
|
11
|
+
import { normalizeConfig } from "./config.js";
|
|
12
|
+
import { getTypeInfos } from "./schema-scanner.js";
|
|
13
|
+
const HELP = `
|
|
14
|
+
Usage: cli <file.graphql>
|
|
15
|
+
`;
|
|
16
|
+
// cli foo.graphql
|
|
17
|
+
const { positionals, values } = parseArgs({
|
|
18
|
+
args: process.argv.slice(2), allowPositionals: true,
|
|
19
|
+
options: {
|
|
20
|
+
// --port
|
|
21
|
+
port: {
|
|
22
|
+
type: "string",
|
|
23
|
+
description: "Port to run the server on",
|
|
24
|
+
default: "4000",
|
|
25
|
+
},
|
|
26
|
+
verbose: {
|
|
27
|
+
type: "boolean",
|
|
28
|
+
description: "Verbose output",
|
|
29
|
+
default: false
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
if (!positionals.length) {
|
|
34
|
+
console.info(HELP);
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
const [filePath] = positionals;
|
|
38
|
+
if (!filePath) {
|
|
39
|
+
console.error(HELP);
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
42
|
+
const port = values.port ? Number.parseInt(values.port, 10) : NaN;
|
|
43
|
+
if (Number.isNaN(port)) {
|
|
44
|
+
console.error("--port must be a number");
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
const startFakeServer = async ({ schema, mockObject }) => {
|
|
48
|
+
const mocks = Object.fromEntries(Object.entries(mockObject).map(([key, value]) => {
|
|
49
|
+
return [key, () => value];
|
|
50
|
+
}));
|
|
51
|
+
const server = new ApolloServer({
|
|
52
|
+
schema: addMocksToSchema({
|
|
53
|
+
schema: makeExecutableSchema({
|
|
54
|
+
typeDefs: schema
|
|
55
|
+
}),
|
|
56
|
+
mocks,
|
|
57
|
+
}),
|
|
58
|
+
});
|
|
59
|
+
const { url } = await startStandaloneServer(server, { listen: { port: port } });
|
|
60
|
+
console.log(`🚀 Server listening at: ${url}`);
|
|
61
|
+
};
|
|
62
|
+
try {
|
|
63
|
+
const schema = buildSchema(await fs.readFile(filePath, "utf-8"));
|
|
64
|
+
const normalizedConfig = normalizeConfig({
|
|
65
|
+
typesFile: "types.ts",
|
|
66
|
+
});
|
|
67
|
+
const typeInfos = getTypeInfos(normalizedConfig, schema);
|
|
68
|
+
const code = generateCode(normalizedConfig, typeInfos);
|
|
69
|
+
if (values.verbose) {
|
|
70
|
+
console.info("Generated code:");
|
|
71
|
+
console.info(code);
|
|
72
|
+
}
|
|
73
|
+
// execute code in vm and get all exports
|
|
74
|
+
const exports = {};
|
|
75
|
+
vm.runInNewContext(code, { exports });
|
|
76
|
+
if (values.verbose) {
|
|
77
|
+
console.info("Exports:");
|
|
78
|
+
console.info(exports);
|
|
79
|
+
}
|
|
80
|
+
await startFakeServer({
|
|
81
|
+
schema,
|
|
82
|
+
mockObject: exports
|
|
83
|
+
});
|
|
84
|
+
// write to file
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
console.error(error);
|
|
88
|
+
process.exit(1);
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=cli.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,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,WAAW,EAAE,MAAM,SAAS,CAAC;AAEtC,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,MAAM,IAAI,GAAG;;CAEZ,CAAC;AACF,kBAAkB;AAClB,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,SAAS,CAAC;IACtC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,gBAAgB,EAAE,IAAI;IACnD,OAAO,EAAE;QACL,SAAS;QACT,IAAI,EAAE;YACF,IAAI,EAAE,QAAQ;YACd,WAAW,EAAE,2BAA2B;YACxC,OAAO,EAAE,MAAM;SAClB;QACD,OAAO,EAAE;YACL,IAAI,EAAE,SAAS;YACf,WAAW,EAAE,gBAAgB;YAC7B,OAAO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|
|
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"}
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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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O,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"}
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,57 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,EAA6B,cAAc,EAAE,MAAM,wCAAwC,CAAC;AAqBnG,MAAM,CAAC,MAAM,aAAa,GAAG;IACzB,MAAM,EAAE,QAAQ;IAChB,GAAG,EAAE,EAAE;IACP,KAAK,EAAE,GAAG;IACV,OAAO,EAAE,IAAI;IACb,EAAE,EAAE,qBAAqB;IACzB,UAAU,EAAE,CAAC;CAChB,CAAA;AAoBD,MAAM,UAAU,cAAc,CAAC,SAAkB;IAC7C,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,KAAK,IAAI,EAAE,CAAC;QACtD,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACnD,CAAC;IACD,IAAI,CAAC,CAAC,WAAW,IAAI,SAAS,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACtD,CAAC;IACD,IAAI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,IAAI,EAAE;QACxC,OAAO,EAAE,SAAS,CAAC,gBAAgB;YAC/B,CAAC,CAAC,cAAc,CAAC,EAAE,gBAAgB,EAAE,SAAS,CAAC,gBAAgB,EAAE,CAAC;YAClE,CAAC,CAAC,cAAc,CAAC,EAAE,CAAC;QACxB,aAAa,EAAE;YACX,MAAM,EAAE,SAAS,CAAC,aAAa,EAAE,MAAM,IAAI,aAAa,CAAC,MAAM;YAC/D,GAAG,EAAE,SAAS,CAAC,aAAa,EAAE,GAAG,IAAI,aAAa,CAAC,GAAG;YACtD,KAAK,EAAE,SAAS,CAAC,aAAa,EAAE,KAAK,IAAI,aAAa,CAAC,KAAK;YAC5D,OAAO,EAAE,SAAS,CAAC,aAAa,EAAE,OAAO,IAAI,aAAa,CAAC,OAAO;YAClE,EAAE,EAAE,SAAS,CAAC,aAAa,EAAE,EAAE,IAAI,aAAa,CAAC,EAAE;YACnD,UAAU,EAAE,SAAS,CAAC,aAAa,EAAE,UAAU,IAAI,aAAa,CAAC,UAAU;SAC9E;KACJ,CAAC;AACN,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,iCAAiC,CAAC;AAKtE,eAAO,MAAM,MAAM,EAAE,cAOpB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// MEMO: The tests for this module are covered by `e2e/*.e2e.ts`.
|
|
2
|
+
import { generateCode } from './code-generator.js';
|
|
3
|
+
import { normalizeConfig, validateConfig } from './config.js';
|
|
4
|
+
import { getTypeInfos } from './schema-scanner.js';
|
|
5
|
+
export const plugin = (schema, _documents, config, _info) => {
|
|
6
|
+
validateConfig(config);
|
|
7
|
+
const normalizedConfig = normalizeConfig(config);
|
|
8
|
+
const typeInfos = getTypeInfos(normalizedConfig, schema);
|
|
9
|
+
const code = generateCode(normalizedConfig, typeInfos);
|
|
10
|
+
return code;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,iEAAiE;AAGjE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC9D,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnD,MAAM,CAAC,MAAM,MAAM,GAAmB,CAAC,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE;IACxE,cAAc,CAAC,MAAM,CAAC,CAAC;IAEvB,MAAM,gBAAgB,GAAG,eAAe,CAAC,MAAM,CAAC,CAAC;IACjD,MAAM,SAAS,GAAG,YAAY,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;IACzD,MAAM,IAAI,GAAG,YAAY,CAAC,gBAAgB,EAAE,SAAS,CAAC,CAAC;IACvD,OAAO,IAAI,CAAC;AAChB,CAAC,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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;AAqCrC,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,CAAA;AACD,MAAM,MAAM,gBAAgB,GAAG,qBAAqB,GAAG,yBAAyB,CAAC;AAkJjF,KAAK,SAAS,GAAG;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAC;CAC1C,CAAA;AACD,MAAM,MAAM,cAAc,GAAG;IACzB,IAAI,EAAE,QAAQ,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,SAAS,EAAE,CAAC;CACvB,CAAC;AACF,MAAM,MAAM,gBAAgB,GAAG;IAC3B,IAAI,EAAE,UAAU,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,aAAa,EAAE,MAAM,EAAE,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,OAAO,CAAC,EAAE,gBAAgB,GAAG,SAAS,CAAA;CACzC,CAAC;AACF,MAAM,MAAM,QAAQ,GAAG,cAAc,GAAG,gBAAgB,CAAC;AAEzD,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,GAAG,QAAQ,EAAE,CAqE9E"}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { transformComment } from '@graphql-codegen/visitor-plugin-common';
|
|
2
|
+
import { Kind, } from 'graphql';
|
|
3
|
+
// The fork of https://github.com/dotansimha/graphql-code-generator/blob/e1dc75f3c598bf7f83138ca533619716fc73f823/packages/plugins/typescript/resolvers/src/visitor.ts#L85-L91
|
|
4
|
+
// The fork of https://github.com/dotansimha/graphql-code-generator/blob/ba84a3a2758d94dac27fcfbb1bafdf3ed7c32929/packages/plugins/other/visitor-plugin-common/src/base-visitor.ts#L422
|
|
5
|
+
function convertName(node, config) {
|
|
6
|
+
let convertedName = '';
|
|
7
|
+
convertedName += config.typesPrefix;
|
|
8
|
+
convertedName += config.convert(node);
|
|
9
|
+
convertedName += config.typesSuffix;
|
|
10
|
+
return convertedName;
|
|
11
|
+
}
|
|
12
|
+
const parseTypeNodeStructure = (node) => {
|
|
13
|
+
if (node.kind === Kind.NON_NULL_TYPE) {
|
|
14
|
+
return parseTypeNodeStructure(node.type);
|
|
15
|
+
}
|
|
16
|
+
else if (node.kind === Kind.LIST_TYPE) {
|
|
17
|
+
return `array`;
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
// string, number, boolean, null
|
|
21
|
+
if (node.name.value === "String") {
|
|
22
|
+
return "string";
|
|
23
|
+
}
|
|
24
|
+
if (node.name.value === "Int") {
|
|
25
|
+
return "number";
|
|
26
|
+
}
|
|
27
|
+
if (node.name.value === "Float") {
|
|
28
|
+
return "number";
|
|
29
|
+
}
|
|
30
|
+
if (node.name.value === "Boolean") {
|
|
31
|
+
return "boolean";
|
|
32
|
+
}
|
|
33
|
+
if (node.name.value === "ID") {
|
|
34
|
+
return "string";
|
|
35
|
+
}
|
|
36
|
+
return `object`;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
function valueOf(value) {
|
|
40
|
+
// object
|
|
41
|
+
if (value.kind === Kind.OBJECT) {
|
|
42
|
+
return value.fields.reduce((acc, field) => {
|
|
43
|
+
// @ts-expect-error TODO: nesting type
|
|
44
|
+
acc[field.name.value] = valueOf(field.value);
|
|
45
|
+
return acc;
|
|
46
|
+
}, {});
|
|
47
|
+
}
|
|
48
|
+
// list
|
|
49
|
+
if (value.kind === Kind.LIST) {
|
|
50
|
+
return value.values.map(v => {
|
|
51
|
+
return valueOf(v);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
// null
|
|
55
|
+
if (value.kind === Kind.NULL) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
// string
|
|
59
|
+
if (value.kind === Kind.STRING) {
|
|
60
|
+
return value.value;
|
|
61
|
+
}
|
|
62
|
+
// enum
|
|
63
|
+
if (value.kind === Kind.ENUM) {
|
|
64
|
+
return value.value;
|
|
65
|
+
}
|
|
66
|
+
// int
|
|
67
|
+
if (value.kind === Kind.INT) {
|
|
68
|
+
return Number.parseInt(value.value, 10);
|
|
69
|
+
}
|
|
70
|
+
// float
|
|
71
|
+
if (value.kind === Kind.FLOAT) {
|
|
72
|
+
return Number.parseFloat(value.value);
|
|
73
|
+
}
|
|
74
|
+
// boolean
|
|
75
|
+
if (value.kind === Kind.BOOLEAN) {
|
|
76
|
+
return value.value;
|
|
77
|
+
}
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
79
|
+
throw new Error(`Unknown kind of value ${value}`);
|
|
80
|
+
}
|
|
81
|
+
const typeToFunction = (type, config) => {
|
|
82
|
+
switch (type) {
|
|
83
|
+
case "String":
|
|
84
|
+
return `"${config.defaultValues.String}"`;
|
|
85
|
+
case "Int":
|
|
86
|
+
return `${config.defaultValues.Int}`;
|
|
87
|
+
case "Float":
|
|
88
|
+
return `${config.defaultValues.Float}`;
|
|
89
|
+
case "Boolean":
|
|
90
|
+
return `${config.defaultValues.Boolean ? "true" : "false"}`;
|
|
91
|
+
case "ID":
|
|
92
|
+
return `"${config.defaultValues.ID}"`;
|
|
93
|
+
default:
|
|
94
|
+
// reference to the object
|
|
95
|
+
return `${type}`;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const typeToFunctionWithArray = (type, config) => {
|
|
99
|
+
return `Array.from({ length: ${config.defaultValues.listLength} }).map(() => ${typeToFunction(type, config)})`;
|
|
100
|
+
};
|
|
101
|
+
// NamedType/ListType handling
|
|
102
|
+
const nodeToExpression = ({ currentNode, isArray = false, config }) => {
|
|
103
|
+
if (currentNode.kind === "NonNullType") {
|
|
104
|
+
return nodeToExpression({
|
|
105
|
+
currentNode: currentNode.type, isArray, config
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else if (currentNode.kind === "NamedType") {
|
|
109
|
+
if (isArray) {
|
|
110
|
+
return {
|
|
111
|
+
expression: typeToFunctionWithArray(currentNode.name.value, config)
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
return {
|
|
116
|
+
expression: typeToFunction(currentNode.name.value, config)
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else if (currentNode.kind === "ListType") {
|
|
121
|
+
return nodeToExpression({ currentNode: currentNode.type, isArray: true, config });
|
|
122
|
+
}
|
|
123
|
+
throw new Error("Unknown node kind");
|
|
124
|
+
};
|
|
125
|
+
function parseFieldOrInputValueDefinition(node, convertedTypeName, config) {
|
|
126
|
+
const exampleDirective = node.directives?.find(d => d.name.value === "example");
|
|
127
|
+
// fake
|
|
128
|
+
// if @example directive is not found, return random value for the scalar type
|
|
129
|
+
if (!exampleDirective) {
|
|
130
|
+
return {
|
|
131
|
+
example: nodeToExpression({ currentNode: node.type, config })
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
if (!exampleDirective.arguments) {
|
|
135
|
+
throw new Error("@example directive must have arguments");
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* @example(value: "value")
|
|
139
|
+
* -> { value: "value" }
|
|
140
|
+
*/
|
|
141
|
+
const value = exampleDirective.arguments.find(a => a.name.value === "value");
|
|
142
|
+
if (value) {
|
|
143
|
+
// if node type is not equal to the value type, throw an error
|
|
144
|
+
const rawValue = valueOf(value.value);
|
|
145
|
+
const nodeType = parseTypeNodeStructure(node.type);
|
|
146
|
+
const fieldName = node.name.value;
|
|
147
|
+
// array, object, string, number, boolean, null
|
|
148
|
+
const rawValueType = Object.prototype.toString.call(rawValue).slice(8, -1).toLowerCase();
|
|
149
|
+
if (nodeType !== rawValueType) {
|
|
150
|
+
throw new Error(`${convertedTypeName}.${fieldName}: @example directive value type must be ${nodeType}. @example(value: ${nodeType})`);
|
|
151
|
+
}
|
|
152
|
+
return { example: { value: rawValue } };
|
|
153
|
+
}
|
|
154
|
+
throw new Error(`@example directive must have value argument. @example(value: "value")`);
|
|
155
|
+
}
|
|
156
|
+
function parseObjectTypeOrInputObjectTypeDefinition(node, config) {
|
|
157
|
+
const originalTypeName = node.name.value;
|
|
158
|
+
const convertedTypeName = convertName(originalTypeName, config);
|
|
159
|
+
return {
|
|
160
|
+
type: 'object',
|
|
161
|
+
name: originalTypeName,
|
|
162
|
+
fields: [
|
|
163
|
+
...(node.fields ?? []).map((field) => ({
|
|
164
|
+
name: field.name.value,
|
|
165
|
+
...parseFieldOrInputValueDefinition(field, convertedTypeName, config),
|
|
166
|
+
})),
|
|
167
|
+
],
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
export function getTypeInfos(config, schema) {
|
|
171
|
+
const types = Object.values(schema.getTypeMap());
|
|
172
|
+
const userDefinedTypeDefinitions = types
|
|
173
|
+
.map((type) => type.astNode)
|
|
174
|
+
.filter((node) => {
|
|
175
|
+
if (!node)
|
|
176
|
+
return false;
|
|
177
|
+
return (node.kind === Kind.OBJECT_TYPE_DEFINITION ||
|
|
178
|
+
node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ||
|
|
179
|
+
node.kind === Kind.INTERFACE_TYPE_DEFINITION ||
|
|
180
|
+
node.kind === Kind.UNION_TYPE_DEFINITION);
|
|
181
|
+
});
|
|
182
|
+
const objectTypeDefinitions = userDefinedTypeDefinitions.filter((node) => {
|
|
183
|
+
if (!node)
|
|
184
|
+
return false;
|
|
185
|
+
return node.kind === Kind.OBJECT_TYPE_DEFINITION;
|
|
186
|
+
});
|
|
187
|
+
return types
|
|
188
|
+
.map((type) => type.astNode)
|
|
189
|
+
.filter((node) => {
|
|
190
|
+
if (!node)
|
|
191
|
+
return false;
|
|
192
|
+
return (node.kind === Kind.OBJECT_TYPE_DEFINITION ||
|
|
193
|
+
node.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION ||
|
|
194
|
+
node.kind === Kind.INTERFACE_TYPE_DEFINITION ||
|
|
195
|
+
node.kind === Kind.UNION_TYPE_DEFINITION);
|
|
196
|
+
})
|
|
197
|
+
.map((node) => {
|
|
198
|
+
if (node?.kind === Kind.OBJECT_TYPE_DEFINITION || node?.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION) {
|
|
199
|
+
return parseObjectTypeOrInputObjectTypeDefinition(node, config);
|
|
200
|
+
}
|
|
201
|
+
else if (node?.kind === Kind.INTERFACE_TYPE_DEFINITION) {
|
|
202
|
+
return {
|
|
203
|
+
type: 'abstract',
|
|
204
|
+
name: convertName(node.name.value, config),
|
|
205
|
+
possibleTypes: objectTypeDefinitions
|
|
206
|
+
.filter((objectTypeDefinitionNode) => (objectTypeDefinitionNode.interfaces ?? []).some((i) => i.name.value === node.name.value))
|
|
207
|
+
.map((objectTypeDefinitionNode) => convertName(objectTypeDefinitionNode.name.value, config)),
|
|
208
|
+
comment: node.description ? transformComment(node.description) : undefined,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
return {
|
|
213
|
+
type: 'abstract',
|
|
214
|
+
name: convertName(node.name.value, config),
|
|
215
|
+
possibleTypes: (node.types ?? []).map((type) => convertName(type.name.value, config)),
|
|
216
|
+
comment: node.description ? transformComment(node.description) : undefined,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=schema-scanner.js.map
|