So I have create this query for my GraphiQL in Hasura, This will get all of the users data and it aims to include the total amount/count of the other data from analytics and follow table where user's id is equal to user_id from the analytics and follow table
query getAllUsersData {
users {
id
username
bio
first_name
last_name
banner
profile_image_url
created_at
followers: follow_aggregate(where: {user_id: {_eq: id}, is_follow: {_eq: 1}}) {
aggregate {
count: count
}
}
visits: analytics_aggregate(where: {user_id: {_eq: id}, type: {_eq: "visit"}}) {
aggregate {
count: count
}
}
shares: analytics_aggregate(where: {user_id: {_eq: id}, type: {_eq: "share"}}) {
aggregate {
count: count
}
}
}
}
The problem here is I wont be able to run it because of the follow_aggregate
and analytics_aggregate
, It gives me an error of Cannot query field "follow_aggragate" and "analytics_aggregate" on type "users".
I am trying this thru Hasura's project dashboard, I have check already the documentations of this from Hasura and I am still really lacking. Hoping someone would guide me into this, Would really appreciate thank you in advance.
I have already done my research, Look for steps of aggregation and also look for other methods.
What I am trying to output is:
{
users:
{
userdatahere..
followersinthere,
visitsinthere,
sharesinthere
},
{
userdatahere..
followersinthere,
visitsinthere,
sharesinthere
},
}
Or let me give you a quick structure but SQL:
SELECT u.id, u.username, u.bio, u.first_name, u.last_name, u.banner, u.profile_image_url, u.created_at,
COUNT(f.user_id) AS followers,
SUM(CASE WHEN a.type = 'visit' THEN 1 ELSE 0 END) AS visits,
SUM(CASE WHEN a.type = 'share' THEN 1 ELSE 0 END) AS shares
FROM user u
LEFT JOIN follow f ON u.id = f.user_id
LEFT JOIN analytics a ON u.id = a.user_id
GROUP BY u.id
Update 1/8/2023: I have also tried this method (which is array relationship):
- Database Table Users relationship to other tables
- Here's the column of the users table
- And this is the output users table + links table The only problem now is how can I implement it with schema graphiql
Answer: Preview Output
- I setup foreign keys
- Tracked the Untracked foreign-key relationships
- and it created array of relationship
- The I use my schema again but from
follow
tofollows
query getAllUsersData {
users {
id
username
bio
first_name
last_name
banner
profile_image_url
created_at
followers: follows_aggregate(where: {is_follow: {_eq: 1}}) {
aggregate {
count: count
}
}
visits: analytics_aggregate(where: {type: {_eq: "visit"}}) {
aggregate {
count: count
}
}
shares: analytics_aggregate(where: {type: {_eq: "share"}}) {
aggregate {
count: count
}
}
}
}
Thanks to spatialaustin