UNPKG

foobot-graphql

Version:

GraphQL server for Foobot device data.

217 lines (198 loc) 5.91 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _graphql = require('graphql'); var _graphqlDate = require('graphql-date'); var _graphqlDate2 = _interopRequireDefault(_graphqlDate); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var resolveToDate = function resolveToDate(source, args, context, info) { var value = source[info.fieldName]; if (value != null) { return new Date(value * 1000); } return value; }; var resolveSensor = function resolveSensor(source, args, context, info) { var index = source.sensors.indexOf(info.fieldName); return { units: source.units[index], datapoints: source.datapoints.map(function (datapoint) { return { time: new Date(datapoint[0] * 1000), value: datapoint[index] }; }) }; }; var Datapoint = new _graphql.GraphQLObjectType({ name: 'Datapoint', description: 'A single datapoint from a sensor.', fields: { time: { type: _graphqlDate2.default, description: 'The timestamp of the reading.' }, value: { type: _graphql.GraphQLFloat, description: 'The numeric value of the sensor reading.' } } }); var Sensor = new _graphql.GraphQLObjectType({ name: 'Sensor', description: 'Information from a single sensor.', fields: { units: { type: _graphql.GraphQLString, description: 'The units for the value returned in each datapoint.' }, datapoints: { type: new _graphql.GraphQLList(Datapoint), description: 'The set of datapoints for the requested period.' } } }); var Sensors = new _graphql.GraphQLObjectType({ name: 'Sensors', fields: { start: { type: _graphqlDate2.default, description: 'The timestamp of the earliest returned datapoint.', resolve: resolveToDate }, end: { type: _graphqlDate2.default, description: 'The timestamp of the last returned datapoint.', resolve: resolveToDate }, expires: { type: _graphqlDate2.default, description: 'The timestamp at which we expect a new datapoint from the device. ' + 'Clients can use this to determine when their data is stale.', resolve: resolveToDate }, pm: { type: Sensor, description: 'Particulate matter.', resolve: resolveSensor }, tmp: { type: Sensor, description: 'Temperature.', resolve: resolveSensor }, hum: { type: Sensor, description: 'Humidity.', resolve: resolveSensor }, co2: { type: Sensor, description: 'Carbon dioxide.', resolve: resolveSensor }, voc: { type: Sensor, description: 'Volatile organic compounds.', resolve: resolveSensor }, allpollu: { type: Sensor, description: 'Overall pollution index.', resolve: resolveSensor } } }); var Device = new _graphql.GraphQLObjectType({ name: 'Device', description: 'Information about a single device.', fields: { uuid: { type: _graphql.GraphQLString, description: 'The UUID of the device.' }, name: { type: _graphql.GraphQLString, description: 'The friendly name of the device.', resolve: function resolve(_ref, args, _ref2, info) { var uuid = _ref.uuid; var loaders = _ref2.loaders; return loaders.device.load(uuid).then(function (device) { return device.name; }); } }, mac: { type: _graphql.GraphQLString, description: 'The MAC of the device.', resolve: function resolve(_ref3, args, _ref4, info) { var uuid = _ref3.uuid; var loaders = _ref4.loaders; return loaders.device.load(uuid).then(function (device) { return device.mac; }); } }, userID: { type: _graphql.GraphQLInt, description: 'The ID of the user who owns the device.', resolve: function resolve(_ref5, args, _ref6, info) { var uuid = _ref5.uuid; var loaders = _ref6.loaders; return loaders.device.load(uuid).then(function (device) { return device.userId; }); } }, sensors: { type: Sensors, description: 'The set of sensors on a single device.', args: { period: { type: _graphql.GraphQLInt, defaultValue: 0, description: 'Number of seconds between start time of the period and now.' }, averageBy: { type: _graphql.GraphQLInt, defaultValue: 0, description: 'Resolution of the returned datapoints in seconds. Use 0 or 300 ' + 'for no averaging. For long range requests, it is recommended to ' + 'use 3600 (hourly average) or a multiple.' } }, resolve: function resolve(_ref7, _ref8, _ref9, info) { var uuid = _ref7.uuid; var period = _ref8.period, averageBy = _ref8.averageBy; var loaders = _ref9.loaders; return loaders.datapoints.load([uuid, period, averageBy]); } } } }); var schema = new _graphql.GraphQLSchema({ query: new _graphql.GraphQLObjectType({ name: 'Query', fields: { device: { type: Device, args: { uuid: { type: _graphql.GraphQLString, description: 'The UUID of the device. If none is supplied, the default will ' + 'be determined based on the server’s configuration.' } }, resolve: function resolve(source, args, context, info) { var uuid = args.uuid || context.client.defaultDevice; if (!uuid) { throw new Error('Supply a device UUID or configure a default.'); } return { uuid: uuid }; } } } }) }); exports.default = schema; if (require.main === module) { console.log((0, _graphql.printSchema)(schema)); }