graphql-codegen
Version:
Generate client code based on a GraphQL schema and query documents
51 lines (38 loc) • 1.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.typeNameFromGraphQLType = typeNameFromGraphQLType;
exports.typeDeclarationForGraphQLType = typeDeclarationForGraphQLType;
var _printing = require('../utilities/printing.js');
var _changeCase = require('change-case');
var _graphql = require('graphql');
function typeNameFromGraphQLType(type, unmodifiedTypeName) {
var nullable = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
if (type instanceof _graphql.GraphQLNonNull) {
return typeNameFromGraphQLType(type.ofType, unmodifiedTypeName, false);
}
var typeName = void 0;
if (type instanceof _graphql.GraphQLList) {
typeName = '[' + typeNameFromGraphQLType(type.ofType, unmodifiedTypeName, true) + ']';
} else if (type === _graphql.GraphQLID) {
typeName = 'GraphQLID';
} else {
typeName = unmodifiedTypeName || type.name;
}
return nullable ? typeName + '?' : typeName;
}
function typeDeclarationForGraphQLType(type) {
if (type instanceof _graphql.GraphQLEnumType) {
return enumerationDeclaration(type);
}
}
function enumerationDeclaration(type) {
var name = type.name;
var description = type.description;
var values = type.getValues();
var caseDeclarations = values.map(function (value) {
return 'case ' + (0, _changeCase.camelCase)(value.name) + ' = "' + value.value + '"' + (0, _printing.wrap)(' /// ', value.description);
});
return (0, _printing.join)([description && '/// ' + description + '\n', 'public enum ' + name + ': String ', (0, _printing.block)(caseDeclarations), '\n\n', 'extension ' + name + ': JSONDecodable, JSONEncodable {}']);
}