graphql-codegen
Version:
Generate client code based on a GraphQL schema and query documents
96 lines (71 loc) • 4.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 generateSource(context) {
var typeDefinitions = context.getTypesUsed().map(function (type) {
return (0, _types.typeDeclarationForGraphQLType)(type);
});
var queryClassDefinitions = context.getQueries().map(function (query) {
return classDefinitionForQuery(query);
});
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')]);
}
function importDeclarations() {
return 'import Apollo';
}
function classDefinitionForQuery(_ref) {
var name = _ref.name;
var variables = _ref.variables;
var fields = _ref.fields;
var source = _ref.source;
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]);
}();
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', variablesProperty), (0, _printing.wrap)('\n', structDeclaration({ name: "Data", properties: properties }))]);
}
function structDeclaration(_ref2) {
var name = _ref2.name;
var _ref2$properties = _ref2.properties;
var properties = _ref2$properties === undefined ? [] : _ref2$properties;
var propertyDeclarations = properties.map(function (_ref3) {
var name = _ref3.name;
var typeName = _ref3.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 }));
});
return 'public struct ' + name + ': GraphQLMapConvertible ' + (0, _printing.block)([(0, _printing.wrap)('', (0, _printing.join)(propertyDeclarations, '\n'), '\n'), initializerDeclaration, (0, _printing.join)(nestedStructDeclarations, '\n')]);
}