-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Current user subscription #4112
Comments
@Maxpain177 Any reason you aren't setting the |
@0x777 any insights on the subscriptions bit? |
@rikinsk Because we want to get not only current user from |
Can you get the user id from your auth server, and query users using that and 2 different subscriptions / queries? subscription currentUser {
currentUser:user(where: {id: {_eq: 1}}) {
id
email
}
}
subscription otherUsers {
otherUsers:user(where: {id: {_neq: 1}}) {
id
email
}
} |
I agree with @kelly-ry4n: why not just look up the current user’s id and create a subscription on |
@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: create function get_current_user(hasura_session json) returns setof users as $$
select * from users where id = (hasura_session ->> 'x-hasura-user-id')::Int
$$ language sql stable; Console doesn't yet support tracking functions which require session variables so you'll need to manually hit {
"type": "track_function",
"version": 2,
"args": {
"function": {
"schema": "public",
"name": "get_current_user"
},
"configuration": {
"session_argument": "hasura_session"
}
}
} or it can also be made part of metadata as follows: {
"version": 2,
"tables": [
{
"table": {
"schema": "public",
"name": "users"
}
}
],
"functions": [
{
"function": {
"schema": "public",
"name": "get_current_user"
},
"configuration": {
"session_argument": "hasura_session"
}
}
]
} Using viewsTo avoid the cons that you listed, you just need to create an object relationship to the users table. So this will be the workflow:
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 query MyQuery {
users(where: { isCurrentUser: {} }) {
id
stats {
...
}
}
} that where clause can be used in any larger query where I need to filter for the current user. |
@0x777 method 2 is failed for 2.20 the relationships page is failed with follow request POST /v1/metadata {"type":"pg_suggest_relationships","args":{"omit_tracked":true,"tables":[null],"source":"App"}} {
"code": "parse-failed",
"error": "Error when parsing command suggest_relationships.\nSee our documentation at https://hasura.io/docs/latest/graphql/core/api-reference/metadata-api/index.html#metadata-apis.\nInternal error message: \n Previous branch failure: Error in $: expected Null, but encountered Array\n Previous branch failure: Error in $: parsing PostgresQualified_TableName failed, expected Object, but encountered Null\nparsing Text failed, expected String, but encountered Null",
"path": "$.args.tables[0]"
}
|
We 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:So, we have two ways to do that:
1. Make a custom function, using
X-Hasura-User-Id
session variable.Pros:
users
)Cons:
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:
Cons:
users_current
instead ofusers
), so we can't reuse cache and the same fragments on our react/apollo frontend.What we can do?
The text was updated successfully, but these errors were encountered: