使用graphql查询连接3个表

时间:2017-11-12 07:49:17

标签: graphql graphql-js express-graphql

我想加入3个表,就像我们在基于主键和外键的mysql中一样。

我可以使用graphql(http://graphql.org/)这样做吗? 我的表结构和graphql查询如下。感谢

query($companyId:String){
  Data{
    reach{
     department {
       departmentId
       departmentName
       description
     }
      companyDepartment(companyId:$companyId) {
        primaryId
        departmentId
        companyId
        createdDate
        modifiedDate
        modifiedBy
      }
      company(companyId:$companyId) {
        companyId
        companyName
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

你必须打破思维,用Graph方式模型思考:)

类型公司(节点)< - CompanyDepartmentConection(连接边缘的名称) - >输入部门(节点)

基于这个非常有用的article,无论如何我会为你做的,例如。模式

概念

interface Node {
  id: ID!
  name: String
}

type Company implements Node {
  id: ID!
  name: String
  departmentsConnection: CompanyDepartmentConnection
}
type CompanyDepartmentConnection {
  pageInfo: PageInfo!
  edges: [CompanyDepartmentEdge]
}
type CompanyDepartmentEdge {
  cursor: String!
  node: Company
  linkedAt: DateTime
}