/
number.js
39 lines (34 loc) · 1.19 KB
/
number.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
const { GraphQLScalarType } = require('graphql')
const { GraphQLError } = require('graphql/error')
module.exports = class ConstraintNumberType extends GraphQLScalarType {
constructor (type, args) {
super({
name: `ConstraintNumber`,
serialize (value) {
return type.serialize(value)
},
parseValue (value) {
value = type.serialize(value)
if (args.min && value < args.min) {
throw new GraphQLError(`Must be at least ${args.min}`)
}
if (args.max && value > args.max) {
throw new GraphQLError(`Must be no greater than ${args.max}`)
}
if (args.exclusiveMin && value <= args.exclusiveMin) {
throw new GraphQLError(`Must be greater than ${args.exclusiveMin}`)
}
if (args.exclusiveMax && value >= args.exclusiveMax) {
throw new GraphQLError(`Must be no greater than ${args.exclusiveMax}`)
}
if (args.multipleOf && value % args.multipleOf !== 0) {
throw new GraphQLError(`Must be a multiple of ${args.multipleOf}`)
}
return type.parseValue(value)
},
parseLiteral (ast) {
return type.parseLiteral(ast)
}
})
}
}