From 36d1a24484e9fc15801dc2d460a7513e123e3028 Mon Sep 17 00:00:00 2001 From: Brendan Chen Date: Thu, 23 Jan 2025 03:26:59 -0800 Subject: [PATCH] update resolvers for updated schema --- src/resolvers.ts | 135 +++++++++++++++-------------------------------- 1 file changed, 43 insertions(+), 92 deletions(-) diff --git a/src/resolvers.ts b/src/resolvers.ts index 29cf156..24718ac 100644 --- a/src/resolvers.ts +++ b/src/resolvers.ts @@ -1,4 +1,4 @@ -import { Coordinates, Eta, OrderedStop, Resolvers, Route, Shuttle, Stop, System } from "./generated/graphql"; +import { Coordinates, Eta, OrderedStop, Resolvers } from "./generated/graphql"; import { ServerContext } from "./ServerContext"; export const resolvers: Resolvers = { @@ -26,18 +26,7 @@ export const resolvers: Resolvers = { }, System: { routes: async (parent, args, contextValue, info) => { - const routes = await contextValue.repository.getRoutesBySystemId(parent.id); - return routes.map(({ - color, - id, - name, - polylineCoordinates, - }) => ({ - color, - id, - name, - polylineCoordinates, - })); + return await contextValue.repository.getRoutesBySystemId(parent.id); }, stops: async (parent, args, contextValue, info) => { const stops = await contextValue.repository.getStopsBySystemId(parent.id); @@ -80,11 +69,7 @@ export const resolvers: Resolvers = { const shuttle = await contextValue.repository.getShuttleById(args.id); if (shuttle === null) return null; - return { - coordinates: shuttle.coordinates as Coordinates, - id: shuttle.id, - name: shuttle.name, - }; + return shuttle; }, shuttles: async (parent, args, contextValue, info) => { const shuttles = await contextValue.repository.getShuttlesBySystemId(parent.id); @@ -93,6 +78,7 @@ export const resolvers: Resolvers = { coordinates: shuttle.coordinates, name: shuttle.name, id: shuttle.id, + routeId: shuttle.routeId, })); } }, @@ -108,6 +94,7 @@ export const resolvers: Resolvers = { coordinates: coordinates as Coordinates, name, route: parent, + routeId: parent.id, id, })); }, @@ -120,11 +107,8 @@ export const resolvers: Resolvers = { if (!stop) return null; return { - stop: { - id: stop.id, - name: stop.name, - coordinates: stop.coordinates as Coordinates, - }, + stopId: args.forStopId, + routeId: parent.id, route: parent, } }, @@ -135,16 +119,10 @@ export const resolvers: Resolvers = { const etaForStopId = await contextValue.repository.getEtaForShuttleAndStopId(parent.id, args.forStopId); if (etaForStopId === null) return null; - const stop = await contextValue.repository.getStopById(etaForStopId.stopId); - if (stop === null) return null; - return { - stop: { - coordinates: stop.coordinates, - id: stop.id, - name: stop.name, - }, + stopId: args.forStopId, secondsRemaining: etaForStopId.secondsRemaining, + shuttleId: parent.id, shuttle: parent, }; }, @@ -156,17 +134,11 @@ export const resolvers: Resolvers = { secondsRemaining, stopId, }): Promise => { - const stop = await contextValue.repository.getStopById(stopId); - if (stop === null) return null; - return { secondsRemaining, - stop: { - coordinates: stop.coordinates, - id: stop.id, - name: stop.name, - }, + stopId, shuttle: parent, + shuttleId: parent.id, } })); @@ -177,10 +149,7 @@ export const resolvers: Resolvers = { return []; }, route: async (parent, args, contextValue, info) => { - const shuttle = await contextValue.repository.getShuttleById(parent.id); - if (shuttle === null) return null; - - const route = await contextValue.repository.getRouteById(shuttle?.routeId); + const route = await contextValue.repository.getRouteById(parent.routeId); if (route === null) return null; return { @@ -193,42 +162,16 @@ export const resolvers: Resolvers = { }, Stop: { orderedStops: async (parent, args, contextValue, info) => { - const orderedStops = await contextValue.repository.getOrderedStopsByStopId(parent.id); - - const computedOrderedStops = await Promise.all(orderedStops.map(async ({ - routeId, - stopId, - }): Promise => { - const stop = await contextValue.repository.getStopById(stopId); - const route = await contextValue.repository.getRouteById(routeId); - if (stop === null || route === null) return null; - - return { - stop: { - coordinates: stop.coordinates, - id: stop.id, - name: stop.name, - }, - route: { - name: route.name, - id: route.id, - polylineCoordinates: route.polylineCoordinates, - color: route.color, - }, - }; - })); - - if (computedOrderedStops.every((value) => value !== null)) { - return computedOrderedStops as OrderedStop[]; - } - - return []; - } + return await contextValue.repository.getOrderedStopsByStopId(parent.id); + }, + etas: async (parent, args, contextValue, info) => { + return await contextValue.repository.getEtasForStopId(parent.id); + }, }, OrderedStop: { nextStop: async (parent, args, contextValue, info): Promise => { - const routeId = parent.route.id; - const stopId = parent.stop.id; + const routeId = parent.routeId; + const stopId = parent.stopId; const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId); if (!currentOrderedStop) return null; @@ -236,21 +179,18 @@ export const resolvers: Resolvers = { const nextOrderedStop = currentOrderedStop.nextStop; if (!nextOrderedStop) return null; - const nextStopObject = await contextValue.repository.getStopById(nextOrderedStop.stopId); - if (!nextStopObject) return null; + const nextOrderedStopObject = await contextValue.repository.getStopById(nextOrderedStop.stopId); + if (!nextOrderedStopObject) return null; return { route: parent.route, - stop: { - coordinates: nextStopObject.coordinates as Coordinates, - id: nextStopObject.id, - name: nextStopObject.name, - } + routeId: parent.routeId, + stopId: nextOrderedStopObject.id, } }, previousStop: async (parent, args, contextValue, info): Promise => { - const routeId = parent.route.id; - const stopId = parent.stop.id; + const routeId = parent.routeId; + const stopId = parent.stopId; const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId); if (!currentOrderedStop) return null; @@ -258,17 +198,28 @@ export const resolvers: Resolvers = { const previousOrderedStop = currentOrderedStop.previousStop; if (!previousOrderedStop) return null; - const nextStopObject = await contextValue.repository.getStopById(previousOrderedStop.stopId); - if (!nextStopObject) return null; + const previousOrderedStopObject = await contextValue.repository.getStopById(previousOrderedStop.stopId); + if (!previousOrderedStopObject) return null; return { route: parent.route, - stop: { - coordinates: nextStopObject.coordinates as Coordinates, - id: nextStopObject.id, - name: nextStopObject.name, - } + routeId: parent.routeId, + stopId: previousOrderedStopObject.id, } }, + stop: async (parent, args, contextValue, info) => { + return await contextValue.repository.getStopById(parent.stopId); + }, + route: async (parent, args, contextValue, info) => { + return await contextValue.repository.getRouteById(parent.routeId); + }, + }, + ETA: { + stop: async (parent, args, contextValue, info) => { + return await contextValue.repository.getStopById(parent.stopId); + }, + shuttle: async (parent, args, contextValue, info) => { + return await contextValue.repository.getShuttleById(parent.shuttleId); + }, }, }; \ No newline at end of file