diff --git a/server/graphql/types/station.js b/server/graphql/types/station.js index 491152a..eb4c7ae 100644 --- a/server/graphql/types/station.js +++ b/server/graphql/types/station.js @@ -1,3 +1,5 @@ +import DataLoader from 'dataloader' +import { map } from 'lodash' import db, {aql} from '../../db' import Route from './route' @@ -27,11 +29,24 @@ export const resolvers = { longitude ({ stop_lon }) { return stop_lon }, - routes ({ stop_id }, {from, length}) { - return db().query(aql` + routes ({ stop_id }, { from, length }) { + return RoutesByStationIdLoader.load({ stop_id, from, length }) + } + } +} + +const getRoutesByStationIds = (stopIds) => { + const { from, length } = stopIds[0] + stopIds = map(stopIds, 'stop_id') + + return new Promise((resolve, reject) => { + db().query(aql` + for stopId in ${stopIds} + let from = stopId.from + let stops = ( for stop in stops - filter stop.parent_station == ${stop_id} + filter stop.parent_station == stopId return stop.stop_id ) @@ -47,15 +62,21 @@ export const resolvers = { return trip.route_id ) - for route in routes - filter route.route_id in routes_of_stops - limit ${from}, ${length} - return route - `).then(cursor => cursor.all()) - } - } + let routes = ( + for route in routes + filter route.route_id in routes_of_stops + limit ${from}, ${length} + return route + ) + + return routes + `).then(cursor => cursor.all()) + .then(routes => resolve(routes)) + }); } +const RoutesByStationIdLoader = new DataLoader(getRoutesByStationIds); + export function stationDbId (stationDtoId) { return 'StopArea:' + stationDtoId } diff --git a/server/package.json b/server/package.json index 4dae3e2..861bb03 100644 --- a/server/package.json +++ b/server/package.json @@ -14,12 +14,12 @@ "co": "^4.6.0", "co-express": "^1.2.2", "cors": "^2.8.1", + "dataloader": "^1.2.0", "express": "git+https://github.com/expressjs/express.git#1dbfee66235c6a4e2f8e440264f71f31ddbe75d4", + "express-graphql": "^0.5.4", "falcor-express": "^0.1.2", "falcor-router": "^0.4.0", - "express-graphql": "^0.5.4", "graphql": "^0.7.0", - "cors": "^2.8.1", "http2": "^3.3.4", "joi": "^9.0.4", "lodash.flatten": "^4.4.0",