2
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)
}

如果我的Category级别没有决定怎么办,我正在使用Prisma ORM 和 MongoDB。

4

1 回答 1

8

不确定我是否正确理解了您的问题。您能否更详细地说明您要做什么?

你是不是在尝试任意深度嵌套的自我关系?然后你可以做这样的事情:

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
  }
}

查询类别会给你这个响应:

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"
        }
      }
    ]
  }
}

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

于 2019-01-03T10:33:21.457 回答