Parent without children does not get returned

I’m trying to create a schema for user and groups, both with roles, so my schema is this (roles can be an array on both entities, but from what I know, it will be a 3.0 feature):

define

id sub attribute, value string;
is_active sub attribute, value boolean;
created_at sub attribute, value datetime;
updated_at sub attribute, value datetime;

first_name sub attribute, value string;
last_name sub attribute, value string;
main_email sub attribute, value string;
document_number sub attribute, value string;
phone_number sub attribute, value string;
profile_photo sub attribute, value string;
birth_date sub attribute, value datetime;
user_gender sub attribute, value string;

group_name sub attribute, value string;
group_description sub attribute, value string;

role_authority sub attribute, value string;

user_roles sub relation,
    relates ur_user,
    relates ur_role;

group_roles sub relation,
    relates gr_group,
    relates gr_role;

user_groups sub relation,
    relates ug_user,
    relates ug_group;

user sub entity,
    owns id @key,
    owns first_name,
    owns last_name,
    owns main_email,
    owns document_number,
    owns phone_number,
    owns profile_photo,
    owns birth_date,
    owns user_gender,
    owns is_active,
    owns created_at,
    owns updated_at,
    plays user_roles:ur_user,
    plays user_groups:ug_user;

group sub entity,
    owns id @key,
    owns group_name,
    owns group_description,
    owns is_active,
    owns created_at,
    owns updated_at,
    plays group_roles:gr_group,
    plays user_groups:ug_group;

role sub entity,
    owns role_authority @key,
    plays user_roles:ur_role,
    plays group_roles:gr_role;

I inserted some data to test, one user with roles and the other without

insert

$role_master isa role, has role_authority "MASTER";
$role_user_read isa role, has role_authority "ROLE_USER_READ";
$role_user_create isa role, has role_authority "ROLE_USER_CREATE";
$role_user_update isa role, has role_authority "ROLE_USER_UPDATE";
$role_group_read isa role, has role_authority "ROLE_GROUP_READ";
$role_group_create isa role, has role_authority "ROLE_GROUP_CREATE";
$role_group_update isa role, has role_authority "ROLE_GROUP_UPDATE";
$role_group_delete isa role, has role_authority "ROLE_GROUP_DELETE";

$user_tony isa user,
    has id "90113601-0e7b-4d07-8e71-2f8f44e6fc9f",
    has first_name "Tony",
    has last_name "Stark",
    has main_email "iron.man@email.com",
    has document_number "98765432154",
    has phone_number "16988774455",
    has birth_date 1990-04-28,
    has user_gender "CIS_MALE",
    has is_active true,
    has created_at 2024-01-01T19:28:44.981,
    has updated_at 2024-01-01T19:28:44.981;

$user_steve isa user,
    has id "46399aba-e4b8-4edc-b51f-f2700c7c944a",
    has first_name "Steve",
    has last_name "Rogers",
    has main_email "captain.america@email.com",
    has document_number "32165498700",
    has phone_number "1633221100",
    has birth_date 1990-05-10,
    has user_gender "CIS_MALE",
    has is_active false,
    has created_at 2024-01-01T19:28:44.981,
    has updated_at 2024-01-01T19:28:44.981;

----------------------------------------------------------------------------------------------

match

$user_tony isa user, has id "90113601-0e7b-4d07-8e71-2f8f44e6fc9f";

$role_user_read isa role, has role_authority "ROLE_USER_READ";
$role_user_create isa role, has role_authority "ROLE_USER_CREATE";
$role_user_update isa role, has role_authority "ROLE_USER_UPDATE";

insert

$user_role_user_read (ur_user:$user_tony, ur_role: $role_user_read) isa user_roles;
$user_role_user_create (ur_user:$user_tony, ur_role: $role_user_create) isa user_roles;
$user_role_user_update (ur_user:$user_tony, ur_role: $role_user_update) isa user_roles;

When I execute the query bellow, for Tony it returns the 3 roles, for Steve, it does not return anything.

So, what I want is to get the user with all the roles and groups (with their respective roles).
Is there a way to put the roles in some sort of array attribute?
How can I always retrieve the user (given it exists) even if it does not has any role/group?

match
$user_roles (ur_user: $user, ur_role: $role) isa user_roles;
$user isa user, has id "<user-id>";
$role isa role;
get $user, $role;
group $user;

Hi, pattern matching in TypeQL is an “all-or-nothing” match. This means that if any part of the pattern is absent (in your case, Steve’s roles), then the entire match will fail and nothing will be returned.

To match the pattern in parts, you should use a Fetch query with nested subqueries. For example:

match
  $user isa user, has id "<user-id>";
fetch
  $user: id;
  "user_roles": {
    match
      $role isa role;
      $user_roles (ur_user: $user, ur_role: $role) isa user_roles;
    fetch
      $role: role_authority;
  };
1 Like

Thanks a lot, it worked

1 Like

Just for knowledge for whoever needs, here is the full query for what I wanted to do:

match
  $user isa user, has id "<user-id>";
fetch
  $user: id, first_name, profile_photo;
  "user_roles": {
    match
      $role isa role;
      $user_roles (ur_user: $user, ur_role: $role) isa user_roles;
    fetch
      $role: role_authority;
  };
  "user_groups": {
    match
      $group isa group;
      $user_groups (ug_user:$user, ug_group: $group) isa user_groups;
    fetch
      $group: id, group_name;
      "group_roles": {
        match
          $role isa role;
          $group_roles (gr_group: $group, gr_role: $role) isa group_roles;
        fetch
          $role: role_authority;
      };
  };