Replaced default height and weight fields with fields taking units argument.
This commit is contained in:
48
src/web/plugins/actors.js
Normal file
48
src/web/plugins/actors.js
Normal file
@@ -0,0 +1,48 @@
|
||||
'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: ["birthdate"])
|
||||
height(units:Units): String @requires(columns: ["height"])
|
||||
weight(units:Units): String @requires(columns: ["weight"])
|
||||
}
|
||||
`,
|
||||
resolvers: {
|
||||
Actor: {
|
||||
age(parent, _args, _context, _info) {
|
||||
if (!parent.birthdate) return null;
|
||||
|
||||
return moment().diff(parent.birthdate, '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];
|
||||
7
src/web/plugins/plugins.js
Normal file
7
src/web/plugins/plugins.js
Normal file
@@ -0,0 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const ActorPlugins = require('./actors');
|
||||
|
||||
module.exports = {
|
||||
ActorPlugins,
|
||||
};
|
||||
@@ -10,6 +10,8 @@ const bodyParser = require('body-parser');
|
||||
const ConnectionFilterPlugin = require('postgraphile-plugin-connection-filter');
|
||||
const PgSimplifyInflectorPlugin = require('@graphile-contrib/pg-simplify-inflector');
|
||||
|
||||
const { ActorPlugins } = require('./plugins/plugins');
|
||||
|
||||
const {
|
||||
fetchReleases,
|
||||
fetchReleaseById,
|
||||
@@ -47,7 +49,10 @@ function initServer() {
|
||||
graphileBuildOptions: {
|
||||
pgOmitListSuffix: true,
|
||||
},
|
||||
appendPlugins: [PgSimplifyInflectorPlugin, ConnectionFilterPlugin],
|
||||
appendPlugins: [
|
||||
PgSimplifyInflectorPlugin, ConnectionFilterPlugin,
|
||||
...ActorPlugins,
|
||||
],
|
||||
},
|
||||
));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user