mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
move other resolvers into their own files
This commit is contained in:
@@ -1,205 +1,19 @@
|
|||||||
import { Coordinates, Eta, OrderedStop, Resolvers } from "./generated/graphql";
|
import { Coordinates, Eta, OrderedStop, Resolvers } from "./generated/graphql";
|
||||||
import { ServerContext } from "./ServerContext";
|
import { ServerContext } from "./ServerContext";
|
||||||
import { QueryResolvers } from "./resolvers/QueryResolvers";
|
import { QueryResolvers } from "./resolvers/QueryResolvers";
|
||||||
|
import { SystemResolvers } from "./resolvers/SystemResolvers";
|
||||||
|
import { EtaResolvers } from "./resolvers/EtaResolvers";
|
||||||
|
import { OrderedStopResolvers } from "./resolvers/OrderedStopResolvers";
|
||||||
|
import { StopResolvers } from "./resolvers/StopResolvers";
|
||||||
|
import { ShuttleResolvers } from "./resolvers/ShuttleResolvers";
|
||||||
|
import { RouteResolvers } from "./resolvers/RouteResolvers";
|
||||||
|
|
||||||
export const MergedResolvers: Resolvers<ServerContext> = {
|
export const MergedResolvers: Resolvers<ServerContext> = {
|
||||||
...QueryResolvers,
|
...QueryResolvers,
|
||||||
System: {
|
...SystemResolvers,
|
||||||
routes: async (parent, args, contextValue, info) => {
|
...RouteResolvers,
|
||||||
return await contextValue.repository.getRoutesBySystemId(parent.id);
|
...ShuttleResolvers,
|
||||||
},
|
...StopResolvers,
|
||||||
stops: async (parent, args, contextValue, info) => {
|
...OrderedStopResolvers,
|
||||||
const stops = await contextValue.repository.getStopsBySystemId(parent.id);
|
...EtaResolvers,
|
||||||
return stops.map(({
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
coordinates
|
|
||||||
}) => ({
|
|
||||||
id,
|
|
||||||
name,
|
|
||||||
// Both ICoordinates and Coordinates have the same definition
|
|
||||||
coordinates: coordinates as Coordinates,
|
|
||||||
}));
|
|
||||||
},
|
|
||||||
stop: async (parent, args, contextValue, info) => {
|
|
||||||
if (!args.id) return null;
|
|
||||||
const stop = await contextValue.repository.getStopById(args.id);
|
|
||||||
if (stop === null) return null;
|
|
||||||
|
|
||||||
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 shuttle;
|
|
||||||
},
|
|
||||||
shuttles: async (parent, args, contextValue, info) => {
|
|
||||||
const shuttles = await contextValue.repository.getShuttlesBySystemId(parent.id);
|
|
||||||
|
|
||||||
return shuttles.map(shuttle => ({
|
|
||||||
coordinates: shuttle.coordinates,
|
|
||||||
name: shuttle.name,
|
|
||||||
id: shuttle.id,
|
|
||||||
routeId: shuttle.routeId,
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Route: {
|
|
||||||
shuttles: async (parent, args, contextValue, info) => {
|
|
||||||
const shuttles = await contextValue.repository.getShuttlesByRouteId(parent.id);
|
|
||||||
|
|
||||||
return shuttles.map(({
|
|
||||||
coordinates,
|
|
||||||
name,
|
|
||||||
id,
|
|
||||||
}) => ({
|
|
||||||
coordinates: coordinates as Coordinates,
|
|
||||||
name,
|
|
||||||
route: parent,
|
|
||||||
routeId: parent.id,
|
|
||||||
id,
|
|
||||||
}));
|
|
||||||
},
|
|
||||||
orderedStop: async (parent, args, contextValue, info) => {
|
|
||||||
if (!args.forStopId) return null;
|
|
||||||
const orderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(parent.id, args.forStopId);
|
|
||||||
if (!orderedStop) return null;
|
|
||||||
|
|
||||||
const stop = await contextValue.repository.getStopById(orderedStop.stopId);
|
|
||||||
if (!stop) return null;
|
|
||||||
|
|
||||||
return {
|
|
||||||
stopId: args.forStopId,
|
|
||||||
routeId: parent.id,
|
|
||||||
route: parent,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Shuttle: {
|
|
||||||
eta: async (parent, args, contextValue, info) => {
|
|
||||||
if (!args.forStopId) return null;
|
|
||||||
const etaForStopId = await contextValue.repository.getEtaForShuttleAndStopId(parent.id, args.forStopId);
|
|
||||||
if (etaForStopId === null) return null;
|
|
||||||
|
|
||||||
return {
|
|
||||||
stopId: args.forStopId,
|
|
||||||
secondsRemaining: etaForStopId.secondsRemaining,
|
|
||||||
shuttleId: parent.id,
|
|
||||||
shuttle: parent,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
etas: async (parent, args, contextValue, info) => {
|
|
||||||
const etasForShuttle = await contextValue.repository.getEtasForShuttleId(parent.id);
|
|
||||||
if (!etasForShuttle) return null;
|
|
||||||
|
|
||||||
const computedEtas = await Promise.all(etasForShuttle.map(async ({
|
|
||||||
secondsRemaining,
|
|
||||||
stopId,
|
|
||||||
}): Promise<Eta | null> => {
|
|
||||||
return {
|
|
||||||
secondsRemaining,
|
|
||||||
stopId,
|
|
||||||
shuttle: parent,
|
|
||||||
shuttleId: parent.id,
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
if (computedEtas.every((eta) => eta !== null)) {
|
|
||||||
return computedEtas;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [];
|
|
||||||
},
|
|
||||||
route: async (parent, args, contextValue, info) => {
|
|
||||||
const route = await contextValue.repository.getRouteById(parent.routeId);
|
|
||||||
if (route === null) return null;
|
|
||||||
|
|
||||||
return {
|
|
||||||
color: route.color,
|
|
||||||
id: route.id,
|
|
||||||
name: route.name,
|
|
||||||
polylineCoordinates: route.polylineCoordinates,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Stop: {
|
|
||||||
orderedStops: async (parent, args, contextValue, info) => {
|
|
||||||
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.routeId;
|
|
||||||
const stopId = parent.stopId;
|
|
||||||
|
|
||||||
const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId);
|
|
||||||
if (!currentOrderedStop) return null;
|
|
||||||
|
|
||||||
const nextOrderedStop = currentOrderedStop.nextStop;
|
|
||||||
if (!nextOrderedStop) return null;
|
|
||||||
|
|
||||||
const nextOrderedStopObject = await contextValue.repository.getStopById(nextOrderedStop.stopId);
|
|
||||||
if (!nextOrderedStopObject) return null;
|
|
||||||
|
|
||||||
return {
|
|
||||||
route: parent.route,
|
|
||||||
routeId: parent.routeId,
|
|
||||||
stopId: nextOrderedStopObject.id,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
previousStop: async (parent, args, contextValue, info): Promise<OrderedStop | null> => {
|
|
||||||
const routeId = parent.routeId;
|
|
||||||
const stopId = parent.stopId;
|
|
||||||
|
|
||||||
const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId);
|
|
||||||
if (!currentOrderedStop) return null;
|
|
||||||
|
|
||||||
const previousOrderedStop = currentOrderedStop.previousStop;
|
|
||||||
if (!previousOrderedStop) return null;
|
|
||||||
|
|
||||||
const previousOrderedStopObject = await contextValue.repository.getStopById(previousOrderedStop.stopId);
|
|
||||||
if (!previousOrderedStopObject) return null;
|
|
||||||
|
|
||||||
return {
|
|
||||||
route: parent.route,
|
|
||||||
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);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
13
src/resolvers/EtaResolvers.ts
Normal file
13
src/resolvers/EtaResolvers.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { Resolvers } from "../generated/graphql";
|
||||||
|
import { ServerContext } from "../ServerContext";
|
||||||
|
|
||||||
|
export const EtaResolvers: Resolvers<ServerContext> = {
|
||||||
|
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);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
52
src/resolvers/OrderedStopResolvers.ts
Normal file
52
src/resolvers/OrderedStopResolvers.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import { OrderedStop, Resolvers } from "../generated/graphql";
|
||||||
|
import { ServerContext } from "../ServerContext";
|
||||||
|
|
||||||
|
export const OrderedStopResolvers: Resolvers<ServerContext> = {
|
||||||
|
OrderedStop: {
|
||||||
|
nextStop: async (parent, args, contextValue, info): Promise<OrderedStop | null> => {
|
||||||
|
const routeId = parent.routeId;
|
||||||
|
const stopId = parent.stopId;
|
||||||
|
|
||||||
|
const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId);
|
||||||
|
if (!currentOrderedStop) return null;
|
||||||
|
|
||||||
|
const nextOrderedStop = currentOrderedStop.nextStop;
|
||||||
|
if (!nextOrderedStop) return null;
|
||||||
|
|
||||||
|
const nextOrderedStopObject = await contextValue.repository.getStopById(nextOrderedStop.stopId);
|
||||||
|
if (!nextOrderedStopObject) return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
route: parent.route,
|
||||||
|
routeId: parent.routeId,
|
||||||
|
stopId: nextOrderedStopObject.id,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
previousStop: async (parent, args, contextValue, info): Promise<OrderedStop | null> => {
|
||||||
|
const routeId = parent.routeId;
|
||||||
|
const stopId = parent.stopId;
|
||||||
|
|
||||||
|
const currentOrderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(routeId, stopId);
|
||||||
|
if (!currentOrderedStop) return null;
|
||||||
|
|
||||||
|
const previousOrderedStop = currentOrderedStop.previousStop;
|
||||||
|
if (!previousOrderedStop) return null;
|
||||||
|
|
||||||
|
const previousOrderedStopObject = await contextValue.repository.getStopById(previousOrderedStop.stopId);
|
||||||
|
if (!previousOrderedStopObject) return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
route: parent.route,
|
||||||
|
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);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
|
}
|
||||||
36
src/resolvers/RouteResolvers.ts
Normal file
36
src/resolvers/RouteResolvers.ts
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
import { Coordinates, Resolvers } from "../generated/graphql";
|
||||||
|
import { ServerContext } from "../ServerContext";
|
||||||
|
|
||||||
|
export const RouteResolvers: Resolvers<ServerContext> = {
|
||||||
|
Route: {
|
||||||
|
shuttles: async (parent, args, contextValue, info) => {
|
||||||
|
const shuttles = await contextValue.repository.getShuttlesByRouteId(parent.id);
|
||||||
|
|
||||||
|
return shuttles.map(({
|
||||||
|
coordinates,
|
||||||
|
name,
|
||||||
|
id,
|
||||||
|
}) => ({
|
||||||
|
coordinates: coordinates as Coordinates,
|
||||||
|
name,
|
||||||
|
route: parent,
|
||||||
|
routeId: parent.id,
|
||||||
|
id,
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
orderedStop: async (parent, args, contextValue, info) => {
|
||||||
|
if (!args.forStopId) return null;
|
||||||
|
const orderedStop = await contextValue.repository.getOrderedStopByRouteAndStopId(parent.id, args.forStopId);
|
||||||
|
if (!orderedStop) return null;
|
||||||
|
|
||||||
|
const stop = await contextValue.repository.getStopById(orderedStop.stopId);
|
||||||
|
if (!stop) return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
stopId: args.forStopId,
|
||||||
|
routeId: parent.id,
|
||||||
|
route: parent,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
52
src/resolvers/ShuttleResolvers.ts
Normal file
52
src/resolvers/ShuttleResolvers.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import { Eta, Resolvers } from "../generated/graphql";
|
||||||
|
import { ServerContext } from "../ServerContext";
|
||||||
|
|
||||||
|
export const ShuttleResolvers: Resolvers<ServerContext> = {
|
||||||
|
Shuttle: {
|
||||||
|
eta: async (parent, args, contextValue, info) => {
|
||||||
|
if (!args.forStopId) return null;
|
||||||
|
const etaForStopId = await contextValue.repository.getEtaForShuttleAndStopId(parent.id, args.forStopId);
|
||||||
|
if (etaForStopId === null) return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
stopId: args.forStopId,
|
||||||
|
secondsRemaining: etaForStopId.secondsRemaining,
|
||||||
|
shuttleId: parent.id,
|
||||||
|
shuttle: parent,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
etas: async (parent, args, contextValue, info) => {
|
||||||
|
const etasForShuttle = await contextValue.repository.getEtasForShuttleId(parent.id);
|
||||||
|
if (!etasForShuttle) return null;
|
||||||
|
|
||||||
|
const computedEtas = await Promise.all(etasForShuttle.map(async ({
|
||||||
|
secondsRemaining,
|
||||||
|
stopId,
|
||||||
|
}): Promise<Eta | null> => {
|
||||||
|
return {
|
||||||
|
secondsRemaining,
|
||||||
|
stopId,
|
||||||
|
shuttle: parent,
|
||||||
|
shuttleId: parent.id,
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (computedEtas.every((eta) => eta !== null)) {
|
||||||
|
return computedEtas;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
},
|
||||||
|
route: async (parent, args, contextValue, info) => {
|
||||||
|
const route = await contextValue.repository.getRouteById(parent.routeId);
|
||||||
|
if (route === null) return null;
|
||||||
|
|
||||||
|
return {
|
||||||
|
color: route.color,
|
||||||
|
id: route.id,
|
||||||
|
name: route.name,
|
||||||
|
polylineCoordinates: route.polylineCoordinates,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
13
src/resolvers/StopResolvers.ts
Normal file
13
src/resolvers/StopResolvers.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { Resolvers } from "../generated/graphql";
|
||||||
|
import { ServerContext } from "../ServerContext";
|
||||||
|
|
||||||
|
export const StopResolvers: Resolvers<ServerContext> = {
|
||||||
|
Stop: {
|
||||||
|
orderedStops: async (parent, args, contextValue, info) => {
|
||||||
|
return await contextValue.repository.getOrderedStopsByStopId(parent.id);
|
||||||
|
},
|
||||||
|
etas: async (parent, args, contextValue, info) => {
|
||||||
|
return await contextValue.repository.getEtasForStopId(parent.id);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
63
src/resolvers/SystemResolvers.ts
Normal file
63
src/resolvers/SystemResolvers.ts
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import { Coordinates, Resolvers } from "../generated/graphql";
|
||||||
|
import { ServerContext } from "../ServerContext";
|
||||||
|
|
||||||
|
export const SystemResolvers: Resolvers<ServerContext> = {
|
||||||
|
System: {
|
||||||
|
routes: async (parent, args, contextValue, info) => {
|
||||||
|
return await contextValue.repository.getRoutesBySystemId(parent.id);
|
||||||
|
},
|
||||||
|
stops: async (parent, args, contextValue, info) => {
|
||||||
|
const stops = await contextValue.repository.getStopsBySystemId(parent.id);
|
||||||
|
return stops.map(({
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
coordinates
|
||||||
|
}) => ({
|
||||||
|
id,
|
||||||
|
name,
|
||||||
|
// Both ICoordinates and Coordinates have the same definition
|
||||||
|
coordinates: coordinates as Coordinates,
|
||||||
|
}));
|
||||||
|
},
|
||||||
|
stop: async (parent, args, contextValue, info) => {
|
||||||
|
if (!args.id) return null;
|
||||||
|
const stop = await contextValue.repository.getStopById(args.id);
|
||||||
|
if (stop === null) return null;
|
||||||
|
|
||||||
|
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 shuttle;
|
||||||
|
},
|
||||||
|
shuttles: async (parent, args, contextValue, info) => {
|
||||||
|
const shuttles = await contextValue.repository.getShuttlesBySystemId(parent.id);
|
||||||
|
|
||||||
|
return shuttles.map(shuttle => ({
|
||||||
|
coordinates: shuttle.coordinates,
|
||||||
|
name: shuttle.name,
|
||||||
|
id: shuttle.id,
|
||||||
|
routeId: shuttle.routeId,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user