traxxx/src/web/plugins/actors.js

71 lines
2.2 KiB
JavaScript

'use strict';
const { makeExtendSchemaPlugin, gql } = require('graphile-utils');
const moment = require('moment');
const { cmToFeetInches, cmToInches, kgToLbs } = require('../../utils/convert');
const schemaExtender = makeExtendSchemaPlugin(_build => ({
typeDefs: gql`
enum Units {
METRIC
IMPERIAL
}
extend type Actor {
ageFromBirth: Int @requires(columns: ["dateOfBirth"])
ageAtDeath: Int @requires(columns: ["dateOfBirth", "dateOfDeath"])
height(units:Units): String @requires(columns: ["height"])
weight(units:Units): String @requires(columns: ["weight"])
penisLength(units:Units): String @requires(columns: ["penis_length"])
penisGirth(units:Units): String @requires(columns: ["penis_girth"])
}
`,
resolvers: {
Actor: {
ageFromBirth(parent, _args, _context, _info) {
if (!parent.dateOfBirth) return null;
return moment().diff(parent.dateOfBirth, 'years');
},
ageAtDeath(parent, _args, _context, _info) {
if (!parent.dateOfDeath) return null;
return moment(parent.dateOfDeath).diff(parent.dateOfBirth, 'years');
},
height(parent, args, _context, _info) {
if (!parent.height) return null;
if (args.units === 'IMPERIAL') {
const { feet, inches } = cmToFeetInches(parent.height);
return `${feet}' ${inches}"`;
}
return parent.height.toString();
},
weight(parent, args, _context, _info) {
if (!parent.weight) return null;
return args.units === 'IMPERIAL'
? kgToLbs(parent.weight).toString()
: parent.weight.toString();
},
penisLength(parent, args, _context, _info) {
if (!parent.penisLength) return null;
return args.units === 'IMPERIAL'
? (Math.round(cmToInches(parent.penisLength) * 4) / 4).toString() // round to nearest quarter inch
: parent.penisLength.toString();
},
penisGirth(parent, args, _context, _info) {
if (!parent.penisGirth) return null;
return args.units === 'IMPERIAL'
? (Math.round(cmToInches(parent.penisGirth) * 4) / 4).toString() // round to nearest quarter inch
: parent.penisGirth.toString();
},
},
},
}));
module.exports = [schemaExtender];