graphqlhasura

How can I build query with multiple _and conditions from array in Hasura


I have the following situation in Hasura. A table with articles, a table with tags and one mapping table called articles_tags. What I'm trying to achieve is to get all the articles that have certain tags assigned to them. The query that comes to my mind is the following:

query SomeQuery {
   articles(where: {
    articles_tags: {
      _and: [
         tagId: { _eq: 1 },
         tagId: { _eq: 2 },
         ...
      ]
    }}) {
     id
   }
}

The problem with this query is that the number of items in the _and condition is hardcoded and the only way I can make it dynamic is by string operations, which as I know are marked as bad practise in GraphQl. So my question is do you know some way to pass an array of the tag ids instead of the hardcoded ones and to build the same Hasura condition? Also what are the best practises in this situation?


Solution

  • You could try passing the _and parameters as a variable, this may not be the exact syntax as I don't have your schema

    query SomeQuery($_and: [articles_tags_bool_exp!]!) {
     articles(where: {
       articles_tags: { _and: $_and }
    })