From cbeb894bba1e93464a8316805c080227eaa8de48 Mon Sep 17 00:00:00 2001 From: antoinecellier Date: Mon, 19 Dec 2016 17:06:59 +0100 Subject: [PATCH 1/3] add batch for station routes --- server/graphql/types/station.js | 33 ++++++++++++++++++++++++--------- server/package.json | 4 ++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/server/graphql/types/station.js b/server/graphql/types/station.js index 491152a..48bbc9f 100644 --- a/server/graphql/types/station.js +++ b/server/graphql/types/station.js @@ -1,3 +1,4 @@ +import DataLoader from 'dataloader' import db, {aql} from '../../db' import Route from './route' @@ -28,10 +29,18 @@ export const resolvers = { return stop_lon }, routes ({ stop_id }, {from, length}) { - return db().query(aql` + return RoutesByStationIdLoader.load(stop_id) + } + } +} + +const getRoutesByStationIds = (stopIds) => { + return new Promise((resolve, reject) => { + db().query(aql` + for stopId in ${stopIds} let stops = ( for stop in stops - filter stop.parent_station == ${stop_id} + filter stop.parent_station == stopId return stop.stop_id ) @@ -47,15 +56,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 0, 3 + 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", From aa1245830c6d60b5e9fb2aa08ba93a8ed48edd2c Mon Sep 17 00:00:00 2001 From: antoinecellier Date: Mon, 19 Dec 2016 17:26:48 +0100 Subject: [PATCH 2/3] fix pagination --- server/graphql/types/station.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/server/graphql/types/station.js b/server/graphql/types/station.js index 48bbc9f..9108383 100644 --- a/server/graphql/types/station.js +++ b/server/graphql/types/station.js @@ -1,4 +1,5 @@ import DataLoader from 'dataloader' +import { map } from 'lodash' import db, {aql} from '../../db' import Route from './route' @@ -29,15 +30,20 @@ export const resolvers = { return stop_lon }, routes ({ stop_id }, {from, length}) { - return RoutesByStationIdLoader.load(stop_id) + 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 == stopId @@ -59,7 +65,7 @@ const getRoutesByStationIds = (stopIds) => { let routes = ( for route in routes filter route.route_id in routes_of_stops - limit 0, 3 + limit ${from}, ${length} return route ) From 4e39bd2dc3204af51558f00ee7ea0bc8f0731f94 Mon Sep 17 00:00:00 2001 From: antoinecellier Date: Mon, 19 Dec 2016 17:27:18 +0100 Subject: [PATCH 3/3] fix space --- server/graphql/types/station.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/graphql/types/station.js b/server/graphql/types/station.js index 9108383..eb4c7ae 100644 --- a/server/graphql/types/station.js +++ b/server/graphql/types/station.js @@ -29,8 +29,8 @@ export const resolvers = { longitude ({ stop_lon }) { return stop_lon }, - routes ({ stop_id }, {from, length}) { - return RoutesByStationIdLoader.load({stop_id, from, length}) + routes ({ stop_id }, { from, length }) { + return RoutesByStationIdLoader.load({ stop_id, from, length }) } } } @@ -38,7 +38,7 @@ export const resolvers = { 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}