toi cam thay minh la sieu nhan
This commit is contained in:
110
src/Document/document.schema.ts
Normal file
110
src/Document/document.schema.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { Inject, Injectable, Logger } from '@nestjs/common'
|
||||
import {
|
||||
Pothos,
|
||||
PothosRef,
|
||||
PothosSchema,
|
||||
SchemaBuilderToken,
|
||||
} from '@smatch-corp/nestjs-pothos'
|
||||
import { Builder, SchemaContext } from '../Graphql/graphql.builder'
|
||||
import { PrismaService } from '../Prisma/prisma.service'
|
||||
import { DocumentEvent } from './document.event'
|
||||
import { Document } from '@prisma/client'
|
||||
@Injectable()
|
||||
export class DocumentSchema extends PothosSchema {
|
||||
constructor(
|
||||
@Inject(SchemaBuilderToken) private readonly builder: Builder,
|
||||
private readonly prisma: PrismaService,
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
@PothosRef()
|
||||
document() {
|
||||
return this.builder.prismaObject('Document', {
|
||||
fields: (t) => ({
|
||||
id: t.exposeID('id'),
|
||||
name: t.exposeString('name'),
|
||||
fileUrl: t.exposeString('fileUrl'),
|
||||
createdAt: t.expose('createdAt', { type: 'DateTime' }),
|
||||
updatedAt: t.expose('updatedAt', { type: 'DateTime' }),
|
||||
owner: t.relation('owner'),
|
||||
ownerId: t.exposeID('ownerId'),
|
||||
collaborators: t.exposeStringList('collaborators'),
|
||||
isPublic: t.exposeBoolean('isPublic'),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
@Pothos()
|
||||
init(): void {
|
||||
this.builder.queryFields((t) => ({
|
||||
document: t.prismaField({
|
||||
type: this.document(),
|
||||
args: this.builder.generator.findUniqueArgs('Document'),
|
||||
resolve: async (query, _root, args) => {
|
||||
return await this.prisma.document.findUnique({
|
||||
...query,
|
||||
where: args.where,
|
||||
})
|
||||
},
|
||||
}),
|
||||
|
||||
documents: t.prismaField({
|
||||
type: [this.document()],
|
||||
args: this.builder.generator.findManyArgs('Document'),
|
||||
resolve: async (query, _root, args) => {
|
||||
return await this.prisma.document.findMany({
|
||||
...query,
|
||||
skip: args.skip ?? undefined,
|
||||
take: args.take ?? undefined,
|
||||
orderBy: args.orderBy ?? undefined,
|
||||
where: args.filter ?? undefined,
|
||||
})
|
||||
},
|
||||
}),
|
||||
}))
|
||||
|
||||
this.builder.mutationFields((t) => ({
|
||||
createDocument: t.prismaField({
|
||||
type: this.document(),
|
||||
args: {
|
||||
data: t.arg({
|
||||
type: this.builder.generator.getCreateInput('Document'),
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
resolve: async (query, _root, args) => {
|
||||
return await this.prisma.document.create({
|
||||
...query,
|
||||
data: args.data,
|
||||
})
|
||||
},
|
||||
}),
|
||||
}))
|
||||
|
||||
this.builder.subscriptionFields((t) => ({
|
||||
document: t.field({
|
||||
type: this.document(),
|
||||
args: {
|
||||
documentId: t.arg({
|
||||
type: 'String',
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
subscribe: (_, args, ctx: SchemaContext) => {
|
||||
if (!ctx.isSubscription) throw new Error('Not allowed')
|
||||
const {
|
||||
websocket: { pubSub },
|
||||
} = ctx
|
||||
return pubSub.asyncIterator([
|
||||
`${DocumentEvent.CHANGED}.${args.documentId}`,
|
||||
`${DocumentEvent.CREATED}.${args.documentId}`,
|
||||
`${DocumentEvent.DELETED}.${args.documentId}`,
|
||||
`${DocumentEvent.SAVED}.${args.documentId}`,
|
||||
]) as unknown as AsyncIterable<Document>
|
||||
},
|
||||
resolve: async (payload: Document) => payload,
|
||||
}),
|
||||
}))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user