Home > other >  How to use a query result as a variable ? Hasura Flutter
How to use a query result as a variable ? Hasura Flutter

Time:01-24

I am using Hasura with my Flutter Application..

I have 2 tables: tasks and categories

tasks comprises of id, task_name, category_id, status. category comprises of id, category_name, user_id, color.

What I want to do is get the name of the category that the task belongs to using the category_id

What I thought of is:

query getTasks($user_id: String!) {
  tasks(where: user_id: {_eq: $user_id}}, order_by: {created_at: desc}) {
    category_id
    name
  }
  category_by_pk(id: tasks['category_id']){
    name
  }
}

The part that is tasks['category_id'] being passed as a query variablele is giving an error

Any idea how can I do this?

Thanks in advance

CodePudding user response:

Have you tracked a relationship in Hasura between tasks and categories? Normally with GraphQL you would just traverse the relationship to get information about the related entity:

query getTasks($user_id: String!) {
  tasks(where: user_id: {_eq: $user_id}}, order_by: {created_at: desc}) {
    name
    category { // Just follow the relationship
      id
      name
    }
  }
}
  • Related