Session-Aware Computed Fields
Add session-aware computed fields to a table, with a community membership example
computed fields session argument session-aware Postgres functions per-user state communities exampleTwo session-aware computed fields on the communities table in the react-demo:
is_member—boolean. Does the current user belong to this community?my_joined_at—timestamptz. When did the current user join, ornull.
See Communities.tsx for the UI, and Computed Fields for the underlying feature.
The SQL functions
Section titled “The SQL functions”CREATE OR REPLACE FUNCTION public.community_is_member( community_row public.communities, user_session json)RETURNS booleanLANGUAGE sqlSTABLEAS $$ SELECT EXISTS ( SELECT 1 FROM public.community_members WHERE community_members.community_id = community_row.id AND community_members.user_id = (user_session ->> 'x-hasura-user-id')::uuid );$$;
CREATE OR REPLACE FUNCTION public.community_my_joined_at( community_row public.communities, user_session json)RETURNS timestamptzLANGUAGE sqlSTABLEAS $$ SELECT joined_at FROM public.community_members WHERE community_members.community_id = community_row.id AND community_members.user_id = (user_session ->> 'x-hasura-user-id')::uuid LIMIT 1;$$;Registering the computed fields
Section titled “Registering the computed fields”session_argument: user_session binds the request’s session JSON to the function’s user_session parameter and hides it from the GraphQL schema.
computed_fields: - name: is_member definition: function: name: community_is_member schema: public session_argument: user_session comment: Whether the current user is a member of this community - name: my_joined_at definition: function: name: community_my_joined_at schema: public session_argument: user_session comment: When the current user joined this community, or null if they aren't a memberIn the dashboard: Database → communities → Edit GraphQL → Computed Fields. Set Session Argument to user_session for both.

Granting access
Section titled “Granting access”select_permissions: - role: user permission: columns: - id - name - description - created_at - updated_at computed_fields: - is_member - my_joined_at filter: {}See Permissions.
Querying
Section titled “Querying”query GetCommunities { communities { id name description is_member my_joined_at }}Using the values in the UI
Section titled “Using the values in the UI”
is_member drives Join/Leave; my_joined_at renders as a small muted line under Leave for member rows:
const isMember = (community: Community) => community.is_member;
// ...
<td> {isMember(community) ? ( <> <button type="button" className="btn btn-secondary" onClick={() => handleLeave(community.id)} > Leave </button> {community.my_joined_at && ( <div style={{ fontSize: '0.8em', opacity: 0.7, marginTop: '4px' }}> joined {formatRelativeTime(community.my_joined_at)} </div> )} </> ) : ( <button type="button" className="btn btn-primary" onClick={() => handleJoin(community.id)} > Join </button> )}</td>Related resources
Section titled “Related resources”- Computed Fields — the underlying feature
- Permissions — granting role access to a field
- Full source — Communities.tsx on GitHub