From 69020844453dbcc044078c6328750d4a108bc3b9 Mon Sep 17 00:00:00 2001 From: Enoch Osarenren Date: Tue, 4 Jun 2024 16:05:18 +0100 Subject: [PATCH] added todos data to user object --- src/app.module.ts | 4 +++- src/graphql.ts | 7 +++++++ src/todos/todos.module.ts | 10 ++++++++++ src/todos/todos.service.spec.ts | 18 ++++++++++++++++++ src/todos/todos.service.ts | 31 +++++++++++++++++++++++++++++++ src/users/users.graphql | 7 +++++++ src/users/users.module.ts | 3 ++- src/users/users.resolver.ts | 14 +++++++++++--- 8 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 src/todos/todos.module.ts create mode 100644 src/todos/todos.service.spec.ts create mode 100644 src/todos/todos.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index cdcf11b..e1e5878 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -8,6 +8,7 @@ import { AuthorModule } from './users/users.module'; import { PostsModule } from './posts/posts.module'; import { ConfigModule } from '@nestjs/config'; import { AlbumsModule } from './albums/albums.module'; +import { TodosModule } from './todos/todos.module'; import configuration from './config/configuration'; @Module({ @@ -28,7 +29,8 @@ import configuration from './config/configuration'; }), AuthorModule, PostsModule, - AlbumsModule + AlbumsModule, + TodosModule ], controllers: [AppController], providers: [AppService], diff --git a/src/graphql.ts b/src/graphql.ts index 3cdd23b..0a13c68 100644 --- a/src/graphql.ts +++ b/src/graphql.ts @@ -19,6 +19,7 @@ export class User { company?: Nullable; posts?: Nullable[]>; albums?: Nullable[]>; + todos?: Nullable[]>; } export class Address { @@ -67,6 +68,12 @@ export class Photo { thumbnailUrl?: Nullable; } +export class Todo { + id: number; + title?: Nullable; + completed?: Nullable; +} + export abstract class IQuery { abstract users(): Nullable[]> | Promise[]>>; diff --git a/src/todos/todos.module.ts b/src/todos/todos.module.ts new file mode 100644 index 0000000..ab9f3e5 --- /dev/null +++ b/src/todos/todos.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { TodosService } from './todos.service'; +import { HttpModule } from '@nestjs/axios'; + +@Module({ + imports: [HttpModule], + providers: [TodosService], + exports: [TodosService] +}) +export class TodosModule {} diff --git a/src/todos/todos.service.spec.ts b/src/todos/todos.service.spec.ts new file mode 100644 index 0000000..1b439f5 --- /dev/null +++ b/src/todos/todos.service.spec.ts @@ -0,0 +1,18 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { TodosService } from './todos.service'; + +describe('TodosService', () => { + let service: TodosService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [TodosService], + }).compile(); + + service = module.get(TodosService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/todos/todos.service.ts b/src/todos/todos.service.ts new file mode 100644 index 0000000..7d0554c --- /dev/null +++ b/src/todos/todos.service.ts @@ -0,0 +1,31 @@ +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 { Todo } from 'src/graphql'; + +@Injectable() +export class TodosService { + private readonly baseUrl: string + private readonly logger: Logger = new Logger(TodosService.name) + + constructor( + private readonly httpService: HttpService, + private readonly configService: ConfigService + ) { + this.baseUrl = this.configService.get('api.baseurl') + } + + async getTodosByUserId(userId: number): Promise { + const { data } = await firstValueFrom( + this.httpService.get(`${this.baseUrl}/todos`).pipe( + catchError((err: AxiosError) => { + this.logger.error(err.response.data) + throw new BadRequestException("An error happened!") + }) + ) + ) + return data.filter(o => o.userId === userId) + } +} diff --git a/src/users/users.graphql b/src/users/users.graphql index 65d85ab..f495e3a 100644 --- a/src/users/users.graphql +++ b/src/users/users.graphql @@ -9,6 +9,7 @@ type User { company: Company posts: [Post] albums: [Album] + todos: [Todo] } type Address { @@ -57,6 +58,12 @@ type Photo { thumbnailUrl: String } +type Todo { + id: Int! + title: String + completed: Boolean +} + type Query { users: [User] user(id: Int!): User diff --git a/src/users/users.module.ts b/src/users/users.module.ts index d319d8b..1fe6701 100644 --- a/src/users/users.module.ts +++ b/src/users/users.module.ts @@ -4,9 +4,10 @@ import { UserService } from './users.service'; import { HttpModule } from '@nestjs/axios'; import { PostsModule } from 'src/posts/posts.module'; import { AlbumsModule } from 'src/albums/albums.module'; +import { TodosModule } from 'src/todos/todos.module'; @Module({ - imports: [HttpModule, PostsModule, AlbumsModule], + imports: [HttpModule, PostsModule, AlbumsModule, TodosModule], providers: [UsersResolver, UserService] }) export class AuthorModule {} diff --git a/src/users/users.resolver.ts b/src/users/users.resolver.ts index 8a7228e..96820c0 100644 --- a/src/users/users.resolver.ts +++ b/src/users/users.resolver.ts @@ -1,9 +1,10 @@ import { Args, Parent, Query, ResolveField, Resolver } from "@nestjs/graphql"; -import { Post, User } from "src/graphql"; +import { Album, Post, Todo, User } from "src/graphql"; import { UserService } from "./users.service"; import { PostsService } from "src/posts/posts.service"; import { AlbumsService } from "src/albums/albums.service"; +import { TodosService } from "src/todos/todos.service"; @Resolver('User') export class UsersResolver { @@ -11,7 +12,8 @@ export class UsersResolver { constructor( private readonly userService: UserService, private readonly postsService: PostsService, - private readonly albumService: AlbumsService + private readonly albumService: AlbumsService, + private readonly todoService: TodosService ){} @Query('users') @@ -31,8 +33,14 @@ export class UsersResolver { } @ResolveField('albums') - async getAlbums(@Parent() user) { + async getAlbums(@Parent() user): Promise { const { id } = user return await this.albumService.getUserAlbums(id); } + + @ResolveField('todos') + async getTodos(@Parent() user): Promise { + const { id } = user + return await this.todoService.getTodosByUserId(id) + } } \ No newline at end of file