-
Notifications
You must be signed in to change notification settings - Fork 18
/
s4v4.js
190 lines (169 loc) · 3.74 KB
/
s4v4.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Treehouse - Introduction to GraphQL - Stage 4 - Video 4
const { ApolloServer } = require("apollo-server");
const directors = [
{
id: "director_0",
name: "Steven Spielburg",
movieIds: [
"movie_0",
"movie_1",
],
},
];
const studios = [
{
id: "studio_0",
name: "Paramount",
location: "Hollywood",
movieIds: [
"movie_0",
"movie_1",
"movie_2",
]
},
{
id: "studio_1",
name: "Universal",
location: "Universal City",
movieIds: [
"movie_3",
]
},
];
const movies = [
{
id: "movie_0",
title: "Arachnophobia",
tagline: "Eight legs, two fangs, and an attitude.",
revenue: 53200000,
releaseYear: 2000,
},
{
id: "movie_1",
title: "Armageddon",
tagline: "Earth. It was fun while it lasted.",
revenue: 553700000,
releaseYear: 2000,
},
{
id: "movie_2",
title: "Catch Me If You Can",
tagline: "The true story of a real fake.",
revenue: 352100000,
releaseYear: 2000,
},
{
id: "movie_3",
title: "Christmas Vacation",
tagline: "Yule crack up.",
revenue: 71300000,
releaseYear: 2001,
},
];
let directorIdIndex = directors.length - 1;
let movieIdIndex = movies.length - 1;
/**
* This typeDefs variable holds our GraphQL Schema. This is the only
* part of this file you need to know about, you can ignore the rest!
*/
const typeDefs = `
type Movie {
id: ID!
title: String!
tagline: String
revenue: Int
studio: Studio
releaseYear: Int
directors: [Director!]
}
type Studio {
id: ID!
name: String!
location: String!
}
type Director {
id: ID!
name: String!
}
type Query {
allMovies: [Movie!]
topMovieByRevenue: Movie!
movieById (
movieId: ID!
): Movie!
randomMovieByYear (
year: Int!
): Movie!
}
input DirectorInput {
name: String!
}
type Mutation {
createMovie (
title: String!
tagline: String
revenue: Int
): Movie
addDirectorToMovie (
movieId: ID!
director: DirectorInput
): Movie
}
`;
const resolvers = {
Query: {
allMovies: (root, args, context) => {
return movies;
},
topMovieByRevenue: (root, args, context) => {
const moviesByRevenueDesc = movies.sort((movieA, movieB) => (movieA.revenue < movieB.revenue));
return moviesByRevenueDesc[0];
},
movieById: (root, args, context) => {
return movies.find(movie => movie.id === args.movieId);
},
randomMovieByYear: (root, args, context) => {
const moviesByYear = movies.filter(movie => movie.releaseYear === args.year);
const randomSeed = Math.floor(Math.random() * moviesByYear.length);
return moviesByYear[randomSeed];
}
},
Mutation: {
createMovie: (root, args, context) => {
const newMovie = {
id: `movie_${++movieIdIndex}`,
...args
};
movies.push(newMovie);
return newMovie;
},
addDirectorToMovie: (root, args, context) => {
const newDirector = {
id: `director_${++directorIdIndex}`,
movieIds: [args.movieId],
...args.director
};
directors.push(newDirector);
return movies.find(movie => movie.id === args.movieId);
}
},
Movie: {
studio: (root, args, context) => {
return studios.find(studio => {
return studio.movieIds.find(movieId => movieId === root.id);
});
},
directors: (root, args, context) => {
return directors.filter(director => {
return director.movieIds.find(movieId => movieId === root.id);
});
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
server.listen({ port: 3000 }).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});