backend cung da bi lac

This commit is contained in:
2024-11-15 20:09:07 +07:00
parent f2db004aaf
commit 270d185f18
4 changed files with 35 additions and 11 deletions

View File

@@ -9,6 +9,7 @@ import { Builder, SchemaContext } from '../Graphql/graphql.builder'
import { PrismaService } from '../Prisma/prisma.service' import { PrismaService } from '../Prisma/prisma.service'
import { DocumentEvent } from './document.event' import { DocumentEvent } from './document.event'
import { Document } from '@prisma/client' import { Document } from '@prisma/client'
import { DocumentDelta } from './document.type'
@Injectable() @Injectable()
export class DocumentSchema extends PothosSchema { export class DocumentSchema extends PothosSchema {
constructor( constructor(
@@ -35,6 +36,19 @@ export class DocumentSchema extends PothosSchema {
}) })
} }
@PothosRef()
documentDelta() {
return this.builder.simpleObject('DocumentDelta', {
fields: (t) => ({
pageIndex: t.int(),
documentId: t.string(),
delta: t.field({
type: 'Delta',
}),
}),
})
}
@Pothos() @Pothos()
init(): void { init(): void {
this.builder.queryFields((t) => ({ this.builder.queryFields((t) => ({
@@ -84,7 +98,7 @@ export class DocumentSchema extends PothosSchema {
this.builder.subscriptionFields((t) => ({ this.builder.subscriptionFields((t) => ({
document: t.field({ document: t.field({
type: this.document(), type: this.documentDelta(),
args: { args: {
documentId: t.arg({ documentId: t.arg({
type: 'String', type: 'String',
@@ -101,9 +115,9 @@ export class DocumentSchema extends PothosSchema {
`${DocumentEvent.CREATED}.${args.documentId}`, `${DocumentEvent.CREATED}.${args.documentId}`,
`${DocumentEvent.DELETED}.${args.documentId}`, `${DocumentEvent.DELETED}.${args.documentId}`,
`${DocumentEvent.SAVED}.${args.documentId}`, `${DocumentEvent.SAVED}.${args.documentId}`,
]) as unknown as AsyncIterable<Document> ]) as unknown as AsyncIterable<DocumentDelta>
}, },
resolve: async (payload: Document) => payload, resolve: async (payload: DocumentDelta) => payload,
}), }),
})) }))
} }

View File

@@ -23,9 +23,10 @@ import SimpleObjectPlugin from '@pothos/plugin-simple-objects'
import { User } from '@prisma/client' import { User } from '@prisma/client'
import { getDatamodel } from '../types/pothos.generated' import { getDatamodel } from '../types/pothos.generated'
import { DateTime } from 'luxon' import { DateTime } from 'luxon'
import { Kind } from 'graphql' import { Kind, ValueNode } from 'graphql'
import { DateTimeUtils } from '../common/utils/datetime.utils' import { DateTimeUtils } from '../common/utils/datetime.utils'
import { JsonValue } from '@prisma/client/runtime/library' import { JsonValue } from '@prisma/client/runtime/library'
import Delta from 'quill-delta'
export type SchemaContext = export type SchemaContext =
| { | {
@@ -75,6 +76,10 @@ export interface SchemaBuilderOption {
Input: number Input: number
Output: number | bigint | string Output: number | bigint | string
} }
Delta: {
Input: Delta
Output: Delta
}
} }
} }
@@ -96,11 +101,10 @@ export class Builder extends SchemaBuilder<SchemaBuilderOption> {
], ],
smartSubscriptions: { smartSubscriptions: {
debounceDelay: 1000, debounceDelay: 1000,
...subscribeOptionsFromIterator((name, ctx) => { ...subscribeOptionsFromIterator((name, context) => {
if (ctx.isSubscription) { return context.isSubscription
return ctx.websocket.pubSub.asyncIterator(name) ? context.websocket.pubSub.asyncIterator(name)
} : context.http.pubSub.asyncIterator(name)
return ctx.http.pubSub.asyncIterator(name)
}), }),
}, },
zod: { zod: {
@@ -154,6 +158,12 @@ export class Builder extends SchemaBuilder<SchemaBuilderOption> {
throw new Error('Invalid DateTime') throw new Error('Invalid DateTime')
}, },
}) })
this.scalarType('Delta', {
serialize: (value) => value.toString(),
parseValue: (value: unknown) => value as unknown as Delta,
parseLiteral: (ast: ValueNode) => ast as unknown as Delta,
})
this.addScalarType('Json', JSONObjectResolver) this.addScalarType('Json', JSONObjectResolver)
this.addScalarType('Upload', GraphQLUpload) this.addScalarType('Upload', GraphQLUpload)

File diff suppressed because one or more lines are too long