-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (87 loc) · 1.86 KB
/
index.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
var express = require("express");
var graphqlHTTP = require("express-graphql");
var {buildSchema} = require("graphql");
const emp = [
{
id: "1",
firstName: "John",
lastName: "Wire"
}, {
id: "2",
firstName: "Jem",
lastName: "Will"
}, {
id: "3",
firstName: "Jill",
lastName: "Smith"
}, {
id: "4",
firstName: "Jul",
lastName: "Tan"
}
]
const addr = [
{
id: "1",
street: "Lake street",
city: "Dream City"
}, {
id: "2",
street: "River street",
city: "Dream City"
}, {
id: "3",
street: "Sea street",
city: "Dream City"
}, {
id: "4",
street: "No street",
city: "Dream City"
}
]
const findEmp = (field, value) => (emp.filter(emp => emp[field] === value));
const findAddr = (field, value) => (addr.filter(addr => addr[field] === value));
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
type Employ {
firstName: String!
lastName: String!
street: String!
city: String!
}
type Query {
getEmploy(id: String!): Employ
}
`);
// The root provides the top-level API endpoints
var root = {
getEmploy: function({id}) {
var emp1 = findEmp("id", id);
var addr1 = findAddr("id", id)
return new Employ(emp1[0].firstName, emp1[0].lastName, addr1[0].street, addr1[0].city );
}
}
class Employ{
constructor(firstName, lastName, street, city) {
this.firstName = firstName;
this.lastName = lastName;
this.street = street;
this.city = city;
}
firstName(){
return this.firstName;
}
lastName(){
return this.lastName;
}
street(){
return this.street;
}
city(){
return this.city;
}
}
var app = express();
app.use("/graphql", graphqlHTTP({schema: schema, rootValue: root, graphiql: true}));
app.listen(4000);
console.log("Running a GraphQL API server at localhost:4000/graphql");