Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Issue: Nested CreateWithout[Relation]Input requires mandatory ID fields despite create or connectOrCreate usage #170

Closed
StephanDecker opened this issue Jan 9, 2024 · 7 comments
Assignees
Labels
is: bug Something isn't working package: generator Generator package

Comments

@StephanDecker
Copy link

StephanDecker commented Jan 9, 2024

Great work! Before updating to the official 1 release it worked smoothly. After updating it seems that ID fields that are auto-populated by the database are mandatory when executing the create* mutations. That means: When creating a record I have to handover the id although the id is autogenerated in the database:
Here is the example:

schema.prisma:

model Phase {
    id            String          @id @default(uuid())
    name          String          @unique
}
...?

generated schema.gql (using 1.0.0-rc.5):

input PhaseCreateInput {
	name: String!
}

generated schema.gql (after upgrade to 1.0.1):

input PhaseCreateInput {
    id: String!
    name: String!
}

Could you fix that? Thanks!

PS: That issue has been already solved in an earlier version: #62

@maoosi
Copy link
Owner

maoosi commented Jan 9, 2024

Thanks @StephanDecker ! Using v1.0.1, with the below Prisma model:

model Phase {
    id   String @id @default(uuid())
    name String @unique
}

The GraphQL output I get is:

input PhaseCreateInput {
    id: String
    name: String!
}

Which is expected to both allow you to create with or without specifying id.

What version of Prisma Client are you using? (it should display when running npx prisma generate)

@maoosi maoosi added is: waiting for details Not enough information package: generator Generator package labels Jan 9, 2024
@StephanDecker
Copy link
Author

StephanDecker commented Jan 10, 2024

Yes, depending on the field definition (e.g. autoincrement or uuid) it should create the record with or without specifying id.

In my example it should be:

input PhaseCreateInput {
	name: String!
}

That has been already been implemented in a nice way before, see this commit:
bc7320d
Or this code here:

private isFieldImmutable(searchField: DMMF.Field): boolean {
const defaultValue: any = searchField?.default || null
return (
defaultValue?.name === 'autoincrement'
|| defaultValue?.name === 'uuid'
|| searchField.isUpdatedAt
|| ['updatedAt', 'createdAt'].includes(searchField.name)
)
}

But I don't know where to put it now (after the refactoring) ;-)

Version details:
Prisma Client (v5.7.1)
Prisma-AppSync (1.0.1)

@maoosi
Copy link
Owner

maoosi commented Jan 10, 2024

Hey @StephanDecker, not having id entirely in the output was a mistake that's been fixed as part of the v1.0.0 release.

However, you should be able to create a record with or without specifying id.

On my end, the generated GraphQL output is the correct one:

input PhaseCreateInput {
    id: String
    name: String!
}

The above allows you to either specify or omit id entirely.

Could you please double-check that the output on your end is id: String! and not id: String as in my example above?

@StephanDecker StephanDecker changed the title Issue with generated GraphQL input CreateInput when using @default(uuid()) inside Prisma Schema Issue with generated GraphQL input when using downstream model references Jan 10, 2024
@StephanDecker
Copy link
Author

Sorry, you are completely right. I tried to simplify the problem which is different and didn't work. Unfortunately it's more complicated.
Let's say you have these models:

model ExampleTable {
    id       String     @id @default(uuid())
    refTable RefTable[]
}

model RefTable {
    id                   String             @id @default(uuid())
    downstreamRefTable   DownstreamRefTable @relation(fields: [downstreamRefTableId], references: [id])
    downstreamRefTableId String
    exampleTable         ExampleTable       @relation(fields: [exampleTableId], references: [id])
    exampleTableId       String
}

model DownstreamRefTable {
    id       String     @id @default(uuid())
    name     String     @unique
    refTable RefTable[]
}

It will generate this in schema.gql

type Mutation {
  createExampleTable(
      data: ExampleTableCreateInput!
  ): ExampleTable!
}
    
input ExampleTableCreateInput {
    id: String
    refTable: ExampleTableRefTableCreateNestedInput
}    
    
input ExampleTableRefTableCreateNestedInput {
    connect: [RefTableWhereUniqueInput!]
    create: [RefTableCreateWithoutExampleTableInput!]
    connectOrCreate: [RefTableConnectOrCreateWithoutExampleTableInput!]
}

input RefTableCreateWithoutExampleTableInput {
    id: String
    downstreamRefTable: RefTableDownstreamRefTableCreateNestedInput
    downstreamRefTableId: String!
}    

The downstreamRefTableId: String! is now mandatory but it shouldn't because we have the downstreamRefTable as a reference (either by connect, create or connectOrCreate).

@maoosi maoosi changed the title Issue with generated GraphQL input when using downstream model references Issue: Nested CreateWithout[Relation]Input requires mandatory ID fields despite create or connectOrCreate usage Jan 11, 2024
@maoosi
Copy link
Owner

maoosi commented Jan 11, 2024

Got it! @StephanDecker could you please install prisma-appsync@1.0.2-preview.1 and let me know if that resolves the issue on your end?

To clarify, this issue is specifically about the requirement of mandatory ID fields in the nested CreateWithout[Relation]Input, even when using connect or connectOrCreate, correct? This isn't related to the @default directives or the CreateInput itself?

You can have a look at the fix here: e1a4ea7

@maoosi maoosi added is: bug Something isn't working and removed is: waiting for details Not enough information labels Jan 11, 2024
@maoosi maoosi self-assigned this Jan 11, 2024
@StephanDecker
Copy link
Author

StephanDecker commented Jan 11, 2024

Thanks a lot for the quick fix!!

To clarify, this issue is specifically about the requirement of mandatory ID fields in the nested CreateWithout[Relation]Input, even when using connect or connectOrCreate, correct? This isn't related to the @default directives or the CreateInput itself?

Exactly, it's about the mandatory ID fields in the nested CreateWithout[Relation]Input. Sorry for the confusion before, I mixed that up. I tested the prisma-appsync@1.0.2-preview.1 fix and the errors are gone. :-)

@maoosi
Copy link
Owner

maoosi commented Jan 11, 2024

Thanks for confirming @StephanDecker! Fix was released as part of v1.0.2.

@maoosi maoosi closed this as completed Jan 11, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
is: bug Something isn't working package: generator Generator package
Projects
None yet
Development

No branches or pull requests

2 participants