add more custom resolvers

This commit is contained in:
2024-12-22 19:36:19 -08:00
parent 42f2afa03a
commit bf967c8cb8

View File

@@ -1,54 +1,95 @@
import { Eta, OrderedStop, Resolvers, Route, Shuttle, Stop, System } from "./generated/graphql"; import { Coordinates, Eta, OrderedStop, Resolvers, Route, Shuttle, Stop, System } from "./generated/graphql";
import { ServerContext } from "./serverContext"; import { ServerContext } from "./serverContext";
export const resolvers: Resolvers<ServerContext> = { export const resolvers: Resolvers<ServerContext> = {
Query: { Query: {
systems: (parent, args, contextValue, info) => { systems: async (parent, args, contextValue, info) => {
return contextValue.sharedMemory.systems; const systems = await contextValue.repository.getSystems();
return systems.map(({
name,
id
}) => ({
name,
id
}));
}, },
system: (parent, args, contextValue, info) => { system: async (parent, args, contextValue, info) => {
const systems = contextValue.sharedMemory.systems; if (!args.id) return null;
const system = systems.find((system) => system.id === args.id); const system = await contextValue.repository.getSystemById(args.id);
if (!system) { if (system === null) return null;
return null;
}
return system; return {
name: system.name,
id: system.id,
};
} }
}, },
System: { System: {
stop: (parent, args, contextValue, info) => { routes: async (parent, args, contextValue, info) => {
// TODO implement interface with ID and one function to perform search const routes = await contextValue.repository.getRoutesBySystemId(parent.id);
const stops = parent.stops; return routes.map(({
const stop = stops.find((stop) => stop.id === args.id); color,
if (!stop) { id,
return null; name,
} polylineCoordinates,
}) => ({
return stop; color,
id,
name,
polylineCoordinates,
}));
}, },
route: (parent, args, contextValue, info) => { stops: async (parent, args, contextValue, info) => {
const routes = parent.routes const stops = await contextValue.repository.getStopsBySystemId(parent.id);
const route = routes.find((route) => route.id === args.id); return stops.map(({
if (!route) { id,
return null; name,
} coordinates
}) => ({
return route; id,
name,
// Both ICoordinates and Coordinates have the same definition
coordinates: coordinates as Coordinates,
}));
}, },
shuttle: (parent, args, contextValue, info) => { stop: async (parent, args, contextValue, info) => {
const shuttles = parent.shuttles; if (!args.id) return null;
const shuttle = shuttles.find((shuttle) => shuttle.id === args.id); const stop = await contextValue.repository.getStopById(args.id);
if (!shuttle) { if (stop === null) return null;
return null;
}
return shuttle; return {
id: stop.id,
name: stop.name,
coordinates: stop.coordinates as Coordinates,
};
},
route: async (parent, args, contextValue, info) => {
if (!args.id) return null;
const route = await contextValue.repository.getRouteById(args.id);
if (route === null) return null;
return {
color: route.color,
id: route.id,
name: route.name,
polylineCoordinates: route.polylineCoordinates as Coordinates[],
};
},
shuttle: async (parent, args, contextValue, info) => {
if (!args.id) return null;
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,
};
} }
}, },
Route: { Route: {
nextOrderedStop: (parent, args, contextValue, info) => { nextOrderedStop: (parent, args, contextValue, info) => {
const orderedStop = parent.orderedStops.find((orderedStop) => orderedStop.stop.id === args.forStopId); const orderedStop = parent.orderedStops?.find((orderedStop) => orderedStop.stop?.id === args.forStopId);
if (!orderedStop || !orderedStop.nextStop) { if (!orderedStop || !orderedStop.nextStop) {
return null; return null;
} }
@@ -58,7 +99,7 @@ export const resolvers: Resolvers<ServerContext> = {
}, },
Shuttle: { Shuttle: {
eta: (parent, args, contextValue, info) => { eta: (parent, args, contextValue, info) => {
const etaForNextStop = parent.etas.find((eta) => eta.stop.id === args.forStopId); const etaForNextStop = parent.etas?.find((eta) => eta.stop.id === args.forStopId);
if (!etaForNextStop) { if (!etaForNextStop) {
return null; return null;
} }
@@ -66,4 +107,59 @@ export const resolvers: Resolvers<ServerContext> = {
return etaForNextStop; return etaForNextStop;
}, },
}, },
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<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 [];
}
return computedOrderedStops as OrderedStop[];
}
},
OrderedStop: {
nextStop: async (parent, args, contextValue, info): Promise<OrderedStop | null> => {
const routeId = parent.route.id;
const stopId = parent.stop.id;
const nextOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId);
if (!nextOrderedStop) return null;
const nextStopObject = await contextValue.repository.getStopById(nextOrderedStop.stopId);
if (!nextStopObject) return null;
return {
route: parent.route,
stop: {
coordinates: nextStopObject.coordinates as Coordinates,
id: nextStopObject.id,
name: nextStopObject.name,
}
}
},
}
}; };