UNPKG

graphql-codegen

Version:

Generate client code based on a GraphQL schema and query documents

137 lines (102 loc) 6.09 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray'); var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2); exports.generateSource = generateSource; var _printing = require('../utilities/printing.js'); var _changeCase = require('change-case'); var _graphql = require('graphql'); var _types = require('./types'); var _properties = require('./properties'); var _strings = require('./strings'); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function generateSource(_ref) { var typesUsed = _ref.typesUsed; var queries = _ref.queries; var fragments = _ref.fragments; var typeDefinitions = typesUsed.map(_types.typeDeclarationForGraphQLType); var queryClassDefinitions = queries.map(classDefinitionForQuery); var fragmentClassDefinitions = fragments.map(classDefinitionForFragment); return (0, _printing.join)(['// This file was automatically generated and should not be edited.\n\n', importDeclarations() + '\n', (0, _printing.wrap)('\n', (0, _printing.join)(typeDefinitions, '\n\n'), '\n'), (0, _printing.wrap)('\n', (0, _printing.join)(queryClassDefinitions, '\n\n'), '\n'), (0, _printing.wrap)('\n', (0, _printing.join)(fragmentClassDefinitions, '\n\n'), '\n')]); } function importDeclarations() { return 'import Apollo'; } function classDefinitionForQuery(_ref2) { var name = _ref2.name; var variables = _ref2.variables; var fields = _ref2.fields; var source = _ref2.source; var fragmentNames = _ref2.fragmentNames; var className = (0, _changeCase.pascalCase)(name) + 'Query'; var properties = (0, _properties.propertiesFromFields)(fields); var instancePropertyDeclarations = (0, _printing.join)(variables.map(function (variable) { return 'public let ' + variable.name + ': ' + (0, _types.typeNameFromGraphQLType)(variable.type); }), '\n'); var initializerDeclaration = function () { var initializationParameters = (0, _printing.join)(variables.map(function (variable) { return variable.name + ': ' + (0, _types.typeNameFromGraphQLType)(variable.type); }), ', '); var propertyInitializations = variables.map(function (variable) { return 'self.' + variable.name + ' = ' + variable.name; }); return 'public init(' + initializationParameters + ') ' + (0, _printing.block)(propertyInitializations); }(); var variablesProperty = function () { if (variables.length < 1) return null; var variablesMap = (0, _printing.wrap)('return [', (0, _printing.join)(variables.map(function (variable) { return '"' + variable.name + '": ' + variable.name; }), ', '), ']'); return 'public var variables: GraphQLMap? ' + (0, _printing.block)([variablesMap]); }(); var queryDocument = void 0; if (fragmentNames && fragmentNames.length > 0) { queryDocument = 'public var queryDocument: String ' + (0, _printing.block)([(0, _printing.join)(['return operationDefinition'].concat((0, _toConsumableArray3.default)(fragmentNames.map(function (fragmentName) { return '.appending(' + protocolNameForFragmentName(fragmentName) + 'Fragment.fragmentDefinition)'; }))))]); } return 'public class ' + className + ': GraphQLQuery ' + (0, _printing.block)([(0, _printing.wrap)('', instancePropertyDeclarations, '\n'), initializerDeclaration, (0, _printing.wrap)('\n', 'public let operationDefinition =' + (0, _printing.indent)('\n' + (0, _strings.multilineString)(source))), (0, _printing.wrap)('\n', queryDocument), (0, _printing.wrap)('\n', variablesProperty), (0, _printing.wrap)('\n', structDeclaration({ name: "Data", properties: properties }))]); } function structDeclaration(_ref3) { var name = _ref3.name; var _ref3$properties = _ref3.properties; var properties = _ref3$properties === undefined ? [] : _ref3$properties; var fragmentNames = _ref3.fragmentNames; var propertyDeclarations = properties.map(function (_ref4) { var name = _ref4.name; var typeName = _ref4.typeName; return 'public let ' + name + ': ' + typeName; }); var initializerDeclaration = 'public init(map: GraphQLMap) throws ' + (0, _printing.block)(properties.map(function (property) { return property.name + ' = try map.' + (property.isList ? 'list' : 'value') + '(forKey: "' + property.fieldName + '")'; })); var compositeProperties = properties.filter(function (property) { return property.isComposite; }); var nestedStructDeclarations = compositeProperties.map(function (property) { return (0, _printing.wrap)('\n', structDeclaration({ name: property.unmodifiedTypeName, properties: property.subproperties, fragmentNames: property.fragmentNames })); }); return (0, _printing.join)(['public struct ' + name + ': GraphQLMapConvertible', (0, _printing.wrap)(', ', (0, _printing.join)(fragmentNames && fragmentNames.map(protocolNameForFragmentName), ', ')), ' ', (0, _printing.block)([(0, _printing.wrap)('', (0, _printing.join)(propertyDeclarations, '\n'), '\n'), initializerDeclaration, (0, _printing.join)(nestedStructDeclarations, '\n')])]); } function protocolNameForFragmentName(fragmentName) { return (0, _changeCase.pascalCase)(fragmentName); } function classDefinitionForFragment(_ref5) { var name = _ref5.name; var fields = _ref5.fields; var source = _ref5.source; var protocolName = protocolNameForFragmentName(name); var className = protocolName + 'Fragment'; var properties = (0, _properties.propertiesFromFields)(fields); return (0, _printing.join)(['public class ' + className + ': GraphQLFragment ', (0, _printing.block)(['public static let fragmentDefinition =' + (0, _printing.indent)('\n' + (0, _strings.multilineString)(source) + '\n'), 'public typealias Data = ' + protocolName]), '\n\n', 'public protocol ' + protocolName + ' ', (0, _printing.block)(properties.map(function (_ref6) { var name = _ref6.name; var typeName = _ref6.typeName; return 'var ' + name + ': ' + typeName + ' { get }'; }))]); }