-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
249 lines (214 loc) · 7.17 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const typeOf = require('just-typeof')
const assert = require('assert')
const { GraphQLScalarType } = require('graphql')
const { GraphQLError } = require('graphql/error')
// To which direction the assertion will be applied, on input or output
const DIRECTION_INPUT = 'input'
const DIRECTION_OUTPUT = 'output'
const DIRECTION_BOTH = 'both'
class GraphQLTypeError extends GraphQLError {
}
const isFloat = (n) => parseFloat(n) === n && /^\d+\.\d+$/.test(String(n))
const assertString = (value, attrs) => {
const { name, validate, validationMessages } = attrs
const { min, max, regexp, test } = validate || {}
const defaultMessages = {
type: `Expecting "${name}" to be a string but ${typeOf(value)} was passed.`,
min: `Minimum length for "${name}" is ${min}.`,
max: `Maximum length for "${name}" is ${max}.`,
regexp: `"${name}" is invalid.`,
test: `"${name}" is invalid.`
}
const messages = Object.assign(defaultMessages, validationMessages)
if (typeof value !== 'string') {
throw new GraphQLTypeError(messages.type)
}
if (min && value.length < min) {
throw new GraphQLTypeError(messages.min)
}
if (max && value.length > max) {
throw new GraphQLTypeError(messages.max)
}
if (regexp && !regexp.test(value)) {
throw new GraphQLTypeError(messages.regexp)
}
if (test && !test(value)) {
throw new GraphQLTypeError(messages.test)
}
}
const assertInt = (value, attrs) => {
const { name, validate, validationMessages } = attrs
const { min, max, test } = validate || {}
const defaultMessages = {
type: `Expecting "${name}" to be an integer but ${typeOf(value)} was passed.`,
min: `Minimum number for "${name}" is ${min}.`,
max: `Maximum number for "${name}" is ${max}.`,
test: `"${name}" is invalid.`
}
const messages = Object.assign(defaultMessages, validationMessages)
value = String(value)
if (!Number.isInteger(Number(value))) {
throw new GraphQLTypeError(messages.type)
}
if (min && value < min) {
throw new GraphQLTypeError(messages.min)
}
if (max && value > max) {
throw new GraphQLTypeError(messages.max)
}
if (test && !test(value)) {
throw new GraphQLTypeError(messages.test)
}
}
const assertFloat = (value, attrs) => {
const { name, validate, validationMessages } = attrs
const { min, max, maxDecimals, test } = validate || {}
const defaultMessages = {
type: `Expecting "${name}" to be a float but ${typeOf(value)} was passed.`,
min: `Minimum number for "${name}" is ${min}.`,
max: `Maximum number for "${name}" is ${max}.`,
maxDecimals: `The float number "${name}" should not exceed ${maxDecimals} decimals.`,
test: `"${name}" is invalid.`
}
const messages = Object.assign(defaultMessages, validationMessages)
if (!isFloat(Number(value))) {
throw new GraphQLTypeError(messages.type)
}
value = String(value)
const decimals = value.split('.')[1]
if (min && value < min) {
throw new GraphQLTypeError(messages.min)
}
if (max && value > max) {
throw new GraphQLTypeError(messages.max)
}
if (maxDecimals && decimals.length > maxDecimals) {
throw new GraphQLTypeError(messages.maxDecimals)
}
if (test && !test(value)) {
throw new GraphQLTypeError(messages.test)
}
}
const createScalarType = (attrs, handler, direction = DIRECTION_BOTH) => {
return new GraphQLScalarType({
name: attrs.name,
description: attrs.description,
// Gets invoked when serializing the result to send it back to the client.
serialize (value) {
if (direction === DIRECTION_OUTPUT || direction === DIRECTION_BOTH) {
try {
return handler(value, attrs)
} catch (e) {
console.error(e)
}
}
// Always push forward the output, even on error
return value
},
// Gets invoked to parse client input that was passed through variables.
parseValue (value) {
if (direction === DIRECTION_INPUT || direction === DIRECTION_BOTH) {
return handler(value, attrs)
}
throw new GraphQLTypeError(`"${attrs.name}" type must be Output Type but got input.`)
},
// Gets invoked to parse client input that was passed inline in the query.
parseLiteral (ast) {
if (direction === DIRECTION_INPUT || direction === DIRECTION_BOTH) {
return handler(ast.value, attrs, ast)
}
throw new GraphQLTypeError(`"${attrs.name}" type must be Output Type but got input.`)
}
})
}
/**
* Asserts root attributes passed to the type creator function.
*/
const assertRootAttrs = (attrs) => {
const allowedAttrs = ['name', 'description', 'validate', 'validationMessages', 'resolve']
assert(!!attrs.name, 'Must provide name.')
Object.keys(attrs).forEach(attr => {
assert(allowedAttrs.includes(attr),
`Unknown attribute "${attr}" set for "${attrs.name}" type.`)
})
}
//
// String
//
const stringTypeHandler = (attrs) => {
assertRootAttrs(attrs)
return (value) => {
assertString(value, attrs)
if (typeof attrs.resolve === 'function') {
return attrs.resolve(value)
}
return value
}
}
const createStringType = (attrs) => {
return createScalarType(attrs, stringTypeHandler(attrs), DIRECTION_BOTH)
}
const createStringInputType = (attrs) => {
return createScalarType(attrs, stringTypeHandler(attrs), DIRECTION_INPUT)
}
const createStringOutputType = (attrs) => {
return createScalarType(attrs, stringTypeHandler(attrs), DIRECTION_OUTPUT)
}
//
// Int
//
const intTypeHandler = (attrs) => {
assertRootAttrs(attrs)
return (value) => {
assertInt(value, attrs)
if (typeof attrs.resolve === 'function') {
return attrs.resolve(value)
}
return Number(value)
}
}
const createIntType = (attrs) => {
return createScalarType(attrs, intTypeHandler(attrs), DIRECTION_BOTH)
}
const createIntInputType = (attrs) => {
return createScalarType(attrs, intTypeHandler(attrs), DIRECTION_INPUT)
}
const createIntOutputType = (attrs) => {
return createScalarType(attrs, intTypeHandler(attrs), DIRECTION_OUTPUT)
}
//
// Float
//
const floatTypeHandler = (attrs) => {
assertRootAttrs(attrs)
return (value) => {
assertFloat(value, attrs)
if (typeof attrs.resolve === 'function') {
return attrs.resolve(value)
}
return Number(value)
}
}
const createFloatType = (attrs) => {
return createScalarType(attrs, floatTypeHandler(attrs), DIRECTION_BOTH)
}
const createFloatInputType = (attrs) => {
return createScalarType(attrs, floatTypeHandler(attrs), DIRECTION_INPUT)
}
const createFloatOutputType = (attrs) => {
return createScalarType(attrs, floatTypeHandler(attrs), DIRECTION_OUTPUT)
}
exports.DIRECTION_INPUT = DIRECTION_INPUT
exports.DIRECTION_OUTPUT = DIRECTION_OUTPUT
exports.DIRECTION_BOTH = DIRECTION_BOTH
exports.GraphQLTypeError = GraphQLTypeError
exports.createScalarType = createScalarType
exports.createStringType = createStringType
exports.createStringInputType = createStringInputType
exports.createStringOutputType = createStringOutputType
exports.createIntType = createIntType
exports.createIntInputType = createIntInputType
exports.createIntOutputType = createIntOutputType
exports.createFloatType = createFloatType
exports.createFloatInputType = createFloatInputType
exports.createFloatOutputType = createFloatOutputType