thang an pha qua
This commit is contained in:
20
package-lock.json
generated
20
package-lock.json
generated
@@ -22,6 +22,7 @@
|
||||
"@nestjs/common": "^10.0.0",
|
||||
"@nestjs/config": "^3.2.3",
|
||||
"@nestjs/core": "^10.0.0",
|
||||
"@nestjs/event-emitter": "^2.1.1",
|
||||
"@nestjs/graphql": "^12.2.0",
|
||||
"@nestjs/jwt": "^10.2.0",
|
||||
"@nestjs/platform-express": "^10.0.0",
|
||||
@@ -4655,6 +4656,19 @@
|
||||
"integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==",
|
||||
"license": "0BSD"
|
||||
},
|
||||
"node_modules/@nestjs/event-emitter": {
|
||||
"version": "2.1.1",
|
||||
"resolved": "https://registry.npmjs.org/@nestjs/event-emitter/-/event-emitter-2.1.1.tgz",
|
||||
"integrity": "sha512-6L6fBOZTyfFlL7Ih/JDdqlCzZeCW0RjCX28wnzGyg/ncv5F/EOeT1dfopQr1loBRQ3LTgu8OWM7n4zLN4xigsg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"eventemitter2": "6.4.9"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@nestjs/common": "^8.0.0 || ^9.0.0 || ^10.0.0",
|
||||
"@nestjs/core": "^8.0.0 || ^9.0.0 || ^10.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@nestjs/graphql": {
|
||||
"version": "12.2.1",
|
||||
"resolved": "https://registry.npmjs.org/@nestjs/graphql/-/graphql-12.2.1.tgz",
|
||||
@@ -9553,6 +9567,12 @@
|
||||
"node": ">=6"
|
||||
}
|
||||
},
|
||||
"node_modules/eventemitter2": {
|
||||
"version": "6.4.9",
|
||||
"resolved": "https://registry.npmjs.org/eventemitter2/-/eventemitter2-6.4.9.tgz",
|
||||
"integrity": "sha512-JEPTiaOt9f04oa6NOkc4aH+nVp5I3wEjpHbIPqfgCdD5v5bUzy7xQqwcVO2aDQgOWhI28da57HksMrzK9HlRxg==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/eventemitter3": {
|
||||
"version": "5.0.1",
|
||||
"resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-5.0.1.tgz",
|
||||
|
||||
11
package.json
11
package.json
@@ -44,6 +44,7 @@
|
||||
"@nestjs/common": "^10.0.0",
|
||||
"@nestjs/config": "^3.2.3",
|
||||
"@nestjs/core": "^10.0.0",
|
||||
"@nestjs/event-emitter": "^2.1.1",
|
||||
"@nestjs/graphql": "^12.2.0",
|
||||
"@nestjs/jwt": "^10.2.0",
|
||||
"@nestjs/platform-express": "^10.0.0",
|
||||
@@ -133,13 +134,19 @@
|
||||
"ws": "^8.18.0"
|
||||
},
|
||||
"jest": {
|
||||
"moduleFileExtensions": ["js", "json", "ts"],
|
||||
"moduleFileExtensions": [
|
||||
"js",
|
||||
"json",
|
||||
"ts"
|
||||
],
|
||||
"rootDir": "src",
|
||||
"testRegex": ".*\\.spec\\.ts$",
|
||||
"transform": {
|
||||
"^.+\\.(t|j)s$": "ts-jest"
|
||||
},
|
||||
"collectCoverageFrom": ["**/*.(t|j)s"],
|
||||
"collectCoverageFrom": [
|
||||
"**/*.(t|j)s"
|
||||
],
|
||||
"coverageDirectory": "../coverage",
|
||||
"testEnvironment": "node"
|
||||
},
|
||||
|
||||
@@ -40,8 +40,6 @@ import { WorkshopOrganizationModule } from '../WorkshopOrganization/workshoporga
|
||||
import { WorkshopSubscriptionModule } from '../WorkshopSubscription/workshopsubscription.module'
|
||||
import { initContextCache } from '@pothos/core'
|
||||
import { PubSub } from 'graphql-subscriptions'
|
||||
import { isSubscription } from 'rxjs/internal/Subscription'
|
||||
import { EventEmitter } from 'ws'
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
@@ -90,7 +88,6 @@ import { EventEmitter } from 'ws'
|
||||
debug: process.env.NODE_ENV === 'development' || false,
|
||||
playground: process.env.NODE_ENV === 'development' || false,
|
||||
introspection: process.env.NODE_ENV === 'development' || false,
|
||||
installSubscriptionHandlers: true,
|
||||
subscriptions: {
|
||||
'graphql-ws': true,
|
||||
},
|
||||
@@ -100,6 +97,7 @@ import { EventEmitter } from 'ws'
|
||||
extra,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
}: { req?: Request; subscriptions?: any; extra?: any }) => {
|
||||
initContextCache()
|
||||
if (subscriptions) {
|
||||
return {
|
||||
isSubscription: true,
|
||||
@@ -148,10 +146,7 @@ import { EventEmitter } from 'ws'
|
||||
},
|
||||
{
|
||||
provide: 'PUB_SUB',
|
||||
useFactory: () =>
|
||||
new PubSub({
|
||||
eventEmitter: new EventEmitter({}),
|
||||
}),
|
||||
useFactory: () => new PubSub(),
|
||||
},
|
||||
],
|
||||
exports: [
|
||||
|
||||
@@ -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