mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
update resolvers for updated schema
This commit is contained in:
135
src/resolvers.ts
135
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<ServerContext> = {
|
||||
@@ -26,18 +26,7 @@ export const resolvers: Resolvers<ServerContext> = {
|
||||
},
|
||||
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<ServerContext> = {
|
||||
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<ServerContext> = {
|
||||
coordinates: shuttle.coordinates,
|
||||
name: shuttle.name,
|
||||
id: shuttle.id,
|
||||
routeId: shuttle.routeId,
|
||||
}));
|
||||
}
|
||||
},
|
||||
@@ -108,6 +94,7 @@ export const resolvers: Resolvers<ServerContext> = {
|
||||
coordinates: coordinates as Coordinates,
|
||||
name,
|
||||
route: parent,
|
||||
routeId: parent.id,
|
||||
id,
|
||||
}));
|
||||
},
|
||||
@@ -120,11 +107,8 @@ export const resolvers: Resolvers<ServerContext> = {
|
||||
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<ServerContext> = {
|
||||
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<ServerContext> = {
|
||||
secondsRemaining,
|
||||
stopId,
|
||||
}): Promise<Eta | null> => {
|
||||
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<ServerContext> = {
|
||||
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<ServerContext> = {
|
||||
},
|
||||
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 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<OrderedStop | null> => {
|
||||
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<ServerContext> = {
|
||||
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<OrderedStop | null> => {
|
||||
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<ServerContext> = {
|
||||
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);
|
||||
},
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user