-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (69 loc) · 1.99 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
const express = require("express");
const app = express();
const PORT = 6969;
const userData = require('./MOCK_DATA.json');
//import the graphQL requirements.
const graphql = require('graphql');
const {GraphQLObjectType, GraphQLSchema, GraphQLInt, GraphQLString, GraphQLList} = 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:{
//create your queries here...
getAllUsers: {
type: new GraphQLList(UserType),
args: {id: {type: GraphQLInt }},
resolve(parent, args){
return userData
}
}
//more queries here..
}
});
const Mutation = new GraphQLObjectType({
name:"Mutation",
fields:{
createUser: {
type: UserType,
args: {
firstName: {type:GraphQLString},
lastName: {type:GraphQLString},
email: {type:GraphQLString},
password: {type:GraphQLString}
},
resolve(parents, args){
//database login to insert to dbase...
userData.push({
id:userData.length + 1,
firstname: args.firstName,
lastName:args.lastName,
email: args.email,
password: args.password
});
return args
}
}
}
})
const schema = new graphql.GraphQLSchema({
query: RootQuery,
mutation: Mutation
})
//config the grapQL.
app.use('/graphql', graphqlHTTP({
schema,
graphiql:true
}));
app.listen(PORT, ()=> {
console.log('server running..');
})