2021-07-05 10:43:36 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { GraphQLScalarType } = require('graphql');
|
|
|
|
const { Kind } = require('graphql');
|
|
|
|
const { parseType } = require('@strapi/utils');
|
2021-10-27 18:54:58 +02:00
|
|
|
const { ValidationError } = require('@strapi/utils').errors;
|
2021-07-05 10:43:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A GraphQL scalar used to store Time (HH:mm:ss.SSS) values
|
|
|
|
* @type {GraphQLScalarType}
|
|
|
|
*/
|
|
|
|
const TimeScalar = new GraphQLScalarType({
|
|
|
|
name: 'Time',
|
|
|
|
|
|
|
|
description: 'A time string with format HH:mm:ss.SSS',
|
|
|
|
|
|
|
|
serialize(value) {
|
|
|
|
return parseType({ type: 'time', value });
|
|
|
|
},
|
|
|
|
|
|
|
|
parseValue(value) {
|
|
|
|
return parseType({ type: 'time', value });
|
|
|
|
},
|
|
|
|
|
|
|
|
parseLiteral(ast) {
|
|
|
|
if (ast.kind !== Kind.STRING) {
|
2021-10-27 18:54:58 +02:00
|
|
|
throw new ValidationError('Time cannot represent non string type');
|
2021-07-05 10:43:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const value = ast.value;
|
|
|
|
|
|
|
|
return parseType({ type: 'time', value });
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = TimeScalar;
|