UNPKG

nextbus-graphql

Version:

A GraphQL interface to the NextBus XML Feed

105 lines (86 loc) 3.41 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNode = exports.getAll = exports.get = exports.type = undefined; let get = exports.get = (() => { var _ref = _asyncToGenerator(function* (tag, context) { return (0, _withType2.default)(type.name, (yield context.nextbus.getAgency(tag))); }); return function get(_x, _x2) { return _ref.apply(this, arguments); }; })(); let getAll = exports.getAll = (() => { var _ref2 = _asyncToGenerator(function* (context) { return (yield context.nextbus.getAgencies()).map((0, _withType2.default)(type.name)); }); return function getAll(_x3) { return _ref2.apply(this, arguments); }; })(); let getNode = exports.getNode = (() => { var _ref3 = _asyncToGenerator(function* (id, context) { return get(id, context); }); return function getNode(_x4, _x5) { return _ref3.apply(this, arguments); }; })(); 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); var _ = require('.'); 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 type = exports.type = new graphql.GraphQLObjectType({ name: 'Agency', fields: () => ({ id: relay.globalIdField('Agency', agency => agency.tag), regionTitle: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) }, shortTitle: { type: graphql.GraphQLString }, tag: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) }, title: { type: new graphql.GraphQLNonNull(graphql.GraphQLString) }, routes: (() => { const { connectionType } = relay.connectionDefinitions({ name: _.route.type.name, nodeType: _.route.type }); return { type: connectionType, args: relay.connectionArgs, resolve(agency, args, context) { return _asyncToGenerator(function* () { const routes = yield context.nextbus.getRoutes(agency.tag); return relay.connectionFromArray(routes, args); })(); } }; })(), route: { type: _.route.type, args: { tag: { type: graphql.GraphQLString } }, resolve(agency, { tag }, context) { return _asyncToGenerator(function* () { return _.route.get(agency.tag, tag, context); })(); } } }), interfaces: () => [_relayNode.nodeInterface] });