Hasura where clause using primary key vs specific primary key query

49 views Asked by At

I'm using a Hasura Graphql api on my postgres databse.

I want to get a single row from a table using the primary key but it seems like there are 2 ways of doing this with Hasura, and I'm trying to decide which way I want to do it.

Are there any differences I should be aware of between the following two methods ?

  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
  }
}
1

There are 1 answers

0
Michel Floyd On
  1. The first method should really be author_by_pk since it can only ever return 0 or 1 authors
  2. The second method will return an array with zero or one elements if searching by id. However it is more flexible in that it could return many [0:N] authors if given a broader where query.

In practice with GraphQL one often ends up creating both queries that return singletons as well as queries that return arrays.