UNPKG

nextbus-graphql

Version:

A GraphQL interface to the NextBus XML Feed

109 lines (89 loc) 4.04 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.get = exports.getAll = exports.type = undefined; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; let getAll = exports.getAll = (() => { var _ref = _asyncToGenerator(function* (agencyTag, routeTag, context) { return (yield context.nextbus.getRoute(agencyTag, routeTag)).stops.map(function (stop) { return (0, _withType2.default)(type.name, _extends({}, stop, { agencyTag, routeTag })); }); }); return function getAll(_x, _x2, _x3) { return _ref.apply(this, arguments); }; })(); let get = exports.get = (() => { var _ref2 = _asyncToGenerator(function* (agencyTag, routeTag, stopTag, context) { const stop = (yield getAll(agencyTag, routeTag, context)).find(function (s) { return s.tag === stopTag; }); if (stop) { return stop; } throw new Error(`No stop for agency tag "${ agencyTag }", route tag "${ routeTag }", stop tag "${ stopTag }"`); }); return function get(_x4, _x5, _x6, _x7) { return _ref2.apply(this, arguments); }; })(); exports.getNode = getNode; var _graphql = require('graphql'); var graphql = _interopRequireWildcard(_graphql); var _graphqlRelay = require('graphql-relay'); var relay = _interopRequireWildcard(_graphqlRelay); var _relayNode = require('../relayNode'); var _withType = require('../withType'); var _withType2 = _interopRequireDefault(_withType); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } const encodeID = ([agencyTag, routeTag, stopTag]) => `${ agencyTag }:${ routeTag }:${ stopTag }`; const decodeID = id => id.split(':'); const type = exports.type = new graphql.GraphQLObjectType({ name: 'Stop', description: 'A stop along a route', fields: () => ({ id: relay.globalIdField('Stop', stop => encodeID([stop.agencyTag, stop.routeTag, stop.tag])), agencyTag: { type: new graphql.GraphQLNonNull(graphql.GraphQLString), description: 'The tag of the agency to which this stop belongs' }, lat: { type: new graphql.GraphQLNonNull(graphql.GraphQLString), resolve(stop, args, context) { return _asyncToGenerator(function* () { return stop.lat || (yield get(stop.agencyTag, stop.routeTag, stop.tag, context)).lat; })(); } }, lon: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) }, routeTag: { type: new graphql.GraphQLNonNull(graphql.GraphQLString), description: 'The tag of the route to which this stop belongs' }, shortTitle: { type: graphql.GraphQLString }, stopId: { type: graphql.GraphQLString }, tag: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) }, title: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) } }), interfaces: () => [_relayNode.nodeInterface] }); function getNode(id, context, info) { const [agencyTag, routeTag, stopTag] = decodeID(id); return get(agencyTag, routeTag, stopTag, context, info); }