使用主键的Hasura where子句与特定主键查询

问题描述 投票:0回答:1

我在我的 postgres 数据库上使用 Hasura Graphql api。

我想使用主键从表中获取一行,但似乎有两种方法可以使用 Hasura 执行此操作,我正在尝试决定采用哪种方式。

以下两种方法之间有什么我应该注意的区别吗?

  1. https://hasura.io/docs/latest/queries/postgres/simple-object-queries/#fetch-an-object-using-its-primary-key
query {
  authors_by_pk(id: 1) {
    id
    name
  }
}
  1. https://hasura.io/docs/latest/queries/postgres/filters/index/#the-where-argument
query {
  authors(where: {id: {_eq: 1}}) {
    id
    name
  }
}
graphql filtering primary-key hasura
1个回答
0
投票
  1. 第一个方法实际上应该是
    author_by_pk
    ,因为它只能返回 0 或 1 个作者
  2. 如果按
    id
    搜索,第二种方法将返回一个包含零个或一个元素的数组。然而,它更灵活,因为如果给出更广泛的
    where
    查询,它可以返回许多 [0:N] 个作者。

在 GraphQL 实践中,通常最终会创建返回单例的查询以及返回数组的查询。

© www.soinside.com 2019 - 2024. All rights reserved.