-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
86 lines (76 loc) · 1.55 KB
/
app.js
File metadata and controls
86 lines (76 loc) · 1.55 KB
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
var express = require('express');
var graphqlHTTP = require('express-graphql');
var { buildSchema, GraphQLObjectType } = require('graphql');
// Using https://www.digitalocean.com/community/tutorials/a-practical-graphql-getting-started-guide-with-nodejs as a rough template
var schema = buildSchema(`
type Query {
hello: String
user(id: Int!): Person
parents(child: Int!): [Person]
},
type Person {
id: Int
name: String
age: Int
children: [Person]
}
`);
// Root resolver
var root = {
user: getUser,
parents: getParents
};
function getUser(args) {
var userID = args.id;
const user = users.filter(user => user.id == userID)[0];
var copy = JSON.parse(JSON.stringify(user));
if (user && copy.children && copy.children.length > 0) {
var children = copy.children.map((id) => {
return getUser({id: id});
});
copy.children = children;
}
return copy;
}
function getParents(args) {
return users.filter(user => user.children.indexOf(args.id) !== -1);
}
var users = [
{
id: 1,
name: 'Brian',
age: '21',
children: [3]
},
{
id: 2,
name: 'Kim',
age: '22',
children: []
},
{
id: 3,
name: 'Faith',
age: '23',
children: []
},
{
id: 4,
name: 'Joseph',
age: '23',
children: []
},
{
id: 5,
name: 'Joy',
age: '25',
children: []
}
];
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.listen(4000, () => console.log('Now browse to localhost:4000/graphql'));