-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
191 lines (180 loc) · 4.99 KB
/
app.js
File metadata and controls
191 lines (180 loc) · 4.99 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const express = require('express');
const graphql = require('graphql');
const graphqlHTTP = require('express-graphql');
const app = express();
const mongoose = require('mongoose');
mongoose.set('useFindAndModify', false);
mongoose.set('useNewUrlParser', true);
mongoose.set('useCreateIndex', true);
mongoose.set('useUnifiedTopology', true);
const mongoURI = 'mongodb://localhost:27017/FridayDevDB'
var conn = mongoose.createConnection(mongoURI);
const Schema = mongoose.Schema;
const templateSchema = new Schema({
name: String,
content: String,
authorID: String
});
Template = conn.model('Template',templateSchema);
const authorSchema = new Schema({
NTID: String
});
Author = conn.model('Author', authorSchema);
const {
GraphQLObjectType, GraphQLString,
GraphQLID, GraphQLInt,GraphQLSchema,
GraphQLList,GraphQLNonNull
} = graphql;
const TemplateType = new GraphQLObjectType({
name:"TT",
fields: () => ({
_id: { type: GraphQLID },
name: { type: GraphQLString },
content:{ type: GraphQLString },
authorID: { type: GraphQLString }
})
});
const AuthorType = new GraphQLObjectType({
name: 'AT',
fields: () => ({
_id: { type: GraphQLID },
NTID: { type: GraphQLString },
templates:{
type: new GraphQLList(TemplateType),
resolve(parent, args){
return Template.find({authorID: parent.NTID});
}
}
})
});
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
template: {
type: TemplateType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
return Template.findById(args.id);
}
},
templates:{
type: new GraphQLList(TemplateType),
resolve(parent, args) {
return Template.find({});
}
},
author:{
type: AuthorType,
args: { NTID: { type: GraphQLID } },
resolve(parent, args) {
return Author.findOne({NTID:args.NTID});
}
},
authors:{
type: new GraphQLList(AuthorType),
resolve(parent, args) {
return Author.find({});
}
}
}
});
const Mutation = new GraphQLObjectType({
name: 'Mutation',
fields: {
addAuthor: {
type: AuthorType,
args: {
NTID: { type: new GraphQLNonNull(GraphQLString) },
},
resolve(parent, args) {
let author = new Author({
NTID: args.NTID,
});
return author.save();
}
},
addTemplate:{
type:TemplateType,
args:{
name: { type: new GraphQLNonNull(GraphQLString)},
content: { type: new GraphQLNonNull(GraphQLString)},
authorID: { type: new GraphQLNonNull(GraphQLString)}
},
resolve(parent,args){
let template = new Template({
name:args.name,
content:args.content,
authorID:args.authorID
});
return template.save();
}
},
updateTemplate:{
type:TemplateType,
args:{
id: { type: new GraphQLNonNull(GraphQLID) },
name: { type: GraphQLString },
content: { type: GraphQLString },
authorID: { type: GraphQLString }
},
resolve(parent,args){
return Template.findByIdAndUpdate(args.id,args);
}
},
delTemplate:{
type:TemplateType,
args:{
id: { type: new GraphQLNonNull(GraphQLID)},
},
resolve(parent,args){
return Template.findByIdAndDelete(args.id);
}
}
}
});
/*
updateTemplate:{
type:TemplateType,
args:{
id: { type: new GraphQLNonNull(GraphQLID) },
name: { type: GraphQLString },
pages: { type: GraphQLInt },
authorID: { type: GraphQLID }
},
resolve(parent,args){
return Book.findOneAndUpdate({id:args.id},args);
}
},
delBook:{
type:BookType,
args:{
id: { type: new GraphQLNonNull(GraphQLID)},
},
resolve(parent,args){
return Book.findOneAndDelete({id:args.id});
}
}
}
*/
schema = new GraphQLSchema({
query: RootQuery,
mutation:Mutation
});
const cors=require('cors');
var corsOptions = {
origin: function(origin, callback) {
console.log(origin);
callback(null, true);
}
}
const corsHandler = cors(corsOptions);
app.use(corsHandler);
app.options("*", corsHandler);
var endpoint='FridayDB';
app.use('/'+endpoint, graphqlHTTP({
schema,
graphiql:true
}));
app.listen(3001, () => {
console.log('Listening on http://10.176.8.17:3001/'+endpoint);
});