thang an pha qua
This commit is contained in:
@@ -147,6 +147,7 @@ export class MessageSchema extends PothosSchema {
|
||||
'senderId',
|
||||
'sender',
|
||||
'sentAt',
|
||||
'context',
|
||||
]),
|
||||
description: 'The message to send.',
|
||||
required: true,
|
||||
@@ -156,6 +157,7 @@ export class MessageSchema extends PothosSchema {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
const messageContext = MessageContextType.CHAT
|
||||
// get the sender from the context and add it to the input
|
||||
args.input.sender = {
|
||||
connect: {
|
||||
@@ -165,6 +167,7 @@ export class MessageSchema extends PothosSchema {
|
||||
if (!args.input.sender) {
|
||||
throw new Error('Cannot get sender from context')
|
||||
}
|
||||
args.input.context = messageContext
|
||||
const message = await this.prisma.message.create({
|
||||
...query,
|
||||
data: args.input,
|
||||
@@ -172,7 +175,7 @@ export class MessageSchema extends PothosSchema {
|
||||
if (ctx.isSubscription) {
|
||||
throw new Error('Not allowed')
|
||||
}
|
||||
ctx.http.pubSub.publish('MESSAGE_SENT', message)
|
||||
ctx.http.pubSub.publish(`MESSAGE_SENT_${message.chatRoomId}`, message)
|
||||
return message
|
||||
},
|
||||
}),
|
||||
@@ -182,13 +185,19 @@ export class MessageSchema extends PothosSchema {
|
||||
messageSent: t.field({
|
||||
description: 'Subscribe to messages sent by users.',
|
||||
type: this.message(),
|
||||
subscribe: (_, __, ctx: SchemaContext) => {
|
||||
args: {
|
||||
chatRoomId: t.arg({
|
||||
type: 'String',
|
||||
description: 'The ID of the chat room to subscribe to.',
|
||||
}),
|
||||
},
|
||||
subscribe: (_, args, ctx: SchemaContext) => {
|
||||
if (!ctx.isSubscription) throw new Error('Not allowed')
|
||||
const {
|
||||
websocket: { pubSub },
|
||||
} = ctx
|
||||
return pubSub.asyncIterator(
|
||||
'MESSAGE_SENT',
|
||||
`MESSAGE_SENT_${args.chatRoomId}`,
|
||||
) as unknown as AsyncIterable<Message>
|
||||
},
|
||||
resolve: (payload: Message) => payload,
|
||||
|
||||
Reference in New Issue
Block a user