unlight / prisma-nestjs-graphql

Generate object types, inputs, args, etc. from prisma schema file for usage with @nestjs/graphql module
MIT License
540 stars 79 forks source link

Using custom types when generating #164

Closed Luferov closed 1 year ago

Luferov commented 1 year ago

Hi! Just simple question. Could I use my own custom types in generation, for example, User model?

generator nestgraphql {
  provider = "prisma-nestjs-graphql"
  output   = "../src/@generated"
  reExport = Directories

  fields_Validator_from  = "class-validator"
  fields_Validator_input = true
  fields_Scalars_from    = "graphql-scalars"
  fields_Scalars_input   = true

  decorate_1_type      = "@(Create|Update)@(One|Many)UserArgs"
  decorate_1_field     = data
  decorate_1_name      = ValidateNested
  decorate_1_from      = "class-validator"
  decorate_1_arguments = "[]"

  decorate_2_type      = "@(User)Count"
  decorate_2_field     = "_count"
  decorate_2_name      = "HideField"
  decorate_2_from      = "@nestjs/graphql"
  decorate_2_arguments = "[]"

  // add this, for example, user custom type (use everyone via generation)
  useModel_User = "../src/users/dto/user.type.ts"
}

model User {
  id        Int     @id @default(autoincrement())
  /// @Validator.MinLength(3)
  /// @Validator.MaxLength(30)
  username  String  @unique @db.VarChar(50)
  /// @HideField()
  password  String  @db.VarChar(256)
  /// @FieldType('Scalars.GraphQLEmailAddress')
  email     String  @unique @db.VarChar(50)
  /// @FieldType('Scalars.GraphQLURL')
  avatar    String?
  /// @Validator.MinLength(3)
  /// @Validator.MaxLength(50)
  lastName  String  @map("last_name") @db.VarChar(50)
  /// @Validator.MinLength(3)
  /// @Validator.MaxLength(50)
  firstName String  @map("first_name") @db.VarChar(50)
  /// @Validator.MaxLength(50)
  sirName   String? @map("sir_name") @db.VarChar(50)

  @@map("users")
}

In order not to produce entities and expand the functionality.

unlight commented 1 year ago

You cant generate own type.

But you can extend generated class, like this one https://github.com/unlight/nest-typescript-starter/blob/master/src/user/models/user.ts Also, see https://github.com/unlight/prisma-nestjs-graphql#objecttype