graphql-查询多个表

时间:2018-07-12 21:51:36

标签: graphql

我是graphql的新手,我需要一次查询多个表以显示一些数据。我有一个仪表板,显示来自5个表的房屋信息:地址,人,hostinfo,房间和图像。最初,我有person_id来查询包含address等的person_id表……这是对uml的简要介绍:

entity Address {
    AddressId BigInteger,
    FK_PersonId BigInteger,
    StreetNumber Integer,
    ...
}

entity HostInfo {
    HostInfoId BigInteger,
    FK_AddressId BigInt,
    HomestayName String,
    ...
}

entity Room {
    RoomId BigInteger,
    FK_HostInfoId BigInteger,
    RoomName String,
    ...
}

entity Image {
    FK_AddressId BigInt,
    FK_RoomID BigInt,
    Name String,
    ....
}

entity Person {
    PersonId,
    FirstName String,
    Age Integer required, //TODO remember to check age
}

我的问题是,如何仅使用PersonId使用graphql来获取这些表中的所有数据? 编辑 - - - - 我将typedefiniton重构如下:

    export type Address = {
      StreetNumber: number,
      //..
      Person:Person
    }

    export type HostInfo = {
      HomestayName: string,
      //..
      Person:Person,
      Room:[Room!],
      Address:Address
    }

    export type Room = {
      RoomName: string,
      //..
      RoomImage:[RoomImage!],
      HostInfo:HostInfo
    }

    export type RoomImage = {
      Name: string,
      //..
      Room:Room,
    }

    export type HostInfoImage = {
      Name: string,
      ..
      HostInfo:HostInfo
    }

    export type PersonImage = {
      Name: string,
      //..
      Person:Person
    }

export type Person = {
  FirstName: string,
  ..
  PersonImage:[PersonImage]
  Address:Address
}

并按以下方式执行查询:

query HostInfo($id: ID!) {
  node(id: $id) {
    ... on Person {
      firstName
      address {
        streetNumber
        hostInfo {
            HostName,
            //...
            Room {
                RoomName,
               [RoomImage],
               //....
           }
        }
      }
    }
  }
}

我将在我的实体中显式地显示两种关系。我期待更多的sql(ly)方式来实现。

1 个答案:

答案 0 :(得分:1)

通常,GraphQL将表示模式中显式对象之间的所有链接:如果在数据库模型中,Address引用了Person,则您的Person GraphQL类型将具有一个列表地址。在GraphQL模式语言中,您可能具有:

type Person implements Node {
  id: ID!,
  firstName: String,
  age: Int!,
  address: [Address!]!
}

如果您知道一个人的ID,则假设检索很多可用数据的典型查询看起来像是

query Person($id: ID!) {
  node(id: $id) {
    ... on Person {
      firstName
      age
      address {
        streetNumber
        hostInfo { ... }
      }
    }
  }
}