refactor: improve DateTime handling in GraphQL builder
- Streamlined DateTime serialization and parsing logic in the GraphQL builder for enhanced clarity and maintainability. - Removed commented-out code to reduce clutter and improve code readability. - Ensured consistent error handling for invalid DateTime values.
This commit is contained in:
@@ -130,28 +130,24 @@ export class Builder extends SchemaBuilder<SchemaBuilderOption> {
|
|||||||
this.generator = new PrismaCrudGenerator<BuilderTypes>(this)
|
this.generator = new PrismaCrudGenerator<BuilderTypes>(this)
|
||||||
this.scalarType('DateTime', {
|
this.scalarType('DateTime', {
|
||||||
serialize: (value) => {
|
serialize: (value) => {
|
||||||
// Serialize outgoing DateTime to ISO string
|
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
if (typeof value === 'object' && value !== null && 'toISO' in value) {
|
if (typeof value === 'object' && value !== null && 'toISO' in value) {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
// if value = Date, convert to DateTime
|
|
||||||
if (value instanceof Date) {
|
if (value instanceof Date) {
|
||||||
return DateTimeUtils.toIsoString(DateTimeUtils.fromDate(value))
|
return DateTimeUtils.toIsoString(DateTimeUtils.fromDate(value))
|
||||||
}
|
}
|
||||||
throw new Error('Invalid DateTime')
|
throw new Error('Invalid DateTime')
|
||||||
},
|
},
|
||||||
parseValue: (value) => {
|
parseValue: (value) => {
|
||||||
// Parse incoming ISO string to Luxon DateTime
|
|
||||||
if (typeof value === 'string') {
|
if (typeof value === 'string') {
|
||||||
return DateTimeUtils.fromIsoString(value)
|
return DateTimeUtils.fromIsoString(value)
|
||||||
}
|
}
|
||||||
throw new Error('Invalid DateTime')
|
throw new Error('Invalid DateTime')
|
||||||
},
|
},
|
||||||
parseLiteral: (ast) => {
|
parseLiteral: (ast) => {
|
||||||
// parse string to DateTime
|
|
||||||
if (ast.kind === Kind.STRING) {
|
if (ast.kind === Kind.STRING) {
|
||||||
return DateTimeUtils.fromIsoString(ast.value)
|
return DateTimeUtils.fromIsoString(ast.value)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user