-
Notifications
You must be signed in to change notification settings - Fork 165
/
server.js
78 lines (73 loc) · 2.14 KB
/
server.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
const express = require("express");
const app = express();
const PORT = 5000;
const userData = require("./MOCK_DATA.json");
const graphql = require("graphql")
const { GraphQLObjectType, GraphQLSchema, GraphQLList, GraphQLID, GraphQLInt, GraphQLString } = graphql
const { graphqlHTTP } = require("express-graphql")
const UserType = new GraphQLObjectType({
name: "User",
fields: () => ({
id: { type: GraphQLInt },
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
email: { type: GraphQLString },
password: { type: GraphQLString },
})
})
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
getAllUsers: {
type: new GraphQLList(UserType),
args: { id: {type: GraphQLInt}},
resolve(parent, args) {
return userData;
}
},
findUserById: {
type: UserType,
description: "fetch single user",
args: { id: {type: GraphQLInt}},
resolve(parent, args) {
return userData.find((a) => a.id == args.id);
}
}
}
})
const Mutation = new GraphQLObjectType({
name: "Mutation",
fields: {
createUser: {
type: UserType,
args: {
firstName: {type: GraphQLString},
lastName: { type: GraphQLString },
email: { type: GraphQLString },
password: { type: GraphQLString },
},
resolve(parent, args) {
userData.push({
id: userData.length + 1,
firstName: args.firstName,
lastName: args.lastName,
email: args.email,
password: args.password
})
return args
}
}
}
})
const schema = new GraphQLSchema({query: RootQuery, mutation: Mutation})
app.use("/graphql", graphqlHTTP({
schema,
graphiql: true,
})
);
app.get("/rest/getAllUsers", (req, res) => {
res.send(userData)
});
app.listen(PORT, () => {
console.log("Server running");
});