- Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathserver.js
More file actions
Latest commit
78 lines (73 loc) · 2.14 KB
/
Copy pathserver.js
File metadata and controls
78 lines (73 loc) · 2.14 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
constexpress=require("express");
constapp=express();
constPORT=5000;
constuserData=require("./MOCK_DATA.json");
constgraphql=require("graphql")
const{ GraphQLObjectType, GraphQLSchema, GraphQLList, GraphQLID, GraphQLInt, GraphQLString }=graphql
const{ graphqlHTTP }=require("express-graphql")
constUserType=newGraphQLObjectType({
name: "User",
fields: ()=>({
id: {type: GraphQLInt},
firstName: {type: GraphQLString},
lastName: {type: GraphQLString},
email: {type: GraphQLString},
password: {type: GraphQLString},
})
})
constRootQuery=newGraphQLObjectType({
name: "RootQueryType",
fields: {
getAllUsers: {
type: newGraphQLList(UserType),
args: {id: {type: GraphQLInt}},
resolve(parent,args){
returnuserData;
}
},
findUserById: {
type: UserType,
description: "fetch single user",
args: {id: {type: GraphQLInt}},
resolve(parent,args){
returnuserData.find((a)=>a.id==args.id);
}
}
}
})
constMutation=newGraphQLObjectType({
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
})
returnargs
}
}
}
})
constschema=newGraphQLSchema({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");
});