update resolvers for updated schema

This commit is contained in:
2025-01-23 03:26:59 -08:00
parent 5fe66c1517
commit 36d1a24484

View File

@@ -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"; import { ServerContext } from "./ServerContext";
export const resolvers: Resolvers<ServerContext> = { export const resolvers: Resolvers<ServerContext> = {
@@ -26,18 +26,7 @@ export const resolvers: Resolvers<ServerContext> = {
}, },
System: { System: {
routes: async (parent, args, contextValue, info) => { routes: async (parent, args, contextValue, info) => {
const routes = await contextValue.repository.getRoutesBySystemId(parent.id); return await contextValue.repository.getRoutesBySystemId(parent.id);
return routes.map(({
color,
id,
name,
polylineCoordinates,
}) => ({
color,
id,
name,
polylineCoordinates,
}));
}, },
stops: async (parent, args, contextValue, info) => { stops: async (parent, args, contextValue, info) => {
const stops = await contextValue.repository.getStopsBySystemId(parent.id); const stops = await contextValue.repository.getStopsBySystemId(parent.id);
@@ -80,11 +69,7 @@ export const resolvers: Resolvers<ServerContext> = {
const shuttle = await contextValue.repository.getShuttleById(args.id); const shuttle = await contextValue.repository.getShuttleById(args.id);
if (shuttle === null) return null; if (shuttle === null) return null;
return { return shuttle;
coordinates: shuttle.coordinates as Coordinates,
id: shuttle.id,
name: shuttle.name,
};
}, },
shuttles: async (parent, args, contextValue, info) => { shuttles: async (parent, args, contextValue, info) => {
const shuttles = await contextValue.repository.getShuttlesBySystemId(parent.id); const shuttles = await contextValue.repository.getShuttlesBySystemId(parent.id);
@@ -93,6 +78,7 @@ export const resolvers: Resolvers<ServerContext> = {
coordinates: shuttle.coordinates, coordinates: shuttle.coordinates,
name: shuttle.name, name: shuttle.name,
id: shuttle.id, id: shuttle.id,
routeId: shuttle.routeId,
})); }));
} }
}, },
@@ -108,6 +94,7 @@ export const resolvers: Resolvers<ServerContext> = {
coordinates: coordinates as Coordinates, coordinates: coordinates as Coordinates,
name, name,
route: parent, route: parent,
routeId: parent.id,
id, id,
})); }));
}, },
@@ -120,11 +107,8 @@ export const resolvers: Resolvers<ServerContext> = {
if (!stop) return null; if (!stop) return null;
return { return {
stop: { stopId: args.forStopId,
id: stop.id, routeId: parent.id,
name: stop.name,
coordinates: stop.coordinates as Coordinates,
},
route: parent, route: parent,
} }
}, },
@@ -135,16 +119,10 @@ export const resolvers: Resolvers<ServerContext> = {
const etaForStopId = await contextValue.repository.getEtaForShuttleAndStopId(parent.id, args.forStopId); const etaForStopId = await contextValue.repository.getEtaForShuttleAndStopId(parent.id, args.forStopId);
if (etaForStopId === null) return null; if (etaForStopId === null) return null;
const stop = await contextValue.repository.getStopById(etaForStopId.stopId);
if (stop === null) return null;
return { return {
stop: { stopId: args.forStopId,
coordinates: stop.coordinates,
id: stop.id,
name: stop.name,
},
secondsRemaining: etaForStopId.secondsRemaining, secondsRemaining: etaForStopId.secondsRemaining,
shuttleId: parent.id,
shuttle: parent, shuttle: parent,
}; };
}, },
@@ -156,17 +134,11 @@ export const resolvers: Resolvers<ServerContext> = {
secondsRemaining, secondsRemaining,
stopId, stopId,
}): Promise<Eta | null> => { }): Promise<Eta | null> => {
const stop = await contextValue.repository.getStopById(stopId);
if (stop === null) return null;
return { return {
secondsRemaining, secondsRemaining,
stop: { stopId,
coordinates: stop.coordinates,
id: stop.id,
name: stop.name,
},
shuttle: parent, shuttle: parent,
shuttleId: parent.id,
} }
})); }));
@@ -177,10 +149,7 @@ export const resolvers: Resolvers<ServerContext> = {
return []; return [];
}, },
route: async (parent, args, contextValue, info) => { route: async (parent, args, contextValue, info) => {
const shuttle = await contextValue.repository.getShuttleById(parent.id); const route = await contextValue.repository.getRouteById(parent.routeId);
if (shuttle === null) return null;
const route = await contextValue.repository.getRouteById(shuttle?.routeId);
if (route === null) return null; if (route === null) return null;
return { return {
@@ -193,42 +162,16 @@ export const resolvers: Resolvers<ServerContext> = {
}, },
Stop: { Stop: {
orderedStops: async (parent, args, contextValue, info) => { orderedStops: async (parent, args, contextValue, info) => {
const orderedStops = await contextValue.repository.getOrderedStopsByStopId(parent.id); return await contextValue.repository.getOrderedStopsByStopId(parent.id);
},
const computedOrderedStops = await Promise.all(orderedStops.map(async ({ etas: async (parent, args, contextValue, info) => {
routeId, return await contextValue.repository.getEtasForStopId(parent.id);
stopId, },
}): Promise<OrderedStop | null> => {
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 [];
}
}, },
OrderedStop: { OrderedStop: {
nextStop: async (parent, args, contextValue, info): Promise<OrderedStop | null> => { nextStop: async (parent, args, contextValue, info): Promise<OrderedStop | null> => {
const routeId = parent.route.id; const routeId = parent.routeId;
const stopId = parent.stop.id; const stopId = parent.stopId;
const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId); const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId);
if (!currentOrderedStop) return null; if (!currentOrderedStop) return null;
@@ -236,21 +179,18 @@ export const resolvers: Resolvers<ServerContext> = {
const nextOrderedStop = currentOrderedStop.nextStop; const nextOrderedStop = currentOrderedStop.nextStop;
if (!nextOrderedStop) return null; if (!nextOrderedStop) return null;
const nextStopObject = await contextValue.repository.getStopById(nextOrderedStop.stopId); const nextOrderedStopObject = await contextValue.repository.getStopById(nextOrderedStop.stopId);
if (!nextStopObject) return null; if (!nextOrderedStopObject) return null;
return { return {
route: parent.route, route: parent.route,
stop: { routeId: parent.routeId,
coordinates: nextStopObject.coordinates as Coordinates, stopId: nextOrderedStopObject.id,
id: nextStopObject.id,
name: nextStopObject.name,
}
} }
}, },
previousStop: async (parent, args, contextValue, info): Promise<OrderedStop | null> => { previousStop: async (parent, args, contextValue, info): Promise<OrderedStop | null> => {
const routeId = parent.route.id; const routeId = parent.routeId;
const stopId = parent.stop.id; const stopId = parent.stopId;
const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId); const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId);
if (!currentOrderedStop) return null; if (!currentOrderedStop) return null;
@@ -258,17 +198,28 @@ export const resolvers: Resolvers<ServerContext> = {
const previousOrderedStop = currentOrderedStop.previousStop; const previousOrderedStop = currentOrderedStop.previousStop;
if (!previousOrderedStop) return null; if (!previousOrderedStop) return null;
const nextStopObject = await contextValue.repository.getStopById(previousOrderedStop.stopId); const previousOrderedStopObject = await contextValue.repository.getStopById(previousOrderedStop.stopId);
if (!nextStopObject) return null; if (!previousOrderedStopObject) return null;
return { return {
route: parent.route, route: parent.route,
stop: { routeId: parent.routeId,
coordinates: nextStopObject.coordinates as Coordinates, stopId: previousOrderedStopObject.id,
id: nextStopObject.id,
name: nextStopObject.name,
}
} }
}, },
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);
},
}, },
}; };