graphql-nexus / nexus-plugin-prisma

Deprecated
MIT License
828 stars 118 forks source link

Prisma nexus crud not generating schema correctly #1123

Open facinick opened 2 years ago

facinick commented 2 years ago

I am using prisma and nexus with the following prisma schema:

    model User {
      id           String    @id @default(uuid())
      createdAt    DateTime  @default(now())
      updatedAt    DateTime  @updatedAt
      username     String    @unique
      posts        Post[]
    }

    model Post {
      id          String   @id @default(uuid())
      createdAt   DateTime @default(now())
      updatedAt   DateTime @updatedAt
      title       String
      description String
      author      User     @relation(fields: [userId], references: [id])
      userId      String
    }

and I have created nexus models and mutation as such:

    export const Post = objectType({
        name: 'Post',
        definition(t) {
            t.model.id();
            t.model.title();
            t.model.description();
            t.model.userId();
            t.model.author();
            t.model.updatedAt();
            t.model.createdAt();
        },
    });

    export const PostMutations = extendType({
        type: 'Mutation',
        definition(t) {
            t.crud.createOnePost();
        },
    })

I should be able to run a graphql mutation to create a single post with only userId, title and description. However the types nexus is generating makes it as such that author field is mandatory in the query.

following is generated by nexus:

    type Mutation {
      createOnePost(data: PostCreateInput!): Post!
    }

    input PostCreateInput {
      author: UserCreateNestedOneWithoutPostsInput!
      createdAt: DateTime
      description: String!
      id: String
      title: String!
      updatedAt: DateTime
    }

note 1: I have noticed the following type generated by nexus but it's not being used anywhere:

    input PostCreateWithoutAuthorInput {
      createdAt: DateTime
      description: String!
      id: String
      title: String!
      updatedAt: DateTime
    }

note 2: I've only added relevant code from my source

Could someone please help me figure out what can I do here?

libraries used: