首页 > 解决方案 > Graphql 属于关系

问题描述

所以我正在学习 GraphQL,并且我试图在构建模式时了解我是如何处理关系的。所以我有一个带有字段 show 的 Apps 表,其中设置了 Show 表的外键。如何在 graphql 模式中执行此操作?这就是我目前所拥有的

const schema = buildSchema(`
  type Query {
    app: App
  }
  type App {
    id: Int
    show: Int
    name: String
    url: String
    author: String
    price: Float
    image: String
  }
  type Show {
    id: Int
    name: String
    airDate: String
  }
`);

也许我需要一些东西来告诉它这段关系?谢谢

标签: graphql

解决方案


您通常会设置 GraphQL 架构来引用另一个对象,并且在引用者中根本没有它的 ID:

type App {
  id: Int
  show: Show
  ...
}
type Show {
  id: Int
  name: String
  ...
}

这样,查询就不必知道涉及到中间数据库查找

query GetApp {
  app {
    name
    url
    show { name }
  }
}

推荐阅读