/
index.js
127 lines (116 loc) · 2.62 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var express = require('express');
var graphqlHTTP = require('express-graphql');
var graphql = require('graphql');
const users = [
{
id: 1,
name : "manish"
},
{
id: 2,
name : "test"
},
{
id: 3,
name : "test2"
}
]
const address = [
{
id: 1,
address : "test",
country: "IN",
phone: "000",
user_id: 1
},
{
id: 2,
address : "test2",
country: "IN",
phone: "2222",
user_id: 1
},
{
id: 3,
address : "test3",
country: "IN",
phone: "3333",
user_id: 2
}
]
const countryEnumType = new graphql.GraphQLEnumType({
name: 'countryEnum',
values: {
IN: {
value: "IN",
},
US: {
value: "US",
}
},
});
var addressType = new graphql.GraphQLObjectType({
name: "address",
fields: {
id: { type: graphql.GraphQLID },
address: { type: graphql.GraphQLString },
country: { type: countryEnumType },
phone: { type: graphql.GraphQLString }
}
})
var profileType = new graphql.GraphQLObjectType({
name: 'Profile',
fields: {
id: {
type: graphql.GraphQLID
},
name: {
type: graphql.GraphQLString
},
address: {
type: graphql.GraphQLNonNull(new graphql.GraphQLList(addressType)),
resolve: () => {
return [{
id: "1",
address: "Noida",
country: "IN",
phone: "99999"
}]
}
}
}
})
var queryType = new graphql.GraphQLObjectType({
name: 'Query',
fields: {
hello: {
type: graphql.GraphQLString,
resolve: (_) => {
return "test"
}
},
number: {
type: graphql.GraphQLInt,
resolve: () => {
return Math.round(Math.random() * 100, 0)
}
},
profile: {
type: profileType,
resolve: () => {
return {
id: "1",
name: "Manish"
}
}
}
}
});
var schema = new graphql.GraphQLSchema({ query: queryType });
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
graphiql: true,
}));
app.listen(4000);
console.log('Running a GraphQL API server at localhost:4000/graphql');