Code Monkey home page Code Monkey logo

nest-ideas-api's Introduction

Ideas APP API

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

NPM Version Package License NPM Downloads Travis Linux Coverage Gitter Backers on Open Collective Sponsors on Open Collective

Description

Nest framework TypeScript starter repository.

A reddit/twitter style app to keep track of posted App Ideas. This is the companion source code for the YouTube tutorial Ideas App - NestJS API series.

User Stories - Project Timeline

  • x Authenticate users
  • x Users can CRUD ideas
  • x Users can upvote/downvote ideas
  • x Users can bookmark ideas
  • x Users can comment on ideas
  • x Ideas can be seen in realtime

Stack

  • Database - PostgreSQL
  • REST API - NestJS
  • GraphQL API - NestJS
  • Rest Frontend - Angular with NGRX
  • GraphQL Frontend - React (Native?) with Apollo Client

nest-ideas-api's People

Contributors

dependabot[bot] avatar kelvin-mai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nest-ideas-api's Issues

Question: Partial in UPDATE Methods

Dear @kelvin-mai ,

first of all: This tutorial is awesome ๐ŸŽ‰ Really nice work!

I have a question regarding the use of the Partial<Entity> in your update(...) methods, like in the linked code snippet for the controller:

@Put(':id')
@UseGuards(new AuthGuard())
@UsePipes(new ValidationPipe())
updateIdea(
@Param('id') id: string,
@User('id') user,
@Body() body: Partial<IdeaDTO>,
) {
this.logData({ id, user, body });
return this.ideaService.update(id, user, body);
}

and the corresponding service:

async update(
id: string,
userId: string,
data: Partial<IdeaDTO>,
): Promise<IdeaRO> {
let idea = await this.ideaRepository.findOne({
where: { id },
relations: ['author', 'upvotes', 'downvotes', 'comments'],
});
if (!idea) {
throw new HttpException('Not found', HttpStatus.NOT_FOUND);
}
this.ensureOwnership(idea, userId);
await this.ideaRepository.update({ id }, data);
idea = await this.ideaRepository.findOne({
where: { id },
relations: ['author', 'upvotes', 'downvotes', 'comments'],
});
return this.ideaToResponseObject(idea);
}

As far as i understood, the Partial<IdeaDTO> only tells, that it must be an IdeaDTO, but it must not be "fully filled" (e.g., not all values must be present). Wouldn't it be better to "remove" the Partial part and directly use the IdeaDTO in this case and use the @IsOptional() decorator from the class-validator package?

My thought process is as follows:
Consider the example, where you have different DTOs based on the operation you are calling (e.g., a CreateUserDTO and UpdateUserDTO). For the CreateUserDTO you may provide different attributes than for the UpdateUserDTO (like, the user is not allowed to change his username after registration).

This way you could enforce the user, that he must send certain attributes with the request (e.g., they must be present!). For example, in order to change your user-profile you must send the current password to prove your identity!

Further, it would be much cleaner, if you use Swagger as well - you would use the @ApiModelPropertyOptional() decorator on the attributes that are flagged as @IsOptional(). This way,

This would look something like this:

export class UpdateUserDTO {

   @IsOptional()
   @IsString()   
   @ApiModelPropertyOptional({ type: 'string' })
   firstname?: string;

   @IsOptional()
   @IsString()
   @ApiModelPropertyOptional({ type: 'string' })
   lastname?: string;

   // additional attributes that may be set optionally here
   
   @IsString()
   @MinLength(8)
   @ApiModelProperty({ type: 'string', minLength: 8 })
   password: string;
}

What do you think about this? Or am I missing something crucial here?!
Of course, the corresponding Entity class may needs to have optional attributes as well (e.g., it may be the case that the firstname will never be set) - or you use default values from TypeORM..

I would love to hear your thoughts on this...

All the best

One Graph Principle

Apollo has released their best practices guide for GraphQL based on their work with hundreds of organizations. One of those principles is that we should use one graph. When I asked online about this I was told that all the big kids are using one schema for all queries, types, and mutations. I'm not sure about resolvers yet but my impression is that one page does it all for all modules. So it lives at the root level. Once source of truth.

You are breaking the graph into their relevant modules as we do with everything else including REST. This architecture should probably be re-examined.

Meanwhile, I'm trying to deal with a mental breakdown from trying to get my head around all of GraphQL in one super module. :-)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.