如何使用MongoDB在Prisma ORM中创建类别及其子类别

如何使用MongoDB在Prisma ORM中创建类别及其子类别,mongodb,graphql,prisma,prisma-graphql,plumatic-schema,Mongodb,Graphql,Prisma,Prisma Graphql,Plumatic Schema,如果我的类别级别没有确定,我使用的是ORM和MongoDB。不确定我是否正确理解了您的问题。你能更详细地谈谈你想做什么吗 你是在试图任意地建立深层次的自我关系吗?然后你可以这样做: type Category { id: ID! @id name: String! } type SubCategoryLevel1 { id: ID! @id name: String! parentCategory: Category! @relation(link: INLINE) }

如果我的
类别
级别没有确定,我使用的是ORM和MongoDB。

不确定我是否正确理解了您的问题。你能更详细地谈谈你想做什么吗

你是在试图任意地建立深层次的自我关系吗?然后你可以这样做:

type Category {
  id: ID! @id
  name: String!
}

type SubCategoryLevel1 {
  id: ID! @id
  name: String!
  parentCategory: Category! @relation(link: INLINE)
}

type SubCategoryLevel2 {
  id: ID! @id
  name: String!
  parentCategory: SubCategoryLevel1! @relation(link: INLINE)
}
创建三个级别可用于此查询:

type Category {
  id: ID! @id
  name: String!
  subCategory: Category @relation(name:"SubToParent"link: INLINE)
  parentCategory: Category @relation(name: "SubToParent")
}
查询类别会给您以下响应:

mutation createCategory {
  createCategory(
    data: {
      name: "firstLevel"
      subCategory: {
        create: {
          name: "secondLevel"
          subCategory: { create: { name: "thirdLevel" } }
        }
      }
    }
  ) {
    name
  }
}

如果不能更详细地解释您的问题,我希望这会有所帮助。

如果parentId为空,则它是我的主要类别,而不是子类别。我无法查询parentId为null的类别。这会引发错误还是您不知道如何查询?当我尝试查询时,我会将所有结果存储在类别中,但我希望parentCategory的数据具有null值,我认为我在查询中遗漏了一些内容,或者显然我没有编写正确的查询。然而,我确实尝试在互联网上搜索。我试着回答下面的问题,虽然我知道这是不对的
ctx.db.query.categories({where:{id\u not:parent.parentId}},info)
query allCategories {
  categories {
    name
    subCategory {
      name
    }
    parentCategory {
      name
    }
  }
}

{
  "data": {
    "categories": [
      {
        "name": "firstLevel",
        "subCategory": {
          "name": "secondLevel"
        },
        "parentCategory": null
      },
      {
        "name": "secondLevel",
        "subCategory": {
          "name": "thirdLevel"
        },
        "parentCategory": {
          "name": "firstLevel"
        }
      },
      {
        "name": "thirdLevel",
        "subCategory": null,
        "parentCategory": {
          "name": "secondLevel"
        }
      }
    ]
  }
}