1

我将 Apollo Server、Nexus 和 Prisma 组合用于我的 graphql 服务器。我想创建一个有多个帖子的用户。但不应允许用户在帖子中设置 isPublished 字段。直接创建帖子时,我可以使用computedInputs但我不确定如何在关系创建中获得它。

如何抛出错误或忽略传递的 isPublished 属性?

Prisma Schema:

model User {
  id       Int     @id @default(autoincrement())
  name     String
  email    String
  posts    Post[]
}

model Post {
  id          Int     @id @default(autoincrement())
  content     String
  isPublished Boolean
  user        User?   @relation(fields: [userId], references: [id])
  userId      Int?
}



Nexus Mutation Definitions

const User = objectType({
  name: "User",
  definition(t) {
    t.model.id();
    t.model.email();
    t.model.name();
    t.model.posts();
  },
});

const UserMutation = extendType({
  type: "Mutation",
  definition(t) {
    t.crud.createOneUser({
      alias: "createUser",
      inputs: {
        posts: {
          relateBy: "create",
        },
      }
    });
  },
});



Graphql Mutation

mutation createNewUser {
  createUser(
    data: {
      name: "User1"
      email: "user1@mail.com"
      posts: {
        create: {
          content: "New Content"
          isPublished: true
        }
      }
    }
  ) {
    id
    name
    email
  }
}
4

0 回答 0