1 | "use strict";
|
2 | var __importStar = (this && this.__importStar) || function (mod) {
|
3 | if (mod && mod.__esModule) return mod;
|
4 | var result = {};
|
5 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
6 | result["default"] = mod;
|
7 | return result;
|
8 | };
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const t = __importStar(require("@babel/types"));
|
11 | const path_1 = require("path");
|
12 | const change_case_1 = require("change-case");
|
13 | const graphql_tool_utilities_1 = require("graphql-tool-utilities");
|
14 | class FileContext {
|
15 | constructor(path, options) {
|
16 | this.path = path;
|
17 | this.options = options;
|
18 | this.importedTypes = new Set();
|
19 | }
|
20 | get schemaImports() {
|
21 | const { path, importedTypes, options: { schemaTypesPath }, } = this;
|
22 | return importedTypes.size > 0
|
23 | ? t.importDeclaration([...importedTypes].map((type) => t.importSpecifier(t.identifier(type), t.identifier(type))), t.stringLiteral(importPath(path, schemaTypesPath)))
|
24 | : null;
|
25 | }
|
26 | import(type) {
|
27 | this.importedTypes.add(type);
|
28 | }
|
29 | }
|
30 | exports.FileContext = FileContext;
|
31 | class OperationContext {
|
32 | constructor(operation, ast, options, file) {
|
33 | this.operation = operation;
|
34 | this.ast = ast;
|
35 | this.options = options;
|
36 | this.file = file;
|
37 | this.exportedTypes = [];
|
38 | }
|
39 | get typeName() {
|
40 | let typeName;
|
41 | if (graphql_tool_utilities_1.isOperation(this.operation)) {
|
42 | const { operationName, operationType } = this.operation;
|
43 | typeName = `${change_case_1.ucFirst(operationName)}${change_case_1.ucFirst(operationType)}Data`;
|
44 | }
|
45 | else {
|
46 | const { fragmentName } = this.operation;
|
47 | typeName = `${change_case_1.ucFirst(fragmentName)}FragmentData`;
|
48 | }
|
49 | return this.options.partial
|
50 | ? typeName.replace(/Data$/, 'PartialData')
|
51 | : typeName;
|
52 | }
|
53 | get namespace() {
|
54 | const { exported, typeName } = this;
|
55 | return exported.length > 0
|
56 | ? t.tsModuleDeclaration(t.identifier(typeName), t.tsModuleBlock(exported.map((type) => t.exportNamedDeclaration(type, []))))
|
57 | : null;
|
58 | }
|
59 | get exported() {
|
60 | return this.exportedTypes;
|
61 | }
|
62 | export(type) {
|
63 | this.exportedTypes.push(type);
|
64 | return t.tsTypeReference(t.tsQualifiedName(t.identifier(this.typeName), t.identifier(type.id.name)));
|
65 | }
|
66 | }
|
67 | exports.OperationContext = OperationContext;
|
68 | function importPath(from, to) {
|
69 | const relativePath = path_1.relative(path_1.dirname(from), to);
|
70 | const normalizedPath = relativePath.startsWith('..')
|
71 | ? relativePath
|
72 | : `./${relativePath}`;
|
73 | return normalizedPath.replace(/\.ts$/, '');
|
74 | }
|