Skip to content
This repository was archived by the owner on Dec 23, 2017. It is now read-only.

Commit f94d62a

Browse files
committed
feat(Resolvers): add query, field, and mock resolvers
1 parent 379d13f commit f94d62a

File tree

1 file changed

+134
-13
lines changed

1 file changed

+134
-13
lines changed

src/resolvers.js

Lines changed: 134 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,154 @@ import casual from 'casual';
99
export default {
1010
// Queries (where does the data come from?)
1111
queryResolvers: {
12-
// TODO: Update query resolver name(s) to match schema queries
13-
YourDataSource: (rootValue, { id }, context) =>
12+
searchMoviesByTitle: (rootValue, { options }, context) =>
1413
new Promise((resolve, reject) => {
15-
// TODO: Update to use the model and call the proper method.
16-
context.YourDataSource
17-
.getById(id)
14+
context.IMDBAPI
15+
.searchMoviesByTitle(options)
16+
.then(resolve)
17+
.catch(reject);
18+
}),
19+
getMovieById: (rootValue, { movie_id }, context) =>
20+
new Promise((resolve, reject) => {
21+
context.IMDBAPI
22+
.getMovieById(movie_id)
23+
.then(resolve)
24+
.catch(reject);
25+
}),
26+
searchPersonByName: (rootValue, { name }, context) =>
27+
new Promise((resolve, reject) => {
28+
context.IMDBAPI
29+
.searchPersonByName(name)
30+
.then(resolve)
31+
.catch(reject);
32+
}),
33+
getPersonById: (rootValue, { person_id }, context) =>
34+
new Promise((resolve, reject) => {
35+
context.IMDBAPI
36+
.getPersonById(person_id)
1837
.then(resolve)
1938
.catch(reject);
2039
}),
2140
},
2241

2342
// Data fields (which data from the response goes to which field?)
2443
dataResolvers: {
25-
// TODO: Update to reference the schema type(s) and field(s).
26-
PFX_YourDataSource: {
27-
// If a field isn’t always set, but it shouldn’t break the response, make it nullable.
28-
name: data => data.name || null,
44+
IMDB_Person: {
45+
filmography: ({ filmography }, { filter }) =>
46+
Object.keys(filmography)
47+
.reduce(
48+
(works, position) =>
49+
works.concat(
50+
filmography[position].map(work => ({
51+
position,
52+
imdb_id: work.imdb_id,
53+
title: work.title,
54+
type: work.type,
55+
url: work.url,
56+
year: work.year,
57+
})),
58+
),
59+
[],
60+
)
61+
.filter(work => work.position === filter),
62+
},
63+
IMDB_Metadata: {
64+
// Alias this field to fix the typo.
65+
asp_ratio: data => data.asp_retio,
2966
},
3067
},
3168

3269
// Mock data (How can I get real-feeling fake data while working offline?)
3370
mockResolvers: {
34-
// TODO: Update to mock all schema fields and types.
35-
PFX_YourDataSource: () => ({
36-
id: casual.uuid,
71+
IMDB_Movie: () => ({
72+
cast: () => new MockList([1, 10]),
73+
content_rating: casual.random_element(['PG', 'R', 'PG-13']),
74+
description: casual.sentences(2),
75+
director: casual.name,
76+
genre: () => [casual.random_element(['Action', 'Drama', 'Comedy'])],
77+
imdb_id: `tt${Math.round(10000000 * Math.random())}`,
78+
length: `${casual.integer(75, 190)}`,
79+
original_title: casual.title,
80+
rating: casual.integer(0, 100) / 10,
81+
rating_count: casual.integer(0, 300),
82+
release_date: casual.date('YYYY-MM-DD'),
83+
stars: () => [casual.name],
84+
storyline: casual.sentences(2),
85+
title: casual.title,
86+
trailer: () => new MockList([1, 3]),
87+
writers: () => [casual.name, casual.name],
88+
year: casual.year,
89+
}),
90+
IMDB_Cast: () => ({
91+
character: casual.name,
92+
image:
93+
'https://images-na.ssl-images-amazon.com/images/M/MV5BMTU1MzE4MjAzMV5BMl5BanBnXkFtZTcwMjA2MTMyMw@@._V1_UY44_CR0,0,32,44_AL_.jpg',
94+
link: 'http://www.imdb.com/name/nm0001804/?ref_=tt_cl_t1',
3795
name: casual.name,
38-
lucky_numbers: () => new MockList([0, 3]),
96+
}),
97+
IMDB_Metadata: () => ({
98+
also_known_as: [casual.title],
99+
asp_ratio: '16 : 9',
100+
budget: casual
101+
.integer(900000, 15000000)
102+
.toLocaleString('en-US', { style: 'currency', currency: 'USD' }),
103+
countries: [casual.city, casual.city],
104+
filming_locations: [casual.city, casual.city],
105+
gross: casual
106+
.integer(1000000, 50000000)
107+
.toLocaleString('en-US', { style: 'currency', currency: 'USD' }),
108+
languages: [casual.random_element(['English', 'Spanish'])],
109+
sound_mix: ['Auro 11.1', 'Dolby Digital', 'Dolby Surround 7.1'],
110+
}),
111+
IMDB_Poster: () => ({
112+
large:
113+
'https://images-na.ssl-images-amazon.com/images/M/MV5BMjA4NDg3NzYxMF5BMl5BanBnXkFtZTcwNTgyNzkyNw@@._V1_.jpg',
114+
thumb:
115+
'https://images-na.ssl-images-amazon.com/images/M/MV5BMjA4NDg3NzYxMF5BMl5BanBnXkFtZTcwNTgyNzkyNw@@._V1_UX182_CR0,0,182,268_AL_.jpg',
116+
}),
117+
IMDB_Trailer: () => ({
118+
definition: casual.random_element([
119+
'SD',
120+
'auto',
121+
'480p',
122+
'720p',
123+
'1080p',
124+
]),
125+
mimeType: casual.mime_type,
126+
videoUrl:
127+
'https://video-http.media-imdb.com/MV5BOWQzZjQyZDctNDYzOS00ZjQyLTg4NWItMWQwNjg1YmMzZjdjXkExMV5BbXA0XkFpbWRiLWV0cy10cmFuc2NvZGU@.mp4?Expires=1508520493&Signature=MEPmF1uuC-4WaQhu3P4xei~pr3qKihmacZmPbPxl4Iaj~GO9jBHYOiMZHvrnOSedLS8VQscLBYr-61ubSsbr3aomM7VYqymKLGHHcE4tMO71oM8oB6QHBu3gHh~D~HWNrfkCMrdIGai3GRzVsZX4KcoC8oOvstOn2sBh-YlAQQM_&Key-Pair-Id=APKAILW5I44IHKUN2DYA',
128+
}),
129+
IMDB_Url: () => ({
130+
title: casual.title,
131+
url: casual.url,
132+
year: casual.year,
133+
}),
134+
IMDB_Person: () => ({
135+
birthday: casual.date('YYYY-MM-DD'),
136+
birthplace: `${casual.city}, ${casual.state}, ${casual.country}`,
137+
description: casual.sentences(2),
138+
filmography: () => new MockList([1, 5]),
139+
person_id: `nm${Math.round(10000000 * Math.random())}`,
140+
photos: [
141+
'https://images-na.ssl-images-amazon.com/images/M/MV5BMzA2ODY4MTQ5N15BMl5BanBnXkFtZTgwNTI0NzU5MDE@._V1_SY1000_CR0,0,666,1000_AL_.jpg',
142+
'https://images-na.ssl-images-amazon.com/images/M/MV5BMTQxNTY4MTc0MV5BMl5BanBnXkFtZTcwMDQ2MTY5Mg@@._V1_SY1000_CR0,0,666,1000_AL_.jpg',
143+
],
144+
title: casual.name,
145+
type: ['Actor', 'Writer', 'Producer'],
146+
}),
147+
IMDB_Filmography: () => ({
148+
position: casual.random_element(['actor', 'writer', 'producer', 'self']),
149+
imdb_id: `tt${Math.round(10000000 * Math.random())}`,
150+
title: casual.title,
151+
type: casual.random_element(['TV Series', 'Film']),
152+
url: casual.url,
153+
year: casual.year,
154+
}),
155+
IMDB_PersonImage: () => ({
156+
poster:
157+
'https://images-na.ssl-images-amazon.com/images/M/MV5BMTQwMjAwNzI0M15BMl5BanBnXkFtZTcwOTY1MTMyOQ@@._V1_UY1200_CR173,0,630,1200_AL_.jpg',
158+
thumb:
159+
'https://images-na.ssl-images-amazon.com/images/M/MV5BMTQwMjAwNzI0M15BMl5BanBnXkFtZTcwOTY1MTMyOQ@@._V1_UY317_CR22,0,214,317_AL_.jpg',
39160
}),
40161
},
41162
};

0 commit comments

Comments
 (0)