update websocket
This commit is contained in:
@@ -5,8 +5,10 @@ import {
|
||||
PothosSchema,
|
||||
SchemaBuilderToken,
|
||||
} from '@smatch-corp/nestjs-pothos'
|
||||
import { Builder } from '../Graphql/graphql.builder'
|
||||
import { Builder, SchemaContext } from '../Graphql/graphql.builder'
|
||||
import { PrismaService } from '../Prisma/prisma.service'
|
||||
import { Message, MessageContextType, MessageType } from '@prisma/client'
|
||||
import { DateTimeUtils } from 'src/common/utils/datetime.utils'
|
||||
|
||||
@Injectable()
|
||||
export class MessageSchema extends PothosSchema {
|
||||
@@ -31,15 +33,25 @@ export class MessageSchema extends PothosSchema {
|
||||
chatRoomId: t.exposeID('chatRoomId', {
|
||||
description: 'The ID of the chat room.',
|
||||
}),
|
||||
message: t.expose('message', {
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
type: 'Json' as any,
|
||||
type: t.expose('type', {
|
||||
type: MessageType,
|
||||
description: 'The type of the message.',
|
||||
}),
|
||||
content: t.exposeString('content', {
|
||||
description: 'The message content.',
|
||||
}),
|
||||
sentAt: t.expose('sentAt', {
|
||||
type: 'DateTime',
|
||||
description: 'The date and time the message was sent.',
|
||||
}),
|
||||
context: t.expose('context', {
|
||||
type: MessageContextType,
|
||||
description: 'The context of the message.',
|
||||
}),
|
||||
metadata: t.expose('metadata', {
|
||||
type: 'Json',
|
||||
description: 'The metadata of the message.',
|
||||
}),
|
||||
sender: t.relation('sender', {
|
||||
description: 'The sender of the message.',
|
||||
}),
|
||||
@@ -95,16 +107,34 @@ export class MessageSchema extends PothosSchema {
|
||||
// mutations
|
||||
this.builder.mutationFields((t) => ({
|
||||
testSendMessage: t.field({
|
||||
type: 'String',
|
||||
type: this.message(),
|
||||
description: 'Test sending a message.',
|
||||
resolve: async (_, __, ctx) => {
|
||||
resolve: async (_root, _args, ctx) => {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
ctx.http.pubSub.publish('MESSAGE_SENT', {
|
||||
message: 'Hello, world!',
|
||||
id: '1',
|
||||
senderId: '1',
|
||||
recipientId: '2',
|
||||
chatRoomId: 'b86e840f-81d3-4043-b57a-f9adf719423c',
|
||||
type: MessageType.TEXT,
|
||||
content: 'Hello, world!',
|
||||
context: MessageContextType.CHAT,
|
||||
metadata: {},
|
||||
sentAt: DateTimeUtils.now().toJSDate(),
|
||||
})
|
||||
return 'Message sent'
|
||||
return {
|
||||
id: '1',
|
||||
senderId: '1',
|
||||
recipientId: '2',
|
||||
chatRoomId: 'b86e840f-81d3-4043-b57a-f9adf719423c',
|
||||
type: MessageType.TEXT,
|
||||
content: 'Hello, world!',
|
||||
context: MessageContextType.CHAT,
|
||||
metadata: {},
|
||||
sentAt: DateTimeUtils.now().toJSDate(),
|
||||
}
|
||||
},
|
||||
}),
|
||||
sendMessage: t.prismaField({
|
||||
@@ -112,12 +142,29 @@ export class MessageSchema extends PothosSchema {
|
||||
description: 'Send a message to a chat room.',
|
||||
args: {
|
||||
input: t.arg({
|
||||
type: this.builder.generator.getCreateInput('Message'),
|
||||
type: this.builder.generator.getCreateInput('Message', [
|
||||
'id',
|
||||
'senderId',
|
||||
'sender',
|
||||
'sentAt',
|
||||
]),
|
||||
description: 'The message to send.',
|
||||
required: true,
|
||||
}),
|
||||
},
|
||||
resolve: async (query, _root, args, ctx, _info) => {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
// get the sender from the context and add it to the input
|
||||
args.input.sender = {
|
||||
connect: {
|
||||
id: ctx.http.me?.id,
|
||||
},
|
||||
}
|
||||
if (!args.input.sender) {
|
||||
throw new Error('Cannot get sender from context')
|
||||
}
|
||||
const message = await this.prisma.message.create({
|
||||
...query,
|
||||
data: args.input,
|
||||
@@ -134,24 +181,17 @@ export class MessageSchema extends PothosSchema {
|
||||
this.builder.subscriptionFields((t) => ({
|
||||
messageSent: t.field({
|
||||
description: 'Subscribe to messages sent by users.',
|
||||
args: {},
|
||||
subscribe: async (_, __, ctx) => {
|
||||
if (!ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
return (await ctx.websocket.pubSub.asyncIterator(
|
||||
type: this.message(),
|
||||
subscribe: (_, __, ctx: SchemaContext) => {
|
||||
if (!ctx.isSubscription) throw new Error('Not allowed')
|
||||
const {
|
||||
websocket: { pubSub },
|
||||
} = ctx
|
||||
return pubSub.asyncIterator(
|
||||
'MESSAGE_SENT',
|
||||
)) as unknown as AsyncIterable<unknown>
|
||||
) as unknown as AsyncIterable<Message>
|
||||
},
|
||||
type: this.message(), // Add the type property
|
||||
resolve: (payload) =>
|
||||
payload as {
|
||||
message: 'Json'
|
||||
id: string
|
||||
senderId: string
|
||||
chatRoomId: string
|
||||
sentAt: Date
|
||||
},
|
||||
resolve: (payload: Message) => payload,
|
||||
}),
|
||||
}))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user