fix some bug and produce many problem to solve later

This commit is contained in:
2024-11-02 16:27:28 +07:00
parent e86d979ddb
commit 1a5577f8e6
15 changed files with 751 additions and 509 deletions

View File

@@ -22,6 +22,9 @@ import SchemaBuilder from '@pothos/core'
import SimpleObjectPlugin from '@pothos/plugin-simple-objects'
import { User } from '@prisma/client'
import { getDatamodel } from '../types/pothos.generated'
import { DateTime } from 'luxon'
import { Kind } from 'graphql'
import { DateTimeUtils } from '../common/utils/datetime.utils'
// import { rules } from '../common/graphql/common.graphql.auth-rule';
@@ -29,6 +32,7 @@ export type SchemaContext =
| {
isSubscription: true
websocket: {
req: Request
pubSub: PubSub
me: User
generator: PrismaCrudGenerator<BuilderTypes>
@@ -57,8 +61,8 @@ export interface SchemaBuilderOption {
// AuthZRule: keyof typeof rules;
Scalars: {
DateTime: {
Input: Date
Output: Date
Input: string | DateTime | Date
Output: string | DateTime | Date
}
Json: {
Input: JSON
@@ -112,7 +116,36 @@ export class Builder extends SchemaBuilder<SchemaBuilderOption> {
},
})
this.generator = new PrismaCrudGenerator<BuilderTypes>(this)
this.addScalarType('DateTime', DateTimeResolver)
this.scalarType('DateTime', {
serialize: (value) => {
// Serialize outgoing DateTime to ISO string
if (typeof value === 'string') {
return value
}
if (typeof value === 'object' && value !== null && 'toISO' in value) {
return value
}
// if value = Date, convert to DateTime
if (value instanceof Date) {
return DateTimeUtils.toIsoString(DateTimeUtils.fromDate(value))
}
throw new Error('Invalid DateTime')
},
parseValue: (value) => {
// Parse incoming ISO string to Luxon DateTime
if (typeof value === 'string') {
return DateTimeUtils.fromIsoString(value)
}
throw new Error('Invalid DateTime')
},
parseLiteral: (ast) => {
// parse string to DateTime
if (ast.kind === Kind.STRING) {
return DateTimeUtils.fromIsoString(ast.value)
}
throw new Error('Invalid DateTime')
},
})
this.addScalarType('Json', JSONObjectResolver)
this.addScalarType('Upload', GraphQLUpload)