-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.js
More file actions
231 lines (216 loc) · 5.61 KB
/
schema.js
File metadata and controls
231 lines (216 loc) · 5.61 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import {
graphql,
GraphQLObjectType,
GraphQLNonNull,
GraphQLSchema,
GraphQLString,
GraphQLInt,
GraphQLList,
GraphQLScalarType,
GraphQLID
} from 'graphql';
import { GraphQLError } from 'graphql/error';
import { Kind } from 'graphql/language';
import co from 'co';
import Employee from './models/employee';
/**
* generate projection object for mongoose
* @param {Object} fieldASTs
* @return {Project}
*/
function getProjection (fieldASTs) {
return fieldASTs.selectionSet.selections.reduce((projections, selection) => {
projections[selection.name.value] = 1;
return projections;
}, {});
}
var ValidateStringType = (params) => {
return new GraphQLScalarType({
name: params.name,
serialize: value => {
return value;
},
parseValue: value => {
return value;
},
parseLiteral: ast => {
console.log(ast);
if (ast.kind !== Kind.STRING) {
throw new GraphQLError("Query error: Can only parse strings got a: " + ast.kind, [ast]);
}
if (ast.value.length < params.min) {
throw new GraphQLError(`Query error: minimum length of ${params.min} required: `, [ast]);
}
if (ast.value.length > params.max){
throw new GraphQLError(`Query error: maximum length is ${params.max}: `, [ast]);
}
if(params.regex !== null) {
if(!params.regex.test(ast.value)) {
throw new GraphQLError(`Query error: Not a valid ${params.name}: `, [ast]);
}
}
return ast.value;
}
})
};
var EmailType = ValidateStringType({
name: 'Email',
min: 4,
max: 254,
regex: /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
});
var employeeType = new GraphQLObjectType({
name: 'Employee',
description: 'Employee creator',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLID),
description: 'The id of the employee.',
},
firstName: {
type: new GraphQLNonNull(GraphQLString),
description: 'The first name of the employee.',
},
lastName: {
type: new GraphQLNonNull(GraphQLString),
description: 'The last name of the employee.',
},
title: {
type: GraphQLString,
description: 'The title of the employee',
},
department: {
type: GraphQLString,
description: 'The department name of the employee',
},
mobilePhone: {
type: GraphQLString,
description: 'The mobile phone number of the employee',
},
officePhone: {
type: GraphQLString,
description: 'The office phone number of the employee',
},
email: {
type: GraphQLString,
description: 'The email address of the employee',
},
city: {
type: GraphQLString,
description: 'The city of the employee',
},
pic: {
type: GraphQLString,
description: 'The url of the employee\'s picture',
},
twitterId: {
type: GraphQLString,
description: 'The twitter Id of the employee',
},
blog: {
type: GraphQLString,
description: 'The blog url of the employee',
},
reports: {
type: GraphQLInt,
description: 'The number of people report to the employee',
},
manager: {
type: employeeType,
description: 'The manager of the employee, or null if they have none.',
resolve: (user, params, source, fieldASTs) => {
var projections = getProjection(fieldASTs);
return Employee.findOne({id: user.manager}, projections);
},
}
})
});
var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve: function() {
return 'world';
}
},
employee: {
type: employeeType,
args: {
id: {
name: 'id',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: (root, {id}, source, fieldASTs) => {
var projections = getProjection(fieldASTs);
console.log(projections);
return Employee.findOne({id: id}, projections);
}
}
}
}),
// mutation
/*
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: {
createUser: {
type: userType,
args: {
name: {
name: 'name',
type: GraphQLString
}
},
resolve: (obj, {name}, source, fieldASTs) => co(function *() {
var projections = getProjection(fieldASTs);
var user = new User();
user.name = name;
return yield user.save();
})
},
deleteUser: {
type: userType,
args: {
id: {
name: 'id',
type: new GraphQLNonNull(GraphQLString)
}
},
resolve: (obj, {id}, source, fieldASTs) => co(function *() {
var projections = getProjection(fieldASTs);
console.log(id);
return yield User.findOneAndRemove({_id: id});
})
},
updateUser: {
type: userType,
args: {
id: {
name: 'id',
type: new GraphQLNonNull(GraphQLString)
},
name: {
name: 'name',
type: GraphQLString
}
},
resolve: (obj, {id, name}, source, fieldASTs) => co(function *() {
var projections = getProjection(fieldASTs);
yield User.update({
_id: id
}, {
$set: {
name: name
}
});
return yield User.findById(id, projections);
})
}
}
}) */
});
export var getProjection;
export default schema;