UNPKG

apollo-codegen

Version:

Generate API code or type annotations based on a GraphQL schema and query documents

103 lines (86 loc) 3.07 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.typeDeclaration = typeDeclaration; exports.propertyDeclaration = propertyDeclaration; exports.propertyDeclarations = propertyDeclarations; exports.unionDeclaration = unionDeclaration; var _printing = require('../utilities/printing'); var _changeCase = require('change-case'); function typeDeclaration(generator, _ref, closure) { var interfaceName = _ref.interfaceName, noBrackets = _ref.noBrackets; generator.printNewlineIfNeeded(); generator.printNewline(); generator.print(`export type ${interfaceName} =`); generator.pushScope({ typeName: interfaceName }); if (!noBrackets) { generator.withinBlock(closure, ' {|', '|}'); } else { generator.withinBlock(closure, '', ''); } generator.popScope(); generator.print(';'); } function propertyDeclaration(generator, _ref2, closure) { var propertyName = _ref2.propertyName, typeName = _ref2.typeName, description = _ref2.description, isArray = _ref2.isArray, isNullable = _ref2.isNullable, inInterface = _ref2.inInterface, fragmentSpreads = _ref2.fragmentSpreads; generator.printOnNewline(description && `// ${description}`); if (closure) { generator.printOnNewline(`${propertyName}:`); if (isNullable) { generator.print(' ?'); } if (isArray) { if (!isNullable) { generator.print(' '); } generator.print('Array<'); } generator.pushScope({ typeName: propertyName }); generator.withinBlock(function () { if (fragmentSpreads && fragmentSpreads.length > 0) { fragmentSpreads.forEach(function (n) { return generator.printOnNewline(`...${(0, _changeCase.pascalCase)(n)}Fragment,`); }); } closure(); }, ' {|', '|}'); generator.popScope(); if (isArray) { generator.print(' >'); } } else if (fragmentSpreads && fragmentSpreads.length === 1) { generator.printOnNewline(`${propertyName}: ${isArray ? 'Array<' : ''}${(0, _changeCase.pascalCase)(fragmentSpreads[0])}Fragment${isArray ? '>' : ''}`); } else if (fragmentSpreads && fragmentSpreads.length > 1) { generator.printOnNewline(`${propertyName}: ${isArray ? 'Array<' : ''}`); generator.withinBlock(function () { fragmentSpreads.forEach(function (n) { return generator.printOnNewline(`...${(0, _changeCase.pascalCase)(n)}Fragment,`); }); }, '{|', '|}'); generator.print(isArray ? '>' : ''); } else { generator.printOnNewline(`${propertyName}: ${typeName}`); } generator.print(','); } function propertyDeclarations(generator, properties) { if (!properties) return; properties.forEach(function (property) { return propertyDeclaration(generator, property); }); } function unionDeclaration(generator, typeNames) { if (!typeNames) throw new Error('Union Declaration requires types'); typeNames.forEach(function (typeName) { generator.printOnNewline(`| ${typeName}`); }); } //# sourceMappingURL=language.js.map