UNPKG

ichigoo

Version:

Static site generator with React and GraphQL

88 lines (71 loc) 2.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getRelatedType = exports.getReverseRelatedField = exports.getRelatedKey = exports.getTypeFromKey = exports.getRelationshipFromKey = void 0; var _inflection = require("inflection"); /** * A bit of vocabulary * * Consider this data: * { * posts: [ * { id: 1, title: 'foo', user_id: 123 } * ], * users: [ * { id: 123, name: 'John Doe' } * ] * } * * We'll use the following names: * - key: the keys in the data map, e.g. 'posts', 'users' * - type: for a key, the related type in the graphQL schema, e.g. 'posts' => 'Post', 'users' => 'User' * - field: the keys in a record, e.g. 'id', 'foo', user_id' * - relationship field: a key ending in '_id', e.g. 'user_id' * - related key: for a relationship field, the related key, e.g. 'user_id' => 'users' */ /** * * @param {String} fieldName 'users' * @return {String} 'Users' */ var getRelationshipFromKey = function getRelationshipFromKey(key) { return (0, _inflection.camelize)(key); }; /** * * @param {String} fieldName 'users' * @return {String} 'User' */ exports.getRelationshipFromKey = getRelationshipFromKey; var getTypeFromKey = function getTypeFromKey(key) { return (0, _inflection.camelize)((0, _inflection.singularize)(key)); }; /** * * @param {String} fieldName 'user_id' * @return {String} 'users' */ exports.getTypeFromKey = getTypeFromKey; var getRelatedKey = function getRelatedKey(fieldName) { return (0, _inflection.pluralize)(fieldName.substr(0, fieldName.length - 3)); }; /** * * @param {String} key 'users' * @return {String} 'user_id' */ exports.getRelatedKey = getRelatedKey; var getReverseRelatedField = function getReverseRelatedField(key) { return "".concat((0, _inflection.singularize)(key), "_id"); }; /** * * @param {String} fieldName 'user_id' * @return {String} 'User' */ exports.getReverseRelatedField = getReverseRelatedField; var getRelatedType = function getRelatedType(fieldName) { return getTypeFromKey(fieldName.substr(0, fieldName.length - 3)); }; exports.getRelatedType = getRelatedType;