-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.js
95 lines (92 loc) · 2.37 KB
/
schema.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
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLList,
GraphQLFloat,
} = require("graphql");
const { findNearestStation } = require("./calculations/geolocation");
const { findShortestPath } = require("./calculations/algo");
const delhi = require("./stations/delhi-data.json");
const delhiComplete = require("./scripts/delhi-stations.json");
//Station Type
const detailType = new GraphQLObjectType({
name: "Detail",
fields: () => ({
line: { type: new GraphQLList(GraphQLString) },
layout: { type: GraphQLString },
longitude: { type: GraphQLFloat },
latitude: { type: GraphQLFloat },
}),
});
const stationType = new GraphQLObjectType({
name: "Station",
fields: () => ({
id: { type: GraphQLString },
title: { type: GraphQLString },
connected: { type: GraphQLList(GraphQLString) },
details: { type: detailType },
}),
});
const nearestStationType = new GraphQLObjectType({
name: "nearestStation",
fields: () => ({
nearestStation: { type: GraphQLString },
distance: { type: GraphQLFloat },
}),
});
const stationListType = new GraphQLObjectType({
name: "StationList",
fields: () => ({
station: { type: GraphQLString },
lines: { type: new GraphQLList(GraphQLString) },
}),
});
const routeType = new GraphQLObjectType({
name: "Route",
fields: () => ({
time: { type: GraphQLFloat },
stationsList: { type: new GraphQLList(stationListType) },
fare: { type: GraphQLInt },
interchange: { type: GraphQLInt },
}),
});
//Root Query
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
nearestStation: {
type: nearestStationType,
args: {
latitude: { type: GraphQLFloat },
longitude: { type: GraphQLFloat },
},
resolve(parentValue, args) {
return findNearestStation(args.latitude, args.longitude);
},
},
stations: {
type: new GraphQLList(stationType),
resolve(parentValue, args) {
return delhiComplete;
},
},
route: {
type: routeType,
args: {
source: { type: GraphQLInt },
destination: { type: GraphQLInt },
},
resolve(parentValue, args) {
return findShortestPath(
parseInt(args.source, 10),
parseInt(args.destination, 10)
);
},
},
},
});
module.exports = new GraphQLSchema({
query: RootQuery,
});