redwoodjs / redwood

The App Framework for Startups
https://redwoodjs.com
MIT License
17.31k stars 994 forks source link

[Bug?]: Prisma columns set with @default are required in generated Create*Input SDL #6761

Open ketang opened 2 years ago

ketang commented 2 years ago

What's not working?

I have multiple model fields that have default values for ID generation, timestamps, etc., e.g.:

model TestCase {
  someId String @id @default(cuid())
  requiredValue String
  optionalValue String?
  created DateTime @default(now())
}

yarn rw sdl TestCase generates the core type as expected:

  type TestCase {
    someId: String!
    requiredValue: String!
    optionalValue: String
    created: DateTime!
  }

However the input type marks all defaulted fields as required (while recognizing the nullable field as optional):

  input CreateTestCaseInput {
    someId: String!
    requiredValue: String!
    optionalValue: String
    created: DateTime!
  }

What it should do instead is either mark those fields as optional

  input CreateTestCaseInput {
    someId: String
    requiredValue: String
    optionalValue: String
    created: DateTime
  }

Or, as a lesser alternative, they could be omitted entirely from the input type:

  input CreateTestCaseInput {
    optionalValue: String
  }

How do we reproduce the bug?

$ yarn create redwood-app optional-test-case
# ... output omitted
$ cd optional-test-case/
$ cat >> api/db/schema.prisma
model TestCase {
  someId String @id @default(cuid())
  requiredValue String
  optionalValue String?
  created DateTime @default(now())
}
# ctrl-d
$ yarn rw g sdl TestCase
$ cat ./api/src/graphql/testCases.sdl.ts

Should produce output that includes:

  input CreateTestCaseInput {
    someId: String!
    requiredValue: String!
    optionalValue: String
    created: DateTime!
  }

What's your environment? (If it applies)

$ yarn rw info
  System:
    OS: Linux 5.10 Ubuntu 20.04 LTS (Focal Fossa)
    Shell: 5.0.16 - /bin/bash
  Binaries:
    Node: 16.17.0 - /tmp/xfs-96c05dd3/node
    Yarn: 3.2.3 - /tmp/xfs-96c05dd3/yarn
  npmPackages:
    @redwoodjs/core: 3.2.1 => 3.2.1

Are you interested in working on this?

dthyresson commented 1 year ago

mark those fields as optional

I prefer the "mark those fields as optional" approach because I may want to correct or change a defaulted value (whether it is a timestamp or a default enum, etc).

dthyresson commented 1 year ago

Also, how should the Prisma "@updated" directive be handled? This is an implicit default.

I will look into the scenario generator as well as the SDL generator since it may try to assign values in its mocks.

ahmadbakhshi commented 1 year ago

I think it would be nice to use comment to inform generator what to skip. like what prisma-nestjs-graphql is doing.

Primsa schema

model Community {
  id           String             @id @default(cuid())
  /// @HideField({ input: true, output: false })
  createdAt    DateTime           @default(now())
  /// @HideField({ input: true, output: false })
  updatedAt    DateTime           @updatedAt
  /// @HideField({ match: 'CreateInput' })
  isDeleted    Boolean            @default(false)
  /// @HideField({ match: 'CreateInput' })
  isActive     Boolean            @default(true)
  title        String
  description  String?
  category     CommunityCategory  @relation(fields: [categoryId], references: [id])
  categoryId   Int
  members      CommunityMember[]
  /// @HideField({ input: true, output: false })
  createdBy    User               @relation("CommunityCreatedBy", fields: [createdById], references: [id])
  /// @HideField({ input: true, output: false })
  createdById  String
  /// @HideField({ input: true, output: false })
  modifiedBy   User               @relation("CommunityModifiedBy", fields: [modifiedById], references: [id])
  /// @HideField({ input: true, output: false })
  modifiedById String
  Event        Event[]
  location     CommunityLocation?
}

You can see that I am defining which fields to be part of "input type" or "object type"