0
message.module.ts:
import { Module, forwardRef } from '@nestjs/common';
import { MessageResolver } from './message.resolver';
import { MessageService } from './message.service';
import { MongooseModule } from '@nestjs/mongoose';
import { Message, MessageSchema } from './models/message.model';
import { UserModule } from 'src/user/user.module';
import { GroupModule } from 'src/group/group.module';
import { PubSub } from 'graphql-subscriptions';
@Module({
imports: [
MongooseModule.forFeature([{ name: Message.name, schema: MessageSchema }]),
forwardRef(() => UserModule),
GroupModule,
],
providers: [MessageResolver, MessageService, PubSub],
exports: [MessageService],
})
export class MessageModule {}
message.resolver.ts:
import { PubSub } from 'graphql-subscriptions';
import { MessageService } from './message.service';
import { Inject } from '@nestjs/common';
import { Resolver, Query, Mutation, Args } from '@nestjs/graphql';
import { Message } from './models/message.model';
import { CreateMessageInput } from './dto/message.input';
@Resolver((of) => Message)
export class MessageResolver {
constructor(
@Inject('MessageService')
private readonly service: MessageService,
@Inject('PubSub') private readonly pubSub: PubSub,
) {}
@Query((returns) => [Message])
async getMessagesByUserId(
@Args('userId', { type: () => String }) userId: string,
): Promise<Message[]> {
return this.service.getMessagesByUserId(userId);
}
@Mutation((returns) => Message)
async createMessage(
@Args('createMessageInput') createMessageInput: CreateMessageInput,
): Promise<Message> {
const newMessage = this.service.create(createMessageInput);
this.pubSub.publish('messageAdded', { messageAdded: newMessage });
return newMessage;
}
}
I wrote a chat application and it is integrated with Nest and GraphQL.
Now I want to add the resolvers of the message module, but it gives this error
New contributor