notiz-dev / notiz

Frontend for notiz.dev. Built with Angular and Scully 👋
https://notiz.dev
67 stars 9 forks source link

GraphQL Code-First Approach with NestJS 7 #138

Closed utterances-bot closed 3 years ago

utterances-bot commented 3 years ago

GraphQL Code-First Approach with NestJS 7

Create a GraphQL API using Code-First Approach with NestJS 7.

https://notiz.dev/blog/graphql-code-first-with-nestjs-7

NiicooR commented 3 years ago

Hi, excelent post! I'm trying to implement this but at the moment I return this.prisma.user.findMany() in my resolver my application breaks when compilation starts. With no error message. What could I be doing wrong?

This is my console output when npm run start:dev

[9:14:29 AM] Starting compilation in watch mode...
 Error  Debug Failure.

My resolver

@Resolver(() => User)
export class UserResolver {
  constructor(
    private prisma: PrismaService,
  ) {}

  @Query(() => [User])
  breaksAll() {
    return this.prisma.user.findMany();
  }

  @Query(() => [User])
  works() {
    const c = new User();
    c.email = '23';
    c.id = 1;
    return [c];
  }
}

I simplify the User model trying to isolate the issue prisma Entity

model User {
  id    Int     @default(autoincrement()) @id
  email String  @unique
  name  String?
}

GraphQL entity

@ObjectType()
export class User {
  @Field(() => Int)
  id: number;
  @Field(() => String)
  email: string;
  @Field(() => String)
  name: string;
}

Thank you, Nico

marcjulian commented 3 years ago

@NiicooR Hi Nico, I actually had the same error last week. Do you have by any chance typescript@4.2.x installed? After downgrading to typescript@4.1.x this error was resolved. Please let me know if thats the same case for you.

NiicooR commented 3 years ago

@marcjulian, That's the same case for me!. I've just downgraded my typescript version and everything starts working. How did you notice that the typescript's version was the problem?

Anyways, Thank you very much!

marcjulian commented 3 years ago

Perfect. I am not sure, but I know in another application it was working without error and just with a lower typescript version. Might be good to create an issue with prisma so they can fix issues with typescript 4.2.x

eliotis commented 3 years ago

Hi Marc! Great post. In the docs it says that the plugin will "set the nullable property depending on the question mark (e.g. name?: string will set nullable: true)", but it doesn't change the null property in my MySQL database. Only when I explicitly set {nullable: true} does this occur. Have you run into this issue? I can't find anything online about this and it has baffled me for weeks! Help!

marcjulian commented 3 years ago

@eliotis Hi, can you provide a reproduction repo? I am not sure what the problem is.

Have you added the plugin to your nest-cli.json? https://docs.nestjs.com/graphql/cli-plugin

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "plugins": ["@nestjs/graphql"]
  }
}

Also you need to make sure that your file names have one of the following suffix to be picked up by the cli

https://docs.nestjs.com/graphql/cli-plugin#overview

Please, note that your filenames must have one of the following suffixes in order to be analyzed by the plugin: ['.input.ts', '.args.ts', '.entity.ts', '.model.ts']['.input.ts', '.args.ts', '.entity.ts', '.model.ts']

enjora commented 3 years ago

Hi Marc, I have been struggling with something and I hope you can help me. I am relatively new to the Nest and to Programming in general. You mentioned that we are using Prisma and when I checked the other blog you have on the Prisma / Nest integration i can tell that we are following a SDL approach no t. However in this blog you are following code-first .. I am a bit confused with integrating prisma and nest. should I also maintain schema.prisma file under the prisma folder or rely on the autogenerated file like what you have here?

marcjulian commented 3 years ago

Hi @enjora, I wrote about this topic in this disscussion.

TL;DR you need to maintain schema.prisma for your database, and maintain your entity files for your api. They are two different concerns.

Code-first only autogenerates the GraphQL api based on your entity files.