This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.js
325 lines (316 loc) · 10.1 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
'use strict';
// The graphql-sync module is a thin wrapper around graphql-js
// which provides an identical API except it doesn't use promises
// and instead always resolves synchronously. This allows us to
// use it in Foxx (which doesn't support async resolution).
const gql = require('graphql');
// If you want to use graphql-sync in your own Foxx services
// make sure to install it in the Foxx service's folder using
// the "npm" command-line tool and to include the "node_modules"
// folder when bundling your Foxx service for deployment.
const db = require('@arangodb').db;
// Using module.context.collection allows us to use the
// collection with a common prefix based on where the service
// is mounted. This way we can have multiple copies of this
// service mounted on the same database without worrying about
// name conflicts in their collections.
const episodes = module.context.collection('episodes');
const characters = module.context.collection('characters');
const friends = module.context.collection('friends');
const appearsIn = module.context.collection('appearsIn');
let droidType, humanType, characterInterface;
// GraphQL ENUM values can be used as literals rather
// than strings but their possible values have to be
// statically defined.
const speciesType = new gql.GraphQLEnumType({
name: 'Species',
description: 'Species of a character: human or droid.',
values: {
HUMAN: {
value: 'human', // The internal value stored in ArangoDB
description: 'A humanoid creature in the Star Wars universe.'
},
DROID: {
value: 'droid',
description: 'A mechanical creature in the Star Wars universe.'
}
}
});
// In the original graphql-js test fixtures episodes
// are defined using a GraphQL ENUM type. Because we're
// in a database, it makes more sense to define them
// as an object type backed by a collection.
const episodeType = new gql.GraphQLObjectType({
name: 'Episode',
description: 'An episode in the Star Wars Trilogy.',
fields() {
return {
id: {
type: new gql.GraphQLNonNull(gql.GraphQLString),
description: 'The id of the episode.',
resolve(episode) {
// The objects exposed by GraphQL have an "id"
// field which corresponds to ArangoDB's "_key"
// property. This mapping isn't strictly necessary
// but hides the implementation of the data
// source to make it more consistent with other
// GraphQL APIs.
return episode._key;
}
},
// These fields directly correspond to properties
// on the documents and thus don't need "resolve"
// methods.
title: {
type: gql.GraphQLString,
description: 'The title of the episode.'
},
description: {
type: gql.GraphQLString,
description: 'The description of the episode.'
}
};
}
});
characterInterface = new gql.GraphQLInterfaceType({
name: 'Character',
description: 'A character in the Star Wars Trilogy',
fields() {
return {
id: {
type: new gql.GraphQLNonNull(gql.GraphQLString),
description: 'The id of the character.'
},
species: {
type: new gql.GraphQLNonNull(speciesType),
description: 'The species of the character.'
},
name: {
type: gql.GraphQLString,
description: 'The name of the character.'
},
friends: {
type: new gql.GraphQLList(characterInterface),
description: (
'The friends of the character, '
+ 'or an empty list if they have none.'
),
args: {
species: {
type: speciesType,
description: 'The species of the friends.'
}
}
},
appearsIn: {
type: new gql.GraphQLList(episodeType),
description: 'Which movies they appear in.'
}
};
},
resolveType(character) {
// Droids and humans have different fields.
// The "$type" property allows GraphQL to decide which
// GraphQL type a document should correspond to.
return character.$type === 'droid' ? droidType : humanType;
}
});
humanType = new gql.GraphQLObjectType({
name: 'Human',
description: 'A humanoid creature in the Star Wars universe.',
fields() {
return {
id: {
type: new gql.GraphQLNonNull(gql.GraphQLString),
description: 'The id of the human.',
resolve(human) {
return human._key;
}
},
species: {
type: new gql.GraphQLNonNull(speciesType),
description: 'The species of the human.',
resolve(human) {
return human.$type;
}
},
name: {
type: gql.GraphQLString,
description: 'The name of the human.'
},
friends: {
type: new gql.GraphQLList(characterInterface),
description: 'The friends of the human, or an empty list if they have none.',
args: {
species: {
type: speciesType,
description: 'The species of the friends.'
}
},
resolve(human, args) {
// We want to store friendship relations as edges in an
// edge collection. Here we're returning the friends of
// a character with an AQL graph traversal query, see
// https://docs.arangodb.com/Aql/GraphTraversals.html#working-on-collection-sets
const species = args.species || null;
return db._query(aqlQuery`
WITH ${characters}
FOR friend IN ANY ${human} ${friends}
FILTER !${species} || friend.$type == ${species}
SORT friend._key ASC
RETURN friend
`).toArray();
}
},
appearsIn: {
type: new gql.GraphQLList(episodeType),
description: 'Which movies they appear in.',
resolve(human) {
// This query is similar to the friends query except
// episode appearances are directional (a character
// appears in an episode, but an episode does not
// appear in a character), so we are only interested
// in OUTBOUND edges.
return db._query(aqlQuery`
WITH ${characters}
FOR episode IN OUTBOUND ${human._id} ${appearsIn}
SORT episode._key ASC
RETURN episode
`).toArray();
}
},
homePlanet: {
type: gql.GraphQLString,
description: 'The home planet of the human, or null if unknown.'
}
};
},
interfaces: [characterInterface]
});
// The droid type is largely identical to the human type above.
// The "resolve" functions have to be implemented on the object
// "implementing" the GraphQL interface, so we have to repeat
// the full definition on both humans and droids.
droidType = new gql.GraphQLObjectType({
name: 'Droid',
description: 'A mechanical creature in the Star Wars universe.',
fields() {
return {
id: {
type: new gql.GraphQLNonNull(gql.GraphQLString),
description: 'The id of the droid.',
resolve(droid) {
return droid._key;
}
},
species: {
type: new gql.GraphQLNonNull(speciesType),
description: 'The species of the droid.',
resolve(droid) {
return droid.$type;
}
},
name: {
type: gql.GraphQLString,
description: 'The name of the droid.'
},
friends: {
type: new gql.GraphQLList(characterInterface),
description: 'The friends of the droid, or an empty list if they have none.',
args: {
species: {
type: speciesType,
description: 'The species of the friends.'
}
},
resolve(droid, args) {
const species = args.species || null;
return db._query(aqlQuery`
WITH ${characters}
FOR friend IN ANY ${droid} ${friends}
FILTER !${species} || friend.$type == ${species}
SORT friend._key ASC
RETURN friend
`).toArray();
}
},
appearsIn: {
type: new gql.GraphQLList(episodeType),
description: 'Which movies they appear in.',
resolve(droid) {
return db._query(aqlQuery`
FOR episode IN OUTBOUND ${droid._id} ${appearsIn}
SORT episode._key ASC
RETURN episode
`).toArray();
}
},
primaryFunction: {
type: gql.GraphQLString,
description: 'The primary function of the droid.'
}
};
},
interfaces: [characterInterface]
});
const queryType = new gql.GraphQLObjectType({
name: 'Query',
fields() {
return {
hero: {
type: characterInterface,
args: {
episode: {
description: 'If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode.',
type: gql.GraphQLString
}
},
resolve(root, args) {
return characters.document(
args.episode === 'NewHope' ? '1000' :
args.episode === 'Awakens' ? '2002' : '2001'
);
}
},
human: {
type: humanType,
args: {
id: {
description: 'id of the human',
type: new gql.GraphQLNonNull(gql.GraphQLString)
}
},
resolve(root, args) {
// We're using firstExample to make sure we only
// return documents with the right "$type".
return characters.firstExample({
_key: args.id,
$type: 'human'
});
}
},
droid: {
type: droidType,
args: {
id: {
description: 'id of the droid',
type: new gql.GraphQLNonNull(gql.GraphQLString)
}
},
resolve(root, args) {
return characters.firstExample({
_key: args.id,
$type: 'droid'
});
}
}
};
}
});
// This is the GraphQL schema object we need to execute
// queries. See "controller.js" for an example of how it
// is used. Also see the "test" folder for more in-depth
// examples.
module.exports = new gql.GraphQLSchema({
query: queryType
});