GraphQL - Select 每个项目返回列表的不同字段

GraphQL - Select different fields of a returned list per item

假设我有以下查询:

query getBooksQuery($userId: String) {
  getBooks(userId: $userId) {
    id
    description
    image
    user {
      email
      firstName
      lastName
      image
    }
  }
}

假设我必须显示一个包含用户书籍的列表,并在列表上方显示作者详细信息。因为我正在查询 userId,所以我知道所有的书都属于同一个用户。 因此,我不想为列表中的每本书获取相同的用户详细信息,而是在响应中只获取一次。 有什么方法可以声明我只想要第一本书的这些信息,即?

或者这是在响应中包含两个对象的唯一方法,例如:

query getBooksQuery($userId: String) {
  getBooks(userId: $userId) {
    books {
      id
      description
      image
    }
    user {
      email
      firstName
      lastName
      image
    } 
  }
}

不幸的是,第二个解决方案,即使此时更清晰,也需要服务器端工作来支持此自定义查询。所以我在想使用当前的 api 是否仍然可行。 (我也可以做两个请求,一个给用户,一个给书,但是嗯...)

我正在使用没有中继的 Apollo,但我现在正在试验,所以如果在 Apollo 中不可行但在其他方面可行我仍然感兴趣

您可以在一个请求中对 2 'subqueries' 使用相同的参数

query getBooksQuery($userId: String) {
  getBooks(userId: $userId) {
    books {
      id
      description
      image
    }
  }
  getUsers(userId: $userId) {
    user {
      email
      firstName
      lastName
      image
    } 
  }
}