Skip to content

Commit

Permalink
added todos data to user object
Browse files Browse the repository at this point in the history
  • Loading branch information
enochval committed Jun 4, 2024
1 parent 5fc55de commit 6902084
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -28,7 +29,8 @@ import configuration from './config/configuration';
}),
AuthorModule,
PostsModule,
AlbumsModule
AlbumsModule,
TodosModule
],
controllers: [AppController],
providers: [AppService],
Expand Down
7 changes: 7 additions & 0 deletions src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class User {
company?: Nullable<Company>;
posts?: Nullable<Nullable<Post>[]>;
albums?: Nullable<Nullable<Album>[]>;
todos?: Nullable<Nullable<Todo>[]>;
}

export class Address {
Expand Down Expand Up @@ -67,6 +68,12 @@ export class Photo {
thumbnailUrl?: Nullable<string>;
}

export class Todo {
id: number;
title?: Nullable<string>;
completed?: Nullable<boolean>;
}

export abstract class IQuery {
abstract users(): Nullable<Nullable<User>[]> | Promise<Nullable<Nullable<User>[]>>;

Expand Down
10 changes: 10 additions & 0 deletions src/todos/todos.module.ts
Original file line number Diff line number Diff line change
@@ -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 {}
18 changes: 18 additions & 0 deletions src/todos/todos.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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>(TodosService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
31 changes: 31 additions & 0 deletions src/todos/todos.service.ts
Original file line number Diff line number Diff line change
@@ -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<string>('api.baseurl')
}

async getTodosByUserId(userId: number): Promise<Todo[]> {
const { data } = await firstValueFrom(
this.httpService.get<any[]>(`${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)
}
}
7 changes: 7 additions & 0 deletions src/users/users.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type User {
company: Company
posts: [Post]
albums: [Album]
todos: [Todo]
}

type Address {
Expand Down Expand Up @@ -57,6 +58,12 @@ type Photo {
thumbnailUrl: String
}

type Todo {
id: Int!
title: String
completed: Boolean
}

type Query {
users: [User]
user(id: Int!): User
Expand Down
3 changes: 2 additions & 1 deletion src/users/users.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
14 changes: 11 additions & 3 deletions src/users/users.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@

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 {

constructor(
private readonly userService: UserService,
private readonly postsService: PostsService,
private readonly albumService: AlbumsService
private readonly albumService: AlbumsService,
private readonly todoService: TodosService
){}

@Query('users')
Expand All @@ -31,8 +33,14 @@ export class UsersResolver {
}

@ResolveField('albums')
async getAlbums(@Parent() user) {
async getAlbums(@Parent() user): Promise<Album[]> {
const { id } = user
return await this.albumService.getUserAlbums(id);
}

@ResolveField('todos')
async getTodos(@Parent() user): Promise<Todo[]> {
const { id } = user
return await this.todoService.getTodosByUserId(id)
}
}

0 comments on commit 6902084

Please sign in to comment.