-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented query and filters for users, userById, posts comments
- Loading branch information
Showing
16 changed files
with
238 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Args, Parent, Query, ResolveField, Resolver } from "@nestjs/graphql"; | ||
import { CommentsService } from "./comments.service"; | ||
import { Comment, Post } from "src/graphql"; | ||
import { PostsService } from "../posts.service"; | ||
|
||
@Resolver('Comment') | ||
export class CommentsResolver { | ||
|
||
constructor( | ||
private readonly commentsService: CommentsService, | ||
private readonly postsService: PostsService | ||
) {} | ||
|
||
@Query('comments') | ||
async getComments(@Args() args: any): Promise<Comment[]> { | ||
return await this.commentsService.getComments(args) | ||
} | ||
|
||
@ResolveField('post') | ||
async getCommentPost(@Parent() comment): Promise<Post> { | ||
return await this.postsService.getPost(comment.postId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { CommentsService } from './comments.service'; | ||
|
||
describe('CommentsService', () => { | ||
let service: CommentsService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [CommentsService], | ||
}).compile(); | ||
|
||
service = module.get<CommentsService>(CommentsService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { HttpService } from '@nestjs/axios'; | ||
import { BadRequestException, Injectable, Logger } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { AxiosError } from 'axios'; | ||
import { catchError, firstValueFrom } from 'rxjs'; | ||
import { Comment } from 'src/graphql'; | ||
|
||
@Injectable() | ||
export class CommentsService { | ||
private readonly logger: Logger = new Logger(CommentsService.name) | ||
private baseUrl: string | ||
|
||
constructor( | ||
private readonly httpService: HttpService, | ||
private readonly configService: ConfigService | ||
){ | ||
this.baseUrl = this.configService.get<string>('api.baseurl') | ||
} | ||
|
||
async getComments(args: any): Promise<Comment[]> { | ||
const slashCommentId = `${args.commentId ? '/'+args.commentId : ''}` | ||
const queryPostId = `${args.postId ? '?postId=' + args.postId : ''}` | ||
const uri = `${this.baseUrl}/comments${slashCommentId}${queryPostId}` | ||
|
||
const { data } = await firstValueFrom( | ||
this.httpService.get<any|Comment[]>(uri).pipe( | ||
catchError((err: AxiosError) => { | ||
this.logger.error(err.response.data) | ||
throw new BadRequestException("An error happened!") | ||
}) | ||
) | ||
) | ||
|
||
if (args.commentId) { | ||
const res: Array<Comment> = [] | ||
res.push(data) | ||
return res | ||
} | ||
|
||
let rsp: Array<Comment> = data | ||
|
||
if (args.first) { | ||
rsp = rsp.slice(0, args.first) | ||
} | ||
|
||
return rsp | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { Module, forwardRef } from '@nestjs/common'; | ||
import { PostsService } from './posts.service'; | ||
import { HttpModule } from '@nestjs/axios'; | ||
import { PostsResolver } from './posts.resolver'; | ||
import { UserModule } from 'src/users/users.module'; | ||
import { CommentsService } from './comments/comments.service'; | ||
import { CommentsResolver } from './comments/comments.resolver'; | ||
|
||
@Module({ | ||
imports: [HttpModule], | ||
providers: [PostsService, PostsResolver], | ||
imports: [HttpModule, forwardRef(() => UserModule)], | ||
providers: [PostsService, PostsResolver, CommentsService, CommentsResolver], | ||
exports: [PostsService] | ||
}) | ||
export class PostsModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,29 @@ | ||
import { Parent, ResolveField, Resolver } from "@nestjs/graphql"; | ||
import { Args, Parent, Query, ResolveField, Resolver } from "@nestjs/graphql"; | ||
import { PostsService } from "./posts.service"; | ||
import { Post, User } from "src/graphql"; | ||
import { UserService } from "src/users/users.service"; | ||
|
||
@Resolver('Post') | ||
export class PostsResolver { | ||
|
||
constructor(private readonly postsService: PostsService){} | ||
constructor( | ||
private readonly postsService: PostsService, | ||
private readonly userService: UserService | ||
){} | ||
|
||
@Query('posts') | ||
async getPosts(@Args() args: any): Promise<Post[]> { | ||
return await this.postsService.getPosts(args) | ||
} | ||
|
||
@ResolveField('user') | ||
async getPostUser(@Parent() post): Promise<User> { | ||
return await this.userService.getUserById(post.userId) | ||
} | ||
|
||
@ResolveField('comments') | ||
async getPosts(@Parent() post): Promise<Comment[]> { | ||
async getPostComments(@Parent() post, @Args() args): Promise<Comment[]> { | ||
const { id } = post | ||
return await this.postsService.getPostComments(id) | ||
return await this.postsService.getPostComments(id, args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.