diff --git a/src/ServerContext.ts b/src/ServerContext.ts index bccfcf2..b55a8b1 100644 --- a/src/ServerContext.ts +++ b/src/ServerContext.ts @@ -2,4 +2,5 @@ import { InterchangeSystem } from "./entities/InterchangeSystem"; export interface ServerContext { systems: InterchangeSystem[]; + findSystemById: (id: string) => InterchangeSystem | null; } diff --git a/src/resolvers/StopResolvers.ts b/src/resolvers/StopResolvers.ts index 9a2b55a..89f6444 100644 --- a/src/resolvers/StopResolvers.ts +++ b/src/resolvers/StopResolvers.ts @@ -4,14 +4,14 @@ import { ServerContext } from "../ServerContext"; export const StopResolvers: Resolvers = { Stop: { orderedStops: async (parent, args, contextValue, _info) => { - const system = contextValue.systems.find((system) => system.id === parent.id); + const system = contextValue.findSystemById(parent.id); if (!system) { return []; } return await system.shuttleRepository.getOrderedStopsByStopId(parent.id); }, etas: async (parent, args, contextValue, _info) => { - const system = contextValue.systems.find((system) => system.id === parent.id); + const system = contextValue.findSystemById(parent.id); if (!system) { return []; } diff --git a/src/resolvers/SystemResolvers.ts b/src/resolvers/SystemResolvers.ts index dd3c856..8ea01ae 100644 --- a/src/resolvers/SystemResolvers.ts +++ b/src/resolvers/SystemResolvers.ts @@ -3,16 +3,16 @@ import { ServerContext } from "../ServerContext"; export const SystemResolvers: Resolvers = { System: { - routes: async (parent, args, contextValue, _info) => { - const system = contextValue.systems.find((system) => system.id === parent.id); + routes: async (parent, _args, contextValue, _info) => { + const system = contextValue.findSystemById(parent.id); if (!system) { return []; } return await system.shuttleRepository.getRoutesBySystemId(parent.id); }, - stops: async (parent, args, contextValue, _info) => { - const system = contextValue.systems.find((system) => system.id === parent.id); + stops: async (parent, _args, contextValue, _info) => { + const system = contextValue.findSystemById(parent.id); if (!system) { return []; } @@ -21,7 +21,7 @@ export const SystemResolvers: Resolvers = { }, stop: async (parent, args, contextValue, _info) => { if (!args.id) return null; - const system = contextValue.systems.find((system) => system.id === parent.id); + const system = contextValue.findSystemById(parent.id); if (!system) { return null; } @@ -40,7 +40,7 @@ export const SystemResolvers: Resolvers = { }, route: async (parent, args, contextValue, _info) => { if (!args.id) return null; - const system = contextValue.systems.find((system) => system.id === parent.id); + const system = contextValue.findSystemById(parent.id); if (!system) { return null; } @@ -59,7 +59,7 @@ export const SystemResolvers: Resolvers = { }, shuttle: async (parent, args, contextValue, _info) => { if (!args.id) return null; - const system = contextValue.systems.find((system) => system.id === parent.id); + const system = contextValue.findSystemById(parent.id); if (!system) { return null; } @@ -71,7 +71,7 @@ export const SystemResolvers: Resolvers = { return shuttle; }, shuttles: async (parent, args, contextValue, _info) => { - const system = contextValue.systems.find((system) => system.id === parent.id); + const system = contextValue.findSystemById(parent.id); if (!system) { return []; }