@@ -87,11 +87,9 @@ function types.scalar(config)
87
87
assert (type (config .name ) == ' string' , ' type name must be provided as a string' )
88
88
assert (type (config .serialize ) == ' function' , ' serialize must be a function' )
89
89
assert (type (config .isValueOfTheType ) == ' function' , ' isValueOfTheType must be a function' )
90
- if config .parseValue or config .parseLiteral then
91
- assert (
92
- type (config .parseValue ) == ' function' and type (config .parseLiteral ) == ' function' ,
93
- ' must provide both parseValue and parseLiteral to scalar type'
94
- )
90
+ assert (type (config .parseLiteral ) == ' function' , ' parseLiteral must be a function' )
91
+ if config .parseValue then
92
+ assert (type (config .parseValue ) == ' function' , ' parseValue must be a function' )
95
93
end
96
94
97
95
local instance = {
@@ -260,6 +258,26 @@ local function isInt(value)
260
258
return false
261
259
end
262
260
261
+ local function coerceInt (value )
262
+ if value ~= nil then
263
+ value = tonumber (value )
264
+ if not isInt (value ) then return end
265
+ end
266
+
267
+ return value
268
+ end
269
+
270
+ types .int = types .scalar ({
271
+ name = ' Int' ,
272
+ description = " The `Int` scalar type represents non-fractional signed whole numeric values. " ..
273
+ " Int can represent values from -(2^31) to 2^31 - 1, inclusive." ,
274
+ serialize = coerceInt ,
275
+ parseLiteral = function (node )
276
+ return coerceInt (node .value )
277
+ end ,
278
+ isValueOfTheType = isInt ,
279
+ })
280
+
263
281
-- The code from tarantool/checks.
264
282
local function isLong (value )
265
283
if type (value ) == ' number' then
@@ -279,101 +297,97 @@ local function isLong(value)
279
297
return false
280
298
end
281
299
282
- local function coerceInt (value )
283
- local value = tonumber (value )
284
-
285
- if value == nil then return end
286
- if not isInt (value ) then return end
287
-
288
- return value
289
- end
290
-
291
300
local function coerceLong (value )
292
- local value = tonumber64 ( value )
293
-
294
- if value == nil then return end
295
- if not isLong ( value ) then return end
301
+ if value ~= nil then
302
+ value = tonumber64 ( value )
303
+ if not isLong ( value ) then return end
304
+ end
296
305
297
306
return value
298
307
end
299
308
300
- types .int = types .scalar ({
301
- name = ' Int' ,
302
- description = " The `Int` scalar type represents non-fractional signed whole numeric values. " ..
303
- " Int can represent values from -(2^31) to 2^31 - 1, inclusive." ,
304
- serialize = coerceInt ,
305
- parseValue = coerceInt ,
306
- parseLiteral = function (node )
307
- if node .kind == ' int' then
308
- return coerceInt (node .value )
309
- end
310
- end ,
311
- isValueOfTheType = isInt ,
312
- })
313
-
314
309
types .long = types .scalar ({
315
310
name = ' Long' ,
316
311
description = " The `Long` scalar type represents non-fractional signed whole numeric values. " ..
317
312
" Long can represent values from -(2^52) to 2^52 - 1, inclusive." ,
318
313
serialize = coerceLong ,
319
- parseValue = coerceLong ,
320
314
parseLiteral = function (node )
321
- if node .kind == ' long' or node .kind == ' int' then
322
- return coerceLong (node .value )
323
- end
315
+ return coerceLong (node .value )
324
316
end ,
325
317
isValueOfTheType = isLong ,
326
318
})
327
319
320
+ local function isFloat (value )
321
+ return type (value ) == ' number'
322
+ end
323
+
324
+ local function coerceFloat (value )
325
+ if value ~= nil then
326
+ value = tonumber (value )
327
+ if not isFloat (value ) then return end
328
+ end
329
+
330
+ return value
331
+ end
332
+
328
333
types .float = types .scalar ({
329
334
name = ' Float' ,
330
- serialize = tonumber ,
331
- parseValue = tonumber ,
335
+ serialize = coerceFloat ,
332
336
parseLiteral = function (node )
333
- if node .kind == ' float' or node .kind == ' int' then
334
- return tonumber (node .value )
335
- end
336
- end ,
337
- isValueOfTheType = function (value )
338
- return type (value ) == ' number'
337
+ return coerceFloat (node .value )
339
338
end ,
339
+ isValueOfTheType = isFloat ,
340
340
})
341
341
342
+ local function isString (value )
343
+ return type (value ) == ' string'
344
+ end
345
+
346
+ local function coerceString (value )
347
+ if value ~= nil then
348
+ value = tostring (value )
349
+ if not isString (value ) then return end
350
+ end
351
+
352
+ return value
353
+ end
354
+
342
355
types .string = types .scalar ({
343
356
name = ' String' ,
344
357
description = " The `String` scalar type represents textual data, represented as UTF-8 character sequences. " ..
345
358
" The String type is most often used by GraphQL to represent free-form human-readable text." ,
346
- serialize = tostring ,
347
- parseValue = tostring ,
359
+ serialize = coerceString ,
348
360
parseLiteral = function (node )
349
- if node .kind == ' string' then
350
- return node .value
351
- end
352
- end ,
353
- isValueOfTheType = function (value )
354
- return type (value ) == ' string'
361
+ return coerceString (node .value )
355
362
end ,
363
+ isValueOfTheType = isString ,
356
364
})
357
365
358
366
local function toboolean (x )
359
367
return (x and x ~= ' false' ) and true or false
360
368
end
361
369
370
+ local function isBoolean (value )
371
+ return type (value ) == ' boolean'
372
+ end
373
+
374
+ local function coerceBoolean (value )
375
+ if value ~= nil then
376
+ value = toboolean (value )
377
+ if not isBoolean (value ) then return end
378
+ end
379
+
380
+ return value
381
+ end
382
+
362
383
types .boolean = types .scalar ({
363
384
name = ' Boolean' ,
364
385
description = " The `Boolean` scalar type represents `true` or `false`." ,
365
- serialize = toboolean ,
366
- parseValue = toboolean ,
386
+ serialize = coerceBoolean ,
367
387
parseLiteral = function (node )
368
- if node .kind == ' boolean' then
369
- return toboolean (node .value )
370
- else
371
- return nil
372
- end
373
- end ,
374
- isValueOfTheType = function (value )
375
- return type (value ) == ' boolean'
388
+ return coerceBoolean (node .value )
376
389
end ,
390
+ isValueOfTheType = isBoolean ,
377
391
})
378
392
379
393
--[[
@@ -384,14 +398,11 @@ however, defining it as an ID signifies that it is not intended to be human‐re
384
398
--]]
385
399
types .id = types .scalar ({
386
400
name = ' ID' ,
387
- serialize = tostring ,
388
- parseValue = tostring ,
401
+ serialize = coerceString ,
389
402
parseLiteral = function (node )
390
- return node .kind == ' string' or node .kind == ' int' and node .value or nil
391
- end ,
392
- isValueOfTheType = function (value )
393
- return type (value ) == ' string'
403
+ return coerceString (node .value )
394
404
end ,
405
+ isValueOfTheType = isString ,
395
406
})
396
407
397
408
function types .directive (config )
0 commit comments