Current user subscription
See original GitHub issueWe have these tables:
users (id, user_name)
user_stats (user_id, rating, matches_count, etc.)
We want to make a subscription to the current user (with some relations, like user_stats
) with this query:
SELECT *
FROM users
WHERE id = X-Hasura-User-Id
So, we have two ways to do that:
1. Make a custom function, using X-Hasura-User-Id
session variable.
Pros:
- We get right typename (
users
) - We don’t have to recreate all permissions for all roles
- We don’t have to recreate all relations for other tables
Cons:
- This way works only for queries. In subscriptions, this way doesn’t support batching/multiplexing
So we can’t use this way due to performance reasons.
2. Make a view, using custom check ({"id":{"_eq":"X-Hasura-User-Id"}}
)
Pros:
- Subscriptions are multiplexed
Cons:
- We get different typename (
users_current
instead ofusers
), so we can’t reuse cache and the same fragments on our react/apollo frontend. - We have to recreate all permissions
- We have to recreate all relations and maintain them.
What we can do?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Detecting if the current user has an active subscription
Is there anyone with an idea of how to detect if the user has an active subscription, if it returns true then the...
Read more >WooCommerce Subscriptions check if the current user has an ...
WooCommerce Subscriptions check if the current user has an active subscription - functions.php.
Read more >Correct function to get the user's latest Woocommerce ...
I've been through all the documentation and found the wcs_get_users_subscriptions() function, which returns an array of all subscription ...
Read more >Cashier: get a user's current subscription - Laracasts
Hi, I am having some serious difficulties with Laravel Cashier. I can get the subscription created, no problem. When I try to run...
Read more >User Subscription - List - REST API (Azure API Management)
Lists the collection of subscriptions of the specified user. ... User identifier. Must be unique in the current API Management service instance. api-version....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Maxpain177
Both the solutions you suggested work (with small changes)
Using functions:
Subscriptions on functions which accept session variables as their arguments are multiplexed. The function should look something like this:
Console doesn’t yet support tracking functions which require session variables so you’ll need to manually hit
/v1/query
endpoint with this query:or it can also be made part of metadata as follows:
Using views
To avoid the cons that you listed, you just need to create an object relationship to the users table. So this will be the workflow:
create a view called
current_user
as follows:Create permission on
current_user
foruser
roles as{"user_id": {"_eq": "x-hasura-user-id"}}
Create a manual object relationship from
current_user
view touser
table so that you can access all the relationships defined on the user table. Your subscription would look something like this:You can use any of the above approaches.
In case it is useful to anyone, I’m currently doing the second solution that @0x777 suggested with the views, but with a slightly different approach. I have an object relationship from
users
tocurrent_user
labeledisCurrentUser
. That allows me to write queries that don’t start withcurrent_user
at the root. So I can do something like,that where clause can be used in any larger query where I need to filter for the current user.