【问题标题】:Join tables via graphql通过 graphql 连接表格
【发布时间】:2021-08-13 14:44:19
【问题描述】:

我是 graphql 的新手,无法找到简单问题的解决方案。
假设有 POJO:

class Book {
  String title;
  String authourName;
}

class Author {
  String name;
  Integer yearOfBirth;
}
type Book {
    title: String
    authourName: String
}

type Author {
    name: String
    yearOfBirth: String
}

我如何指定一个查询,我可以在 book.authorName = book.name 上加入 Book with Author:

BookWithAuthor(authorName: "xxx") {
  title
  authourName
  yearOfBirth
}

?

【问题讨论】:

  • 你不能在Book 中使用author: Author 而不是authorName: String 吗?然后查询将是{ title { author { name, yearOfBirth } } }
  • 但是如何在不将新数据保存到数据库的情况下指定关系book.authorName = author.name?这只是我的实际问题的一个简单示例:我有很多表并希望将它们加入到一个 graphql 查询中

标签: java graphql


【解决方案1】:

您可以在 Book 类型中包含 Author 为:

type Author {
    name: String
    yearOfBirth: String
}

type Book {
    title: String
    author: Author
}

然后查询 Book 为:

Book(authorName: "xxx") {
  title
  Author{
     name
     yearOfBirth
  }
}

【讨论】:

    猜你喜欢
    • 2020-05-02
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多