forked from confuser/graphql-constraint-directive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (138 loc) · 4.56 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
const {
GraphQLFloat,
GraphQLInt,
GraphQLString,
GraphQLNonNull,
isNonNullType,
isScalarType,
GraphQLList,
isListType
} = require('graphql')
const { getDirectives, mapSchema, MapperKind } = require('@graphql-tools/utils')
const ConstraintStringType = require('./scalars/string')
const ConstraintNumberType = require('./scalars/number')
function constraintDirective () {
const constraintTypes = {}
function getConstraintType (fieldName, type, notNull, directiveArgumentMap, list, listNotNull) {
// Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ as per graphql-js
let uniqueTypeName
if (directiveArgumentMap.uniqueTypeName) {
uniqueTypeName = directiveArgumentMap.uniqueTypeName.replace(/\W/g, '')
} else {
uniqueTypeName =
`${fieldName}_${list ? 'List_' : ''}${listNotNull ? 'ListNotNull_' : ''}${
type.name
}_${notNull ? 'NotNull_' : ''}` +
Object.entries(directiveArgumentMap)
.map(([key, value]) => {
if (
key === 'min' ||
key === 'max' ||
key === 'exclusiveMin' ||
key === 'exclusiveMax' ||
key === 'multipleOf'
) {
return `${key}_${value.toString().replace(/\W/g, 'dot')}`
}
return `${key}_${value.toString().replace(/\W/g, '')}`
})
.join('_')
}
const key = Symbol.for(uniqueTypeName)
let constraintType = constraintTypes[key]
if (constraintType) return constraintType
if (type === GraphQLString) {
if (notNull) {
constraintType = new GraphQLNonNull(
new ConstraintStringType(fieldName, uniqueTypeName, type, directiveArgumentMap)
)
} else {
constraintType = new ConstraintStringType(
fieldName,
uniqueTypeName,
type,
directiveArgumentMap
)
}
} else if (type === GraphQLFloat || type === GraphQLInt) {
if (notNull) {
constraintType = new GraphQLNonNull(
new ConstraintNumberType(fieldName, uniqueTypeName, type, directiveArgumentMap)
)
} else {
constraintType = new ConstraintNumberType(
fieldName,
uniqueTypeName,
type,
directiveArgumentMap
)
}
} else {
throw new Error(`Not a valid scalar type: ${type.toString()}`)
}
if (list) {
constraintType = new GraphQLList(constraintType)
if (listNotNull) {
constraintType = new GraphQLNonNull(constraintType)
}
}
constraintTypes[key] = constraintType
return constraintType
}
function getScalarType (fieldConfig) {
if (isScalarType(fieldConfig)) {
return { scalarType: fieldConfig }
} else if (isListType(fieldConfig)) {
return { ...getScalarType(fieldConfig.ofType), list: true }
} else if (isNonNullType(fieldConfig) && isScalarType(fieldConfig.ofType)) {
return { scalarType: fieldConfig.ofType, scalarNotNull: true }
} else if (isNonNullType(fieldConfig)) {
return { ...getScalarType(fieldConfig.ofType.ofType), list: true, listNotNull: true }
} else {
throw new Error(`Not a valid scalar type: ${fieldConfig.toString()}`)
}
}
function wrapType (fieldConfig, directiveArgumentMap) {
const result = getScalarType(fieldConfig.type)
const fieldName = fieldConfig.astNode.name.value
fieldConfig.type = getConstraintType(
fieldName,
result.scalarType,
result.scalarNotNull,
directiveArgumentMap,
result.list,
result.listNotNull
)
}
return (schema) =>
mapSchema(schema, {
[MapperKind.FIELD]: (fieldConfig) => {
const directives = getDirectives(schema, fieldConfig)
const directiveArgumentMap = directives.constraint
if (directiveArgumentMap) {
wrapType(fieldConfig, directiveArgumentMap)
return fieldConfig
}
}
})
}
const constraintDirectiveTypeDefs = `
directive @constraint(
# String constraints
minLength: Int
maxLength: Int
startsWith: String
endsWith: String
contains: String
notContains: String
pattern: String
format: String
# Number constraints
min: Float
max: Float
exclusiveMin: Float
exclusiveMax: Float
multipleOf: Float
uniqueTypeName: String
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION`
module.exports = { constraintDirective, constraintDirectiveTypeDefs }