forked from fbsamples/f8app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.js
366 lines (343 loc) · 9.41 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/**
* Copyright 2016 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE
*
*/
import {
GraphQLBoolean,
GraphQLFloat,
GraphQLID,
GraphQLInt,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
} from 'graphql';
import {
fromGlobalId,
globalIdField,
mutationWithClientMutationId,
nodeDefinitions,
} from 'graphql-relay';
import Parse from 'parse/node';
const Page = Parse.Object.extend('Page');
const FAQ = Parse.Object.extend('FAQ');
const Session = Parse.Object.extend('Agenda');
const Speaker = Parse.Object.extend('Speakers');
const Notification = Parse.Object.extend('Notification');
const Map = Parse.Object.extend('Maps');
var {nodeInterface, nodeField} = nodeDefinitions(
findObjectByGlobalId,
objectToGraphQLType
);
function findObjectByGlobalId(globalId) {
const {type, id} = fromGlobalId(globalId);
const Ent = ({Page, FAQ, Session, Speaker})[type];
return new Parse.Query(Ent).get(id);
}
function objectToGraphQLType(obj) {
switch (obj.className) {
case 'Page':
return F8PageType;
case 'Session':
return F8SessionType;
case 'Speaker':
return F8SpeakerType;
}
return null;
}
var USERS_SCHEDULE = {};
var F8FriendType = new GraphQLObjectType({
name: 'Friend',
description: 'Facebook friend',
fields: () => ({
id: {
type: GraphQLID,
},
name: {
type: GraphQLString,
},
picture: {
type: GraphQLString,
resolve: (friend) => `https://graph.facebook.com/${friend.id}/picture`,
},
schedule: {
type: new GraphQLList(F8SessionType),
description: 'Friends schedule',
resolve: (friend, args) => new Parse.Query(Session)
.containedIn('objectId', Object.keys(friend.schedule))
.find(),
},
})
});
function loadFriends(rootValue) {
return Parse.Cloud.run('friends', {user: rootValue});
}
function loadFriendsAttending(rootValue, session) {
const {id} = session;
return Parse.Cloud.run('friends', {user: rootValue})
.then(friends => friends.filter(friend => !!friend.schedule[id]));
}
var F8UserType = new GraphQLObjectType({
name: 'User',
description: 'A person who uses our app',
fields: () => ({
id: globalIdField('User'),
name: {
type: GraphQLString,
},
friends: {
type: new GraphQLList(F8FriendType),
description: 'User friends who are also in the F8 app and enabled sharing',
resolve: (user, args, {rootValue}) => loadFriends(rootValue),
},
notifications: {
type: new GraphQLList(F8NotificationType),
resolve: () => new Parse.Query(Notification).find(),
},
faqs: {
type: new GraphQLList(F8FAQType),
resolve: () => new Parse.Query(FAQ).find(),
},
pages: {
type: new GraphQLList(F8PageType),
resolve: () => new Parse.Query(Page).find(),
},
config: {
type: F8ConfigType,
resolve: () => Parse.Config.get(),
}
}),
interfaces: [nodeInterface],
});
var F8MapType = new GraphQLObjectType({
name: 'Map',
description: 'A place at F8 venue',
fields: () => ({
id: globalIdField('Map'),
name: {
type: GraphQLString,
resolve: (map) => map.get('name'),
},
map: {
type: GraphQLString,
resolve: (map) => map.get('x1') && map.get('x1').url(),
},
}),
});
var F8SessionType = new GraphQLObjectType({
name: 'Session',
description: 'Represents F8 agenda item',
fields: () => ({
id: globalIdField('Session'),
title: {
type: GraphQLString,
resolve: (session) => session.get('sessionTitle'),
},
slug: {
type: GraphQLString,
resolve: (session) => session.get('sessionSlug'),
},
day: {
type: GraphQLInt,
resolve: (session) => session.get('day'),
},
startTime: {
type: GraphQLFloat,
resolve: (session) => session.get('startTime').getTime(),
},
endTime: {
type: GraphQLFloat,
resolve: (session) => session.get('endTime').getTime(),
},
location: {
type: F8MapType,
resolve: (session) => new Parse.Query(Map).equalTo('name', session.get('sessionLocation')).first(),
},
description: {
type: GraphQLString,
resolve: (session) => session.get('sessionDescription'),
},
speakers: {
type: new GraphQLList(F8SpeakerType),
resolve: (session) =>
Promise.all((session.get('speakers') || []).map(speaker => speaker.fetch())),
},
isAdded: {
type: GraphQLBoolean,
description: 'If the session has been added to persons schedule',
resolve: (session, args, {rootValue}) => {
return !!USERS_SCHEDULE[session.id];
},
},
friends: {
type: new GraphQLList(F8FriendType),
description: 'User\'s friends who attend this session',
resolve: (session, args, {rootValue}) => loadFriendsAttending(rootValue, session),
},
}),
interfaces: [nodeInterface],
});
var F8PageType = new GraphQLObjectType({
name: 'Page',
description: 'Facebook pages',
fields: () => ({
id: globalIdField('Page'),
title: {
type: GraphQLString,
resolve: (page) => page.get('title'),
},
url: {
type: GraphQLString,
resolve: (page) => `https://www.facebook.com/${page.get('alias')}`,
},
logo: {
type: GraphQLString,
resolve: (page) => {
const logo = page.get('logo');
if (logo) {
return logo.url();
} else {
return `https://graph.facebook.com/${page.get('alias')}/picture?type=large`;
}
}
}
}),
interfaces: [nodeInterface],
});
var F8FAQType = new GraphQLObjectType({
name: 'FAQ',
description: 'Frequently asked questions',
fields: () => ({
id: globalIdField('FAQ'),
question: {
type: GraphQLString,
resolve: (faq) => faq.get('question'),
},
answer: {
type: GraphQLString,
resolve: (faq) => faq.get('answer'),
}
}),
interfaces: [nodeInterface],
});
var F8SpeakerType = new GraphQLObjectType({
name: 'Speaker',
fields: () => ({
id: globalIdField('Speaker'),
name: {
type: GraphQLString,
resolve: (speaker) => speaker.get('speakerName'),
},
title: {
type: GraphQLString,
resolve: (speaker) => speaker.get('speakerTitle'),
},
picture: {
type: GraphQLString,
resolve: (speaker) => speaker.get('speakerPic') && speaker.get('speakerPic').url(),
}
}),
interfaces: [nodeInterface],
});
var F8NotificationType = new GraphQLObjectType({
name: 'Notification',
fields: () => ({
id: globalIdField('Notification'),
text: {
type: GraphQLString,
resolve: (notification) => notification.get('text'),
},
url: {
type: GraphQLString,
resolve: (notification) => notification.get('url'),
},
time: {
type: GraphQLFloat,
description: 'Unix timestamp when the notification was sent.',
resolve: (notification) => notification.get('createdAt').getTime(),
}
}),
});
var F8ConfigType = new GraphQLObjectType({
name: 'Config',
fields: () => ({
wifiNetwork: {
type: GraphQLString,
resolve: (config) => config.get('wifiNetwork'),
},
wifiPassword: {
type: GraphQLString,
resolve: (config) => config.get('wifiPassword'),
},
}),
});
var F8QueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
node: nodeField,
viewer: {
type: F8UserType,
resolve: (rootValue) => rootValue, // TODO: Authenticate user
},
schedule: {
type: new GraphQLList(F8SessionType),
description: 'F8 agenda',
resolve: (user, args) => new Parse.Query(Session)
.ascending('startTime')
.find(),
},
}),
});
var addToScheduleMutation = mutationWithClientMutationId({
name: 'AddToSchedule',
inputFields: {
sessionId: {
type: new GraphQLNonNull(GraphQLID),
},
},
outputFields: {
session: {
type: F8SessionType,
resolve: (payload) => new Parse.Query(Session).get(payload.id),
},
},
mutateAndGetPayload: ({sessionId}, {rootValue}) => {
const {type, id} = fromGlobalId(sessionId);
if (type !== 'Session') {
throw new Error(`Invalid type ${type}`);
}
USERS_SCHEDULE[id] = true;
console.log(`Mutate ${id}`, rootValue);
return {id};
},
});
var F8MutationType = new GraphQLObjectType({
name: 'Mutation',
fields: () => ({
// Add your own mutations here
addToSchedule: addToScheduleMutation,
})
});
export var Schema = new GraphQLSchema({
query: F8QueryType,
mutation: F8MutationType,
});