/
infer-graphql-input-fields.js
320 lines (295 loc) · 8.07 KB
/
infer-graphql-input-fields.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// @flow
const {
GraphQLInputObjectType,
GraphQLBoolean,
GraphQLString,
GraphQLFloat,
GraphQLInt,
GraphQLList,
} = require(`graphql`)
const { oneLine } = require(`common-tags`)
const _ = require(`lodash`)
const invariant = require(`invariant`)
const typeOf = require(`type-of`)
const createTypeName = require(`./create-type-name`)
const createKey = require(`./create-key`)
const {
getExampleValues,
extractFieldNames,
isEmptyObjectOrArray,
INVALID_VALUE,
} = require(`./data-tree-utils`)
const { findLinkedNode } = require(`./infer-graphql-type`)
const { getNodesByType } = require(`../db/nodes`)
const is32BitInteger = require(`../utils/is-32-bit-integer`)
import type {
GraphQLInputFieldConfig,
GraphQLInputFieldConfigMap,
} from "graphql/type/definition"
function typeFields(type): GraphQLInputFieldConfigMap {
switch (type) {
case `boolean`:
return {
eq: { type: GraphQLBoolean },
ne: { type: GraphQLBoolean },
in: { type: new GraphQLList(GraphQLBoolean) },
nin: { type: new GraphQLList(GraphQLBoolean) },
}
case `string`:
return {
eq: { type: GraphQLString },
ne: { type: GraphQLString },
regex: { type: GraphQLString },
glob: { type: GraphQLString },
in: { type: new GraphQLList(GraphQLString) },
nin: { type: new GraphQLList(GraphQLString) },
}
case `int`:
return {
eq: { type: GraphQLInt },
ne: { type: GraphQLInt },
gt: { type: GraphQLInt },
gte: { type: GraphQLInt },
lt: { type: GraphQLInt },
lte: { type: GraphQLInt },
in: { type: new GraphQLList(GraphQLInt) },
nin: { type: new GraphQLList(GraphQLInt) },
}
case `float`:
return {
eq: { type: GraphQLFloat },
ne: { type: GraphQLFloat },
gt: { type: GraphQLFloat },
gte: { type: GraphQLFloat },
lt: { type: GraphQLFloat },
lte: { type: GraphQLFloat },
in: { type: new GraphQLList(GraphQLFloat) },
nin: { type: new GraphQLList(GraphQLFloat) },
}
}
return {}
}
function inferGraphQLInputFields({
value,
nodes,
prefix,
}): ?GraphQLInputFieldConfig {
if (value == null || isEmptyObjectOrArray(value)) return null
switch (typeOf(value)) {
case `array`: {
const headValue = value[0]
let headType = typeOf(headValue)
if (headType === `number`)
headType = is32BitInteger(headValue) ? `int` : `float`
// Determine type for in operator.
let inType
switch (headType) {
case `int`:
inType = GraphQLInt
break
case `float`:
inType = GraphQLFloat
break
case `date`:
case `string`:
inType = GraphQLString
break
case `boolean`:
inType = GraphQLBoolean
break
case `array`:
case `object`: {
let inferredField = inferGraphQLInputFields({
value: headValue,
prefix,
nodes,
})
invariant(
inferredField,
`Could not infer graphQL type for value: ${JSON.stringify(
Object.keys(headValue)
)}`
)
inType = inferredField.type
break
}
default:
invariant(
false,
oneLine`
Could not infer an appropriate GraphQL input type
for value: ${headValue} of type ${headType} along path: ${prefix}
`
)
}
let fields
if (headType === `object`) {
fields = {
elemMatch: {
type: inType,
},
}
} else {
fields = {
...typeFields(headType),
in: { type: new GraphQLList(inType) },
}
}
return {
type: new GraphQLInputObjectType({
name: createTypeName(`${prefix}QueryList`),
fields,
}),
}
}
case `boolean`: {
return {
type: new GraphQLInputObjectType({
name: createTypeName(`${prefix}QueryBoolean`),
fields: typeFields(`boolean`),
}),
}
}
case `date`:
case `string`: {
return {
type: new GraphQLInputObjectType({
name: createTypeName(`${prefix}QueryString`),
fields: typeFields(`string`),
}),
}
}
case `object`: {
const fields = inferInputObjectStructureFromNodes({
nodes,
prefix,
exampleValue: value,
}).inferredFields
if (!_.isEmpty(fields)) {
return {
type: new GraphQLInputObjectType({
name: createTypeName(`${prefix}InputObject`),
fields,
}),
}
} else {
return null
}
}
case `number`: {
if (is32BitInteger(value)) {
return {
type: new GraphQLInputObjectType({
name: createTypeName(`${prefix}QueryInteger`),
fields: typeFields(`int`),
}),
}
} else {
return {
type: new GraphQLInputObjectType({
name: createTypeName(`${prefix}QueryFloat`),
fields: typeFields(`float`),
}),
}
}
}
default:
return null
}
}
const EXCLUDE_KEYS = {
parent: 1,
children: 1,
}
type InferInputOptions = {
nodes: Object[],
typeName?: string,
prefix?: string,
exampleValue?: Object,
}
const recursiveOmitBy = (value, fn) => {
if (_.isObject(value)) {
if (_.isPlainObject(value)) {
value = _.omitBy(value, fn)
} else if (_.isArray(value)) {
// don't mutate original value
value = _.clone(value)
}
_.each(value, (v, k) => {
value[k] = recursiveOmitBy(v, fn)
})
if (_.isEmpty(value)) {
// don't return empty objects - gatsby doesn't support these
return null
}
}
return value
}
const linkedNodeCache = {}
export function inferInputObjectStructureFromNodes({
nodes,
typeName = ``,
prefix = ``,
exampleValue = null,
}: InferInputOptions): Object {
const inferredFields = {}
const isRoot = !prefix
prefix = isRoot ? typeName : prefix
if (exampleValue === null) {
// typeName includes "Connection" string, which is not what we want,
// so extract type from first node
exampleValue = getExampleValues({
nodes,
typeName:
nodes && nodes[0] && nodes[0].internal && nodes[0].internal.type,
})
}
_.each(exampleValue, (v, k) => {
let value = v
let key = k
// Remove fields for traversing through nodes as we want to control
// setting traversing up not try to automatically infer them.
if (value === INVALID_VALUE || (isRoot && EXCLUDE_KEYS[key])) return
if (_.includes(key, `___NODE`)) {
// TODO: Union the objects in array
const isArray = _.isArray(value)
const nodeToFind = isArray ? value[0] : value
const linkedNode = findLinkedNode(nodeToFind)
// Fall back if the linked node can't be found. Prevents crashing, and is
// picked up in infer-graphql-type.js with an error that gives context to
// the user about which node is missing
if (!linkedNode) {
return
}
// Get from cache if found, else store into it
if (linkedNodeCache[linkedNode.internal.type]) {
value = linkedNodeCache[linkedNode.internal.type]
} else {
const relatedNodes = getNodesByType(linkedNode.internal.type)
value = getExampleValues({
nodes: relatedNodes,
typeName: linkedNode.internal.type,
})
value = recursiveOmitBy(value, (_v, _k) => _.includes(_k, `___NODE`))
linkedNodeCache[linkedNode.internal.type] = value
}
if (isArray) {
value = [value]
}
;[key] = key.split(`___`)
}
let field = inferGraphQLInputFields({
nodes,
value,
prefix: `${prefix}${_.upperFirst(key)}`,
})
if (field === null) return
inferredFields[createKey(key)] = field
})
// Add sorting (but only to the top level).
let sort = []
if (typeName) {
sort = extractFieldNames(nodes)
}
return { inferredFields, sort }
}