traxxx/src/web/plugins/actors.js

55 lines
1.5 KiB
JavaScript

'use strict';
const { makeExtendSchemaPlugin, gql } = require('graphile-utils');
const moment = require('moment');
const { cmToFeetInches, kgToLbs } = require('../../utils/convert');
const schemaExtender = makeExtendSchemaPlugin(_build => ({
typeDefs: gql`
enum Units {
METRIC
IMPERIAL
}
extend type Actor {
age: Int @requires(columns: ["dateOfBirth"])
ageAtDeath: Int @requires(columns: ["dateOfBirth", "dateOfDeath"])
height(units:Units): String @requires(columns: ["height"])
weight(units:Units): String @requires(columns: ["weight"])
}
`,
resolvers: {
Actor: {
age(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();
},
},
},
}));
module.exports = [schemaExtender];