forked from dbsystel/1BahnQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
131 lines (117 loc) · 5.79 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const { graphql } = require('graphql');
const schema = require('./schema.js');
const express = require('express');
const graphqlHTTP = require('./express-graphql/dist/index');
const ParkingspaceLoader = require('./Parkingspace/ParkingspaceLoader');
const FlinksterLoader = require('./Flinkster/FlinksterLoader');
const StationLoader = require('./Station/StationLoader');
const OperationLocationLoader = require('./OperationLocation/OperationLocationLoader');
const TravelCenterLoader = require('./TravelCenter/TravelCenterLoader');
const TimetableLoader = require('./Timetable/TimetableLoader.js');
const FacilityLoader = require('./Facility/FacilityLoader.js');
const StationPictureLoader = require('./StationPicture/StationPictureLoader');
const ParkingspaceService = require('./Parkingspace/ParkingspaceService');
const FlinksterService = require('./Flinkster/FlinksterService');
const OperationLocationService = require('./OperationLocation/OperationLocationService');
const StationService = require('./Station/StationService');
const NearbyStationService = require('./Station/NearbyStationsService');
const TravelCenterService = require('./TravelCenter/TravelCenterService');
const FacilityService = require('./Facility/FacilityService.js');
const RoutingService = require('./Routing/RoutingService.js');
const TimetableService = require('./Timetable/TimetableService.js');
const TrackService = require('./Platforms/TrackService.js');
const StationIdMappingService = require('./Station/StationIdMappingService');
const StationPictureService = require('./StationPicture/StationPictureService');
const StationRelationships = require('./Station/StationRelationships');
const ParkingspaceRelationships = require('./Parkingspace/ParkingspaceRelationships');
const RouteRelationships = require('./Routing/RouteRelationships');
const NearbyQuery = require('./NearbyQuery');
// --------- //
const APIToken = process.env.DBDeveloperAuthorization;
const baseURL = process.env.DBBaseURL || 'https://api.deutschebahn.com';
// Loader
const parkingspaceLoader = new ParkingspaceLoader(APIToken, baseURL);
const stationLoader = new StationLoader(APIToken, baseURL);
const timetableLoader = new TimetableLoader(APIToken, baseURL);
const operationLocationLoader = new OperationLocationLoader(APIToken, baseURL);
const travelCenterLoader = new TravelCenterLoader(APIToken, baseURL);
const facilityLoader = new FacilityLoader(APIToken, baseURL);
const flinksterLoader = new FlinksterLoader(APIToken, baseURL);
const stationPictureLoader = new StationPictureLoader(APIToken, baseURL);
// Services
const parkingspaceService = new ParkingspaceService(parkingspaceLoader);
const operationLocationService = new OperationLocationService(operationLocationLoader);
const stationIdMappingService = new StationIdMappingService();
const stationService = new StationService(stationLoader, stationIdMappingService);
const nearbyStationService = new NearbyStationService(stationService);
const travelCenterService = new TravelCenterService(travelCenterLoader);
const facilityService = new FacilityService(facilityLoader)
const routingService = new RoutingService();
const flinksterService = new FlinksterService(flinksterLoader);
const timetableServcie = new TimetableService(timetableLoader);
const trackService = new TrackService(stationIdMappingService);
const stationPictureService = new StationPictureService(stationPictureLoader)
// Relationships
stationService.relationships = new StationRelationships(parkingspaceService, facilityService, timetableServcie, trackService, stationPictureService);
parkingspaceService.relationships = new ParkingspaceRelationships(parkingspaceService, stationService);
routingService.relationships = new RouteRelationships(stationService, trackService);
// Queries
const root = {
parkingSpace: args => parkingspaceService.parkingspaceBySpaceId(args.id),
stationWithEvaId: args => stationService.stationByEvaId(args.evaId),
stationWithStationNumber: args => stationService.stationByBahnhofsnummer(args.stationNumber),
stationWithRill100: args => stationService.stationByRil100(args.rill100),
search: args => ({ stations: stationService.searchStations(args.searchTerm), operationLocations: operationLocationService.searchOperationLocations(args.searchTerm) }),
nearby: args => new NearbyQuery(args.latitude, args.longitude, args.radius, nearbyStationService, parkingspaceService, flinksterService, travelCenterService),
};
const experimental = process.env.experimental
if(experimental) {
root.routing = (args) => {
const routeSearch = routingService.routes(args.from, args.to);
return routeSearch.then(options => [options[0]]);
}
}
const introductionDemoQuery = `
# Welcome to 1BahnQL
#
# GraphiQL is an in-browser IDE for writing, validating, and
# testing GraphQL queries.
#
# Type queries into this side of the screen, and you will
# see intelligent typeaheads aware of the current GraphQL type schema and
# live syntax and validation errors highlighted within the text.
#
# To bring up the auto-complete at any point, just press Ctrl-Space.
#
# Press the run button above, or Cmd-Enter to execute the query, and the result
# will appear in the pane to the right.
#
# Learning resources:
# GraphQL: http://graphql.org
# GraphiQL: https://github.com/graphql/graphiql
# 1BahnQL: https://github.com/dbsystel/1BahnQL
#
#
# Example queries:
# Just comment out the query you would like to test and press the run button above,
# or Cmd-Enter to execute the query
{
stationWithEvaId(evaId: 8000105) {
name
location {
latitude
longitude
}
}
}
`
const app = express();
app.use('/graphql', graphqlHTTP({
schema,
rootValue: root,
graphiql: true
}));
// set the port of our application
// process.env.PORT lets the port be set by Heroku
const port = process.env.PORT || 8080;
app.listen(port, () => console.log(`now browse to localhost:${port}/graphql`));