引用多字段(一对多关系)。

问题描述 投票:0回答:1

我正在做一个项目,在这个项目中,我必须创建一对多的关系,这将得到所有的记录列表,在另一个表的id引用,我必须显示所有选择的数据在多选择字段。(selectArrayInput). 请帮我解决这个问题,如果你能帮我举个例子那就太好了.先谢谢你了。

Example:
district
id    name
1     A
2     B
3     C

block
id    district_id    name
1     1              ABC
2     1              XYZ
3     2              DEF

我使用的是 https://github.com/Steams/ra-data-hasura-graphql hasura-graphql数据供应商 我的应用程序。

graphql react-admin hasura
1个回答
1
投票

你可能正在寻找 "嵌套对象查询"(见。https:/hasura.iodocs1.0graphqlmanualqueriesnested-object-queries.html#nested-object-queries。)

一个例子...

query MyQuery {
  district(where: {id: {_eq: 1}}) {
    id
    name
    blocks {
      id
      name
    }
  }
}

结果。

{
  "data": {
    "district": [
      {
        "id": 1,
        "name": "A",
        "blocks": [
          {
            "id": 1,
            "name": "ABC"
          },
          {
            "id": 2,
            "name": "XYZ"
          }
        ]
      }
    ]
  }
}

或者...

query MyQuery2 {
  block(where: {district: {name: {_eq: "A"}}}) {
    id
    name
    district {
      id
      name
    }
  }
}

结果:

{
  "data": {
    "block": [
      {
        "id": 1,
        "name": "ABC",
        "district": {
          "id": 1,
          "name": "A"
        }
      },
      {
        "id": 2,
        "name": "XYZ",
        "district": {
          "id": 1,
          "name": "A"
        }
      }
    ]
  }
}

这样设置表格...

块。

enter image description here

enter image description here

enter image description here

districts:

enter image description here

enter image description here

enter image description here

另外,我建议使用复数的表名,因为它们更标准,"区 "和 "块"。

© www.soinside.com 2019 - 2024. All rights reserved.