feat: user update and subscribers added
This commit is contained in:
@@ -1,14 +1,22 @@
|
||||
import { Resolver, Query } from '@nestjs/graphql';
|
||||
import { Resolver, Query, Mutation, Args, Subscription } from '@nestjs/graphql';
|
||||
import { User } from './user.model';
|
||||
import { UseGuards } from '@nestjs/common';
|
||||
import { GqlAuthGuard } from '../guards/gql-auth.guard';
|
||||
import { GqlUser } from '../decorators/gql-user.decorator';
|
||||
import { UserService } from './user.service';
|
||||
import { throwErr } from 'src/utils';
|
||||
import * as E from 'fp-ts/lib/Either';
|
||||
import { UpdateUserInput } from './dtos/update-user-input.dto';
|
||||
import { PubSubService } from 'src/pubsub/pubsub.service';
|
||||
|
||||
@Resolver(() => User)
|
||||
export class UserResolver {
|
||||
// TODO: remove the eslint-disable line below once dependencies are added to user.service file
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
constructor() {}
|
||||
constructor(
|
||||
private readonly userService: UserService,
|
||||
private readonly pubsub: PubSubService,
|
||||
) {}
|
||||
|
||||
@Query(() => User, {
|
||||
description:
|
||||
@@ -27,4 +35,30 @@ export class UserResolver {
|
||||
me2(@GqlUser() user: User): User {
|
||||
return user;
|
||||
}
|
||||
|
||||
/* Mutations */
|
||||
|
||||
@Mutation(() => User, {
|
||||
description: 'Update user information',
|
||||
})
|
||||
@UseGuards(GqlAuthGuard)
|
||||
async updateUser(
|
||||
@GqlUser() user: User,
|
||||
@Args('userInput') userInput: UpdateUserInput,
|
||||
): Promise<User> {
|
||||
const updatedUser = await this.userService.updateUser(user, userInput);
|
||||
if (E.isLeft(updatedUser)) throwErr(updatedUser.left);
|
||||
return updatedUser.right;
|
||||
}
|
||||
|
||||
/* Subscriptions */
|
||||
|
||||
@Subscription(() => User, {
|
||||
description: 'Listen for user updates',
|
||||
resolve: (value) => value,
|
||||
})
|
||||
@UseGuards(GqlAuthGuard)
|
||||
userSettingsUpdated(@GqlUser() user: User) {
|
||||
return this.pubsub.asyncIterator(`user/${user.uid}/updated`);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user