mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-19 08:50:29 +00:00
break out dependencies/test data into more files
This commit is contained in:
10
src/index.ts
10
src/index.ts
@@ -2,15 +2,15 @@ import { readFileSync } from "fs";
|
|||||||
import { ApolloServer } from "@apollo/server";
|
import { ApolloServer } from "@apollo/server";
|
||||||
import { startStandaloneServer } from "@apollo/server/standalone";
|
import { startStandaloneServer } from "@apollo/server/standalone";
|
||||||
import { resolvers } from "./resolvers";
|
import { resolvers } from "./resolvers";
|
||||||
import { sharedMemory, SharedMemory } from "./sharedMemory";
|
import { sharedMemory } from "./sharedMemory";
|
||||||
|
import { loadTestData } from "./testData";
|
||||||
|
import { ServerContext } from "./serverContext";
|
||||||
|
|
||||||
const typeDefs = readFileSync("./schema.graphql", "utf8");
|
const typeDefs = readFileSync("./schema.graphql", "utf8");
|
||||||
|
|
||||||
interface ServerContext {
|
|
||||||
sharedMemory: SharedMemory,
|
|
||||||
}
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
|
loadTestData(sharedMemory);
|
||||||
|
|
||||||
const server = new ApolloServer<ServerContext>({
|
const server = new ApolloServer<ServerContext>({
|
||||||
typeDefs,
|
typeDefs,
|
||||||
resolvers
|
resolvers
|
||||||
|
|||||||
136
src/resolvers.ts
136
src/resolvers.ts
@@ -1,133 +1,13 @@
|
|||||||
import { Eta, OrderedStop, Resolvers, Route, Shuttle, Stop, System } from "./generated/graphql";
|
import { Eta, OrderedStop, Resolvers, Route, Shuttle, Stop, System } from "./generated/graphql";
|
||||||
|
import { ServerContext } from "./serverContext";
|
||||||
|
|
||||||
// Mock data
|
export const resolvers: Resolvers<ServerContext> = {
|
||||||
const systems: System[] = [
|
|
||||||
{
|
|
||||||
id: "1",
|
|
||||||
name: "Chapman University",
|
|
||||||
routes: [],
|
|
||||||
stops: [],
|
|
||||||
shuttles: [],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const routes: Route[] = [
|
|
||||||
{
|
|
||||||
name: "Red Route",
|
|
||||||
id: "1",
|
|
||||||
system: systems[0],
|
|
||||||
orderedStops: [],
|
|
||||||
shuttles: [],
|
|
||||||
polylineCoordinates: [],
|
|
||||||
color: "#ffffff",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "Teal Route",
|
|
||||||
id: "2",
|
|
||||||
system: systems[0],
|
|
||||||
orderedStops: [],
|
|
||||||
shuttles: [],
|
|
||||||
polylineCoordinates: [],
|
|
||||||
color: "#ffffff",
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const stops: Stop[] = [
|
|
||||||
{
|
|
||||||
id: "1",
|
|
||||||
name: "Chapman Court",
|
|
||||||
coordinates: {
|
|
||||||
latitude: 33.796001,
|
|
||||||
longitude: -117.8892805,
|
|
||||||
},
|
|
||||||
system: systems[0],
|
|
||||||
etas: [],
|
|
||||||
orderedStops: [],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: "2",
|
|
||||||
name: "Chapman Grand",
|
|
||||||
coordinates: {
|
|
||||||
latitude: 33.804433,
|
|
||||||
longitude: -117.895966,
|
|
||||||
},
|
|
||||||
system: systems[0],
|
|
||||||
etas: [],
|
|
||||||
orderedStops: [],
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const orderedStopsForRedRoute: OrderedStop[] = [
|
|
||||||
{
|
|
||||||
route: routes[0],
|
|
||||||
stop: stops[0],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
route: routes[0],
|
|
||||||
stop: stops[1],
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
const orderedStopsForTealRoute: OrderedStop[] = [
|
|
||||||
{
|
|
||||||
route: routes[1],
|
|
||||||
stop: stops[1],
|
|
||||||
},
|
|
||||||
{
|
|
||||||
route: routes[1],
|
|
||||||
stop: stops[0],
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
orderedStopsForRedRoute[0].nextStop = orderedStopsForRedRoute[1];
|
|
||||||
orderedStopsForRedRoute[1].previousStop = orderedStopsForRedRoute[0];
|
|
||||||
orderedStopsForTealRoute[0].nextStop = orderedStopsForTealRoute[1];
|
|
||||||
orderedStopsForTealRoute[1].previousStop = orderedStopsForTealRoute[0];
|
|
||||||
|
|
||||||
stops[0].orderedStops = [orderedStopsForRedRoute[0], orderedStopsForTealRoute[1]];
|
|
||||||
stops[1].orderedStops = [orderedStopsForRedRoute[1], orderedStopsForTealRoute[0]];
|
|
||||||
routes[0].orderedStops = orderedStopsForRedRoute;
|
|
||||||
routes[1].orderedStops = orderedStopsForTealRoute;
|
|
||||||
|
|
||||||
const shuttles: Shuttle[] = [
|
|
||||||
{
|
|
||||||
name: "Red Shuttle 17",
|
|
||||||
id: "1",
|
|
||||||
coordinates: {
|
|
||||||
latitude: 33.796001,
|
|
||||||
longitude: -117.8892805,
|
|
||||||
},
|
|
||||||
route: routes[0],
|
|
||||||
system: systems[0],
|
|
||||||
etas: [],
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
const etas: Eta[] = [
|
|
||||||
{
|
|
||||||
stop: stops[0],
|
|
||||||
shuttle: shuttles[0],
|
|
||||||
secondsRemaining: 12.023,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
stop: stops[1],
|
|
||||||
shuttle: shuttles[0],
|
|
||||||
secondsRemaining: 600.123,
|
|
||||||
}
|
|
||||||
];
|
|
||||||
|
|
||||||
shuttles[0].etas = etas;
|
|
||||||
|
|
||||||
routes[0].shuttles = shuttles;
|
|
||||||
|
|
||||||
systems[0].stops = stops;
|
|
||||||
systems[0].routes = routes;
|
|
||||||
systems[0].shuttles = shuttles;
|
|
||||||
|
|
||||||
export const resolvers: Resolvers = {
|
|
||||||
Query: {
|
Query: {
|
||||||
systems: () => systems,
|
systems: (parent, args, contextValue, info) => {
|
||||||
|
return contextValue.sharedMemory.systems;
|
||||||
|
},
|
||||||
system: (parent, args, contextValue, info) => {
|
system: (parent, args, contextValue, info) => {
|
||||||
|
const systems = contextValue.sharedMemory.systems;
|
||||||
const system = systems.find((system) => system.id === args.id);
|
const system = systems.find((system) => system.id === args.id);
|
||||||
if (!system) {
|
if (!system) {
|
||||||
return null;
|
return null;
|
||||||
@@ -138,6 +18,8 @@ export const resolvers: Resolvers = {
|
|||||||
},
|
},
|
||||||
System: {
|
System: {
|
||||||
stop: (parent, args, contextValue, info) => {
|
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);
|
const stop = stops.find((stop) => stop.id === args.id);
|
||||||
if (!stop) {
|
if (!stop) {
|
||||||
return null;
|
return null;
|
||||||
@@ -146,6 +28,7 @@ export const resolvers: Resolvers = {
|
|||||||
return stop;
|
return stop;
|
||||||
},
|
},
|
||||||
route: (parent, args, contextValue, info) => {
|
route: (parent, args, contextValue, info) => {
|
||||||
|
const routes = parent.routes
|
||||||
const route = routes.find((route) => route.id === args.id);
|
const route = routes.find((route) => route.id === args.id);
|
||||||
if (!route) {
|
if (!route) {
|
||||||
return null;
|
return null;
|
||||||
@@ -154,6 +37,7 @@ export const resolvers: Resolvers = {
|
|||||||
return route;
|
return route;
|
||||||
},
|
},
|
||||||
shuttle: (parent, args, contextValue, info) => {
|
shuttle: (parent, args, contextValue, info) => {
|
||||||
|
const shuttles = parent.shuttles;
|
||||||
const shuttle = shuttles.find((shuttle) => shuttle.id === args.id);
|
const shuttle = shuttles.find((shuttle) => shuttle.id === args.id);
|
||||||
if (!shuttle) {
|
if (!shuttle) {
|
||||||
return null;
|
return null;
|
||||||
|
|||||||
5
src/serverContext.ts
Normal file
5
src/serverContext.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { SharedMemory } from "./sharedMemory";
|
||||||
|
|
||||||
|
export interface ServerContext {
|
||||||
|
sharedMemory: SharedMemory,
|
||||||
|
}
|
||||||
130
src/testData.ts
Normal file
130
src/testData.ts
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
// Mock data
|
||||||
|
import { Eta, OrderedStop, Route, Shuttle, Stop, System } from "./generated/graphql";
|
||||||
|
import { SharedMemory } from "./sharedMemory";
|
||||||
|
|
||||||
|
const systems: System[] = [
|
||||||
|
{
|
||||||
|
id: "1",
|
||||||
|
name: "Chapman University",
|
||||||
|
routes: [],
|
||||||
|
stops: [],
|
||||||
|
shuttles: [],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const routes: Route[] = [
|
||||||
|
{
|
||||||
|
name: "Red Route",
|
||||||
|
id: "1",
|
||||||
|
system: systems[0],
|
||||||
|
orderedStops: [],
|
||||||
|
shuttles: [],
|
||||||
|
polylineCoordinates: [],
|
||||||
|
color: "#ffffff",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Teal Route",
|
||||||
|
id: "2",
|
||||||
|
system: systems[0],
|
||||||
|
orderedStops: [],
|
||||||
|
shuttles: [],
|
||||||
|
polylineCoordinates: [],
|
||||||
|
color: "#ffffff",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const stops: Stop[] = [
|
||||||
|
{
|
||||||
|
id: "1",
|
||||||
|
name: "Chapman Court",
|
||||||
|
coordinates: {
|
||||||
|
latitude: 33.796001,
|
||||||
|
longitude: -117.8892805,
|
||||||
|
},
|
||||||
|
system: systems[0],
|
||||||
|
etas: [],
|
||||||
|
orderedStops: [],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "2",
|
||||||
|
name: "Chapman Grand",
|
||||||
|
coordinates: {
|
||||||
|
latitude: 33.804433,
|
||||||
|
longitude: -117.895966,
|
||||||
|
},
|
||||||
|
system: systems[0],
|
||||||
|
etas: [],
|
||||||
|
orderedStops: [],
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const orderedStopsForRedRoute: OrderedStop[] = [
|
||||||
|
{
|
||||||
|
route: routes[0],
|
||||||
|
stop: stops[0],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
route: routes[0],
|
||||||
|
stop: stops[1],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
const orderedStopsForTealRoute: OrderedStop[] = [
|
||||||
|
{
|
||||||
|
route: routes[1],
|
||||||
|
stop: stops[1],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
route: routes[1],
|
||||||
|
stop: stops[0],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
orderedStopsForRedRoute[0].nextStop = orderedStopsForRedRoute[1];
|
||||||
|
orderedStopsForRedRoute[1].previousStop = orderedStopsForRedRoute[0];
|
||||||
|
orderedStopsForTealRoute[0].nextStop = orderedStopsForTealRoute[1];
|
||||||
|
orderedStopsForTealRoute[1].previousStop = orderedStopsForTealRoute[0];
|
||||||
|
|
||||||
|
stops[0].orderedStops = [orderedStopsForRedRoute[0], orderedStopsForTealRoute[1]];
|
||||||
|
stops[1].orderedStops = [orderedStopsForRedRoute[1], orderedStopsForTealRoute[0]];
|
||||||
|
routes[0].orderedStops = orderedStopsForRedRoute;
|
||||||
|
routes[1].orderedStops = orderedStopsForTealRoute;
|
||||||
|
|
||||||
|
const shuttles: Shuttle[] = [
|
||||||
|
{
|
||||||
|
name: "Red Shuttle 17",
|
||||||
|
id: "1",
|
||||||
|
coordinates: {
|
||||||
|
latitude: 33.796001,
|
||||||
|
longitude: -117.8892805,
|
||||||
|
},
|
||||||
|
route: routes[0],
|
||||||
|
system: systems[0],
|
||||||
|
etas: [],
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
const etas: Eta[] = [
|
||||||
|
{
|
||||||
|
stop: stops[0],
|
||||||
|
shuttle: shuttles[0],
|
||||||
|
secondsRemaining: 12.023,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
stop: stops[1],
|
||||||
|
shuttle: shuttles[0],
|
||||||
|
secondsRemaining: 600.123,
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
|
shuttles[0].etas = etas;
|
||||||
|
|
||||||
|
routes[0].shuttles = shuttles;
|
||||||
|
|
||||||
|
systems[0].stops = stops;
|
||||||
|
systems[0].routes = routes;
|
||||||
|
systems[0].shuttles = shuttles;
|
||||||
|
|
||||||
|
export function loadTestData(sharedMemory: SharedMemory) {
|
||||||
|
sharedMemory.systems = systems;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user