UNPKG

@aws-amplify/graphql-generator

Version:
131 lines 6.3 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GraphQLStatementsFormatter = void 0; const path = __importStar(require("path")); const prettier_1 = __importDefault(require("prettier")); const codeGeneration_1 = require("@aws-amplify/graphql-types-generator/lib/typescript/codeGeneration"); const CODEGEN_WARNING = 'this is an auto generated file. This will be overwritten'; const LINE_DELIMITOR = '\n'; class GraphQLStatementsFormatter { constructor(language, operation, typesPath) { var _a; this.language = language || 'graphql'; this.opTypeName = { queries: 'Query', mutations: 'Mutation', subscriptions: 'Subscription', }[operation]; this.lintOverrides = []; this.headerComments = []; if (typesPath) { const { dir, name } = path.parse(typesPath); const typesPathWithoutExtension = path.join(dir, name).split(((_a = path.win32) === null || _a === void 0 ? void 0 : _a.sep) || '\\').join(path.posix.sep); if (!typesPathWithoutExtension.startsWith('.')) { this.typesPath = `./${typesPathWithoutExtension}`; } else { this.typesPath = typesPathWithoutExtension; } } else { this.typesPath = null; } this.includeTypeScriptTypes = !!(this.language === 'typescript' && this.opTypeName && this.typesPath); } get typeDefs() { if (!this.includeTypeScriptTypes) return ''; return [ `import * as APITypes from '${this.typesPath}';`, `type Generated${this.opTypeName}<InputType, OutputType> = string & {`, ` __generated${this.opTypeName}Input: InputType;`, ` __generated${this.opTypeName}Output: OutputType;`, `};`, ].join(LINE_DELIMITOR); } format(statements) { switch (this.language) { case 'javascript': this.headerComments.push(CODEGEN_WARNING); this.lintOverrides.push('/* eslint-disable */'); return this.prettify(this.formatJS(statements)); case 'typescript': this.headerComments.push(CODEGEN_WARNING); this.lintOverrides.push(...['/* tslint:disable */', '/* eslint-disable */']); return this.prettify(this.formatJS(statements)); case 'flow': this.headerComments.push('@flow', CODEGEN_WARNING); return this.prettify(this.formatJS(statements)); default: this.headerComments.push(CODEGEN_WARNING); return this.prettify(this.formatGraphQL(statements)); } } formatGraphQL(statements) { const headerBuffer = this.headerComments.map(comment => `# ${comment}`).join(LINE_DELIMITOR); const statementsBuffer = statements ? [...statements.values()].map(s => s.graphql).join(LINE_DELIMITOR) : ''; const formattedOutput = [headerBuffer, LINE_DELIMITOR, statementsBuffer].join(LINE_DELIMITOR); return formattedOutput; } formatJS(statements) { const lintOverridesBuffer = this.lintOverrides.join(LINE_DELIMITOR); const headerBuffer = this.headerComments.map(comment => `// ${comment}`).join(LINE_DELIMITOR); const formattedStatements = []; if (statements) { for (const [key, { graphql, operationName, operationType }] of statements) { const typeTag = this.buildTypeTag(operationName, operationType); const formattedGraphQL = prettier_1.default.format(graphql, { parser: 'graphql' }); formattedStatements.push(`export const ${key} = /* GraphQL */ \`${formattedGraphQL}\`${typeTag}`); } } const typeDefs = this.includeTypeScriptTypes ? [LINE_DELIMITOR, this.typeDefs] : []; const formattedOutput = [lintOverridesBuffer, headerBuffer, ...typeDefs, LINE_DELIMITOR, ...formattedStatements].join(LINE_DELIMITOR); return formattedOutput; } buildTypeTag(operationName, operationType) { if (!this.includeTypeScriptTypes || operationName === undefined || operationType === undefined) return ''; const operationDef = { operationName, operationType }; const resultTypeName = `APITypes.${(0, codeGeneration_1.interfaceNameFromOperation)(operationDef)}`; const variablesTypeName = `APITypes.${(0, codeGeneration_1.interfaceVariablesNameFromOperation)(operationDef)}`; return ` as Generated${this.opTypeName}<${variablesTypeName}, ${resultTypeName}>;`; } prettify(output) { const parserMap = { javascript: 'babel', graphql: 'graphql', typescript: 'typescript', flow: 'flow', angular: 'graphql', }; return prettier_1.default.format(output, { parser: parserMap[this.language || 'graphql'] }); } } exports.GraphQLStatementsFormatter = GraphQLStatementsFormatter; //# sourceMappingURL=GraphQLStatementsFormatter.js.map