mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-16 23:40:32 +00:00
add more custom resolvers
This commit is contained in:
168
src/resolvers.ts
168
src/resolvers.ts
@@ -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";
|
||||
|
||||
export const resolvers: Resolvers<ServerContext> = {
|
||||
Query: {
|
||||
systems: (parent, args, contextValue, info) => {
|
||||
return contextValue.sharedMemory.systems;
|
||||
systems: async (parent, args, contextValue, info) => {
|
||||
const systems = await contextValue.repository.getSystems();
|
||||
return systems.map(({
|
||||
name,
|
||||
id
|
||||
}) => ({
|
||||
name,
|
||||
id
|
||||
}));
|
||||
},
|
||||
system: (parent, args, contextValue, info) => {
|
||||
const systems = contextValue.sharedMemory.systems;
|
||||
const system = systems.find((system) => system.id === args.id);
|
||||
if (!system) {
|
||||
return null;
|
||||
}
|
||||
system: async (parent, args, contextValue, info) => {
|
||||
if (!args.id) return null;
|
||||
const system = await contextValue.repository.getSystemById(args.id);
|
||||
if (system === null) return null;
|
||||
|
||||
return system;
|
||||
return {
|
||||
name: system.name,
|
||||
id: system.id,
|
||||
};
|
||||
}
|
||||
},
|
||||
System: {
|
||||
stop: (parent, args, contextValue, info) => {
|
||||
// TODO implement interface with ID and one function to perform search
|
||||
const stops = parent.stops;
|
||||
const stop = stops.find((stop) => stop.id === args.id);
|
||||
if (!stop) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return stop;
|
||||
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,
|
||||
}));
|
||||
},
|
||||
route: (parent, args, contextValue, info) => {
|
||||
const routes = parent.routes
|
||||
const route = routes.find((route) => route.id === args.id);
|
||||
if (!route) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return route;
|
||||
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,
|
||||
}));
|
||||
},
|
||||
shuttle: (parent, args, contextValue, info) => {
|
||||
const shuttles = parent.shuttles;
|
||||
const shuttle = shuttles.find((shuttle) => shuttle.id === args.id);
|
||||
if (!shuttle) {
|
||||
return null;
|
||||
}
|
||||
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 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: {
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
@@ -58,7 +99,7 @@ export const resolvers: Resolvers<ServerContext> = {
|
||||
},
|
||||
Shuttle: {
|
||||
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) {
|
||||
return null;
|
||||
}
|
||||
@@ -66,4 +107,59 @@ export const resolvers: Resolvers<ServerContext> = {
|
||||
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,
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user