nextbus-graphql
Version:
A GraphQL interface to the NextBus XML Feed
141 lines (123 loc) • 5.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.get = exports.type = undefined;
let get = exports.get = (() => {
var _ref = _asyncToGenerator(function* (agencyTag, routeTag, context) {
return (0, _withType2.default)(type.name, (yield context.nextbus.getRoute(agencyTag, routeTag)));
});
return function get(_x, _x2, _x3) {
return _ref.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);
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 encodeID = ([agencyTag, routeTag]) => `${ agencyTag }:${ routeTag }`;
const decodeID = id => id.split(':');
const type = exports.type = new graphql.GraphQLObjectType({
name: 'Route',
description: 'A route for an agency',
fields: () => ({
id: relay.globalIdField('Route', route => encodeID([route.agencyTag, route.tag])),
// Need agency tag here so id above is globally unique: route tags by themselves are not unique
// since multiple agencies can have identical route tags.
agencyTag: {
type: new graphql.GraphQLNonNull(graphql.GraphQLString),
description: 'The tag of the agency to which this route belongs'
},
color: {
type: new graphql.GraphQLNonNull(graphql.GraphQLString),
resolve(route, args, context) {
return _asyncToGenerator(function* () {
return (yield get(route.agencyTag, route.tag, context)).color;
})();
}
},
latMax: {
type: new graphql.GraphQLNonNull(graphql.GraphQLFloat),
resolve(route, args, context) {
return _asyncToGenerator(function* () {
return (yield get(route.agencyTag, route.tag, context)).latMax;
})();
}
},
latMin: {
type: new graphql.GraphQLNonNull(graphql.GraphQLFloat),
resolve(route, args, context) {
return _asyncToGenerator(function* () {
return (yield get(route.agencyTag, route.tag, context)).latMin;
})();
}
},
lonMax: {
type: new graphql.GraphQLNonNull(graphql.GraphQLFloat),
resolve(route, args, context) {
return _asyncToGenerator(function* () {
return (yield get(route.agencyTag, route.tag, context)).lonMax;
})();
}
},
lonMin: {
type: new graphql.GraphQLNonNull(graphql.GraphQLFloat),
resolve(route, args, context) {
return _asyncToGenerator(function* () {
return (yield get(route.agencyTag, route.tag, context)).lonMin;
})();
}
},
oppositeColor: {
type: new graphql.GraphQLNonNull(graphql.GraphQLString),
resolve(route, args, context) {
return _asyncToGenerator(function* () {
return (yield get(route.agencyTag, route.tag, context)).oppositeColor;
})();
}
},
shortTitle: {
type: graphql.GraphQLString,
resolve(route, args, context) {
return _asyncToGenerator(function* () {
return (yield get(route.agencyTag, route.tag, context)).shortTitle || null;
})();
}
},
stops: (() => {
const { connectionType } = relay.connectionDefinitions({
name: _.stop.type.name,
nodeType: _.stop.type
});
return {
type: connectionType,
args: relay.connectionArgs,
resolve(route, args, context) {
return _asyncToGenerator(function* () {
const stops = yield _.stop.getAll(route.agencyTag, route.tag, context);
return relay.connectionFromArray(stops, args);
})();
}
};
})(),
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] = decodeID(id);
return get(agencyTag, routeTag, context, info);
}