使用AWS AppSync在GraphQL中建立正确的多对多关系吗?

时间:2019-12-23 07:13:09

标签: graphql aws-amplify aws-appsync

我正在使用AWS Amplify及其AppSync创建家族树API。 FamilyTree基于具有父母,子女和婚姻的人。婚姻是我苦苦挣扎的事情。首先,这是GraphQL Transform(“模式”)的输入:

type Person @model {
  id: ID!
  firstName: String!
  marriages: [Married] @connection(name: "marriedToA")
  marriedTo: [Married] @connection(name: "marriedToB")
  parents: [Related] @connection(name: "parentOf")
  children: [Related] @connection(name: "childOf")
}

type Related @model {
  id: ID!
  parent: Person @connection(name: "childOf")
  child: Person @connection(name: "parentOf")
}

type Married @model {
  id: ID!
  partnerA: Person @connection(name: "marriedToB")
  partnerB: Person @connection(name: "marriedToA")
  dateMarried: String!
}

尽管针对孩子和父母的多对多解决方案效果很好(尽管有些嵌套),但婚姻并不能真正解决问题。所以当我这样查询时:

query ListPersons {
  listPersons(limit: 10) {
    items {
      firstName
      children {
        items {
          child {
            firstName
          }
        }
      }
      parents {
        items {
          parent {
            firstName
          }
        }
      }
      marriages {
        items {
          partnerA {
            firstName
          }
          partnerB {
            firstName
          }
        }
      }
      marriedTo {
        items {
          partnerA {
            firstName
          }
          partnerB {
            firstName
          }
        }
      }
    }
  }
}

我得到以下结果:

{
  "data": {
    "listPersons": {
      "items": [
        {
          "firstName": "Bob",
          "children": {
            "items": []
          },
          "parents": {
            "items": [
              {
                "parent": {
                  "firstName": "John"
                }
              }
            ]
          },
          "marriages": {
            "items": []
          },
          "marriedTo": {
            "items": []
          }
        },
        {
          "firstName": "John",
          "children": {
            "items": [
              {
                "child": {
                  "firstName": "Bob"
                }
              },
              {
                "child": {
                  "firstName": "Kyle"
                }
              }
            ]
          },
          "parents": {
            "items": []
          },
          "marriages": {
            "items": []
          },
          "marriedTo": {
            "items": [
              {
                "partnerA": {
                  "firstName": "John"
                },
                "partnerB": {
                  "firstName": "Jane"
                }
              }
            ]
          }
        },
        {
          "firstName": "Kyle",
          "children": {
            "items": []
          },
          "parents": {
            "items": [
              {
                "parent": {
                  "firstName": "John"
                }
              }
            ]
          },
          "marriages": {
            "items": []
          },
          "marriedTo": {
            "items": []
          }
        },
        {
          "firstName": "Jane",
          "children": {
            "items": []
          },
          "parents": {
            "items": []
          },
          "marriages": {
            "items": [
              {
                "partnerA": {
                  "firstName": "John"
                },
                "partnerB": {
                  "firstName": "Jane"
                }
              }
            ]
          },
          "marriedTo": {
            "items": []
          }
        }
      ]
    }
  }
}

因此,现在客户逻辑将始终必须检查结婚已结婚字段。我真正想要的是,一个字段,用于该人的婚姻列表,我可以通过该字段查询伴侣。使用Amplify和AppSync可以实现吗?

0 个答案:

没有答案