-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
137 lines (126 loc) · 3.09 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
132
133
134
135
136
137
const { ApolloServer, gql } = require("apollo-server");
const { GraphQLScalarType } = require("graphql");
const lifts = require("./data/lifts.json");
const trails = require("./data/trails.json");
const typeDefs = gql`
scalar DateTime
type Lift {
id: ID
name: String!
status: LiftStatus!
capacity: Int!
night: Boolean!
elevationGain: Int!
trailAccess: [Trail!]!
}
type Trail {
id: ID
name: String!
status: TrailStatus
difficulty: String!
groomed: Boolean!
trees: Boolean!
night: Boolean!
accessedByLifts: [Lift!]!
}
enum LiftStatus {
OPEN
HOLD
CLOSED
}
enum TrailStatus {
OPEN
CLOSED
}
type SetLiftStatusPayload {
lift: Lift
changed: DateTime
}
type Query {
allLifts(status: LiftStatus): [Lift!]!
findLiftById(id: ID!): Lift!
liftCount(status: LiftStatus!): Int!
allTrails(status: TrailStatus): [Trail!]!
findTrailByName(name: String!): Trail!
trailCount(status: TrailStatus!): Int!
}
type Mutation {
setLiftStatus(id: ID!, status: LiftStatus!): SetLiftStatusPayload!
setTrailStatus(id: ID!, status: TrailStatus!): Trail!
}
`;
const resolvers = {
Query: {
allLifts: (parent, { status }) => {
if (!status) {
return lifts;
} else {
return lifts.filter(lift => lift.status === status);
}
},
findLiftById: (parent, { id }) => {
return lifts.find(lift => id === lift.id);
},
liftCount: (parent, { status }) => {
let i = 0;
lifts.map(lift => {
lift.status === status ? i++ : null;
});
return i;
},
allTrails: (parent, { status }) => {
if (!status) {
return trails;
} else {
return trails.filter(trail => trail.status === status);
}
},
findTrailByName: (parent, { name }) => {
return trails.find(trail => name === trail.name);
},
trailCount: (parent, { status }) => {
let i = 0;
trails.map(trail => {
trail.status === status ? i++ : null;
});
return i;
}
},
Mutation: {
setLiftStatus: (parent, { id, status }) => {
let updatedLift = lifts.find(lift => id === lift.id);
updatedLift.status = status;
return {
lift: updatedLift,
changed: new Date()
};
},
setTrailStatus: (parent, { id, status }) => {
let updatedTrail = trails.find(trail => id === trail.id);
updatedTrail.status = status;
return updatedTrail;
}
},
Lift: {
trailAccess: parent =>
parent.trails.map(id => trails.find(t => id === t.id)).filter(x => x)
},
Trail: {
accessedByLifts: parent =>
parent.lift.map(id => lifts.find(l => id === l.id))
},
DateTime: new GraphQLScalarType({
name: "DateTime",
description: "A valid date time value.",
parseValue: value => new Date(value),
serialize: value => new Date(value).toISOString(),
parseLiteral: ast => new Date(ast.value)
})
};
const server = new ApolloServer({
typeDefs,
resolvers
});
server.listen().then(({ url }) => {
console.log(`Server running at ${url}`);
});