From bb0eed1851fa383b730948fc63721c632063374a Mon Sep 17 00:00:00 2001 From: Ly Tuan Kiet Date: Fri, 15 Nov 2024 20:30:24 +0700 Subject: [PATCH] complete delta?? --- src/Document/document.schema.ts | 57 ++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/Document/document.schema.ts b/src/Document/document.schema.ts index 0ba59ac..0663368 100644 --- a/src/Document/document.schema.ts +++ b/src/Document/document.schema.ts @@ -10,6 +10,7 @@ import { PrismaService } from '../Prisma/prisma.service' import { DocumentEvent } from './document.event' import { Document } from '@prisma/client' import { DocumentDelta } from './document.type' +import Delta from 'quill-delta' @Injectable() export class DocumentSchema extends PothosSchema { constructor( @@ -40,8 +41,8 @@ export class DocumentSchema extends PothosSchema { documentDelta() { return this.builder.simpleObject('DocumentDelta', { fields: (t) => ({ - pageIndex: t.int(), documentId: t.string(), + pageIndex: t.int(), delta: t.field({ type: 'Delta', }), @@ -49,6 +50,17 @@ export class DocumentSchema extends PothosSchema { }) } + @PothosRef() + documentDeltaInput() { + return this.builder.inputType('DocumentDeltaInput', { + fields: (t) => ({ + documentId: t.string(), + pageIndex: t.int(), + delta: t.field({ type: 'Delta' }), + }), + }) + } + @Pothos() init(): void { this.builder.queryFields((t) => ({ @@ -94,6 +106,49 @@ export class DocumentSchema extends PothosSchema { }) }, }), + + testUpdateDocument: t.field({ + type: this.documentDelta(), + args: { + documentId: t.arg({ type: 'String', required: true }), + pageIndex: t.arg({ type: 'Int', required: true }), + }, + resolve: async (_root, args, ctx: SchemaContext) => { + if (ctx.isSubscription) throw new Error('Not allowed') + const delta = new Delta().insert('test') + const documentDelta = { + documentId: args.documentId, + pageIndex: args.pageIndex, + delta, + } + ctx.http.pubSub.publish( + `${DocumentEvent.CHANGED}.${args.documentId}`, + documentDelta, + ) + return documentDelta + }, + }), + + updateDocument: t.field({ + type: this.documentDelta(), + args: { + data: t.arg({ + type: this.documentDeltaInput(), + required: true, + }), + }, + resolve: async (_, args, ctx: SchemaContext) => { + if (ctx.isSubscription) throw new Error('Not allowed') + const { + http: { pubSub }, + } = ctx + pubSub.publish( + `${DocumentEvent.CHANGED}.${args.data.documentId}`, + args.data, + ) + return args.data + }, + }), })) this.builder.subscriptionFields((t) => ({