-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolvers.js
78 lines (66 loc) · 2.67 KB
/
resolvers.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
const crypto = require('crypto');
const logger = require('heroku-logger')
const { paginateResults } = require('./utils');
const resolvers = {
Query: {
appEntry: (_, { user }) => {
if (!user)
user = crypto.randomBytes(20).toString('hex')
logger.info('Open app', { action: 'entry', user: user });
return { id: user };
},
tags: async (_, __, { dataSources }) => dataSources.placeAPI.getTags(),
restaurants: async (_, { pageSize = 10, after, user }, { dataSources }) => {
logger.info('Get all restaurants', { action: 'get', after: after, user: user });
const allRestaurants = await dataSources.placeAPI.allRestaurants();
const restaurants = paginateResults({
after,
pageSize,
results: allRestaurants,
});
return {
restaurants,
cursor: restaurants.length
? restaurants[restaurants.length - 1].id
: null,
hasMore: restaurants.length
? restaurants[restaurants.length - 1].id !==
allRestaurants[allRestaurants.length - 1].id
: false,
};
},
searchRestaurants: async (_, { lat, lng, tagIds, orderBy, priceLevel, pageSize = 10, after, user }, { dataSources }) => {
logger.info('Search restaurants', { action: 'search', tags: tagIds, after: after, user: user });
const allRestaurants = await dataSources.placeAPI.searchRestaurants(tagIds, lat, lng, orderBy, priceLevel);
const restaurants = paginateResults({
after,
pageSize,
results: allRestaurants,
});
return {
restaurants,
cursor: restaurants.length
? restaurants[restaurants.length - 1].id
: null,
hasMore: restaurants.length
? restaurants[restaurants.length - 1].id !==
allRestaurants[allRestaurants.length - 1].id
: false,
};
},
getRestaurantsByPlaceId: async (_, { placeIds, lat, lng, user }, { dataSources }) => {
logger.info('Get restaurants', { action: 'get', place: placeIds, user: user });
return dataSources.placeAPI.getRestaurants(placeIds, lat, lng);
},
getRestaurantByPlaceId: async (_, { placeId, user }, { dataSources }) => {
logger.info('Get restaurant', { action: 'get', place: placeId, user: user });
return dataSources.placeAPI.getRestaurant(placeId);
}
},
Restaurant: {
reviews: async (r, _, { dataSources }) => dataSources.placeAPI.getReviews(r.placeId),
isOpenNow: async (r, _, { dataSources }) => dataSources.placeAPI.isOpen(r.placeId),
photoUrls: async (r, _, { dataSources }) => dataSources.placeAPI.getPhotoUrls(r.placeId, r.photoUrls),
}
};
module.exports = resolvers;