Fetch Query to get the attributes directly

I’m using Java SDK, and I’m executing a fetch query, everyting runs perfectly, but is there a way (a little easier) to parse/map the fetch result to a POJO?

The thing is, I’m using this schema and data: Parent without children does not get returned - #4 by nico_bondarenco

And when I get the fetch result, it’s a JSON, but the attributes values are not directly accessible.

I understand that this is the way TypeDB works, but I’m searching for a easier way to get the “entity” representation of my model, instead of getting each attribute and iterate the array checking if there is a “value” object to get the actual value.

Using the example of my other topic, the result o the fetch is:

{
    "user": {
        "first_name": [ { "value": "Tony", "type": { "label": "first_name", "root": "attribute", "value_type": "string" } } ],
        "id": [ { "value": "90113601-0e7b-4d07-8e71-2f8f44e6fc9f", "type": { "label": "id", "root": "attribute", "value_type": "string" } } ],
        "last_name": [ { "value": "Stark", "type": { "label": "last_name", "root": "attribute", "value_type": "string" } } ],
        "main_email": [ { "value": "iron.man@email.com", "type": { "label": "main_email", "root": "attribute", "value_type": "string" } } ],
        "profile_photo": [  ],
        "type": { "label": "user", "root": "entity" }
    },
    "user_roles": [
        {
            "role": {
                "role_authority": [ { "value": "ROLE_USER_CREATE", "type": { "label": "role_authority", "root": "attribute", "value_type": "string" } } ],
                "type": { "label": "role", "root": "entity" }
            }
        },
        {
            "role": {
                "role_authority": [ { "value": "ROLE_USER_READ", "type": { "label": "role_authority", "root": "attribute", "value_type": "string" } } ],
                "type": { "label": "role", "root": "entity" }
            }
        },
        {
            "role": {
                "role_authority": [ { "value": "ROLE_USER_UPDATE", "type": { "label": "role_authority", "root": "attribute", "value_type": "string" } } ],
                "type": { "label": "role", "root": "entity" }
            }
        }
    ]
}

Is there a way to get the result as something like this:
Or some Java class that do the parser more easily?

{
  "user": {
    "id": "90113601-0e7b-4d07-8e71-2f8f44e6fc9f",
    "first_name": "Tony",
    "last_name": "Stark",
    "main_email": "iron.man@email.com", 
    "profile_photo": null
  },
  "user_roles": [
    "ROLE_USER_CREATE",
    "ROLE_USER_READ",
    "ROLE_USER_UPDATE"
  ]
}

Great question -
this is one of the concerns with the Fetch operation in 2.x that we’re planning in addressing in 3.0!

In 3.0, the fetch structure will be more general and powerful, and more structural/easy to read what output shape will arrive:

match
...
fetch {
  "key-1": $x.name,
  "random-sub-object": [
    match ...
    fetch {...}
  },
  # the one you want, returns { "attr-type-1": attribute-value-1, "attribute-type-2": attribute-value-2, ... }
  "x-repr": { $x.* }
}

To get some, not all

"x-repr": { 
    "name": $x.name,
    "age": $x.age,
    "id": $x.id 
  }

Fetches of attributes will return just attribute values, instead of the entire nested structure with value and type!

So, in short, in 2.x this isn’t something we can do… in 3.0 it will be much more flexible intuitive!


Your intuition for what is the easy-to-use data structure makes me think there’s probably more room for syntactic sugar:

However your instinct that that is the best shape you want to see makes me think we should generalise the { $x.* } to also allow a syntactic sugar for the full form:

"x-repr": { 
    $x.name,
    $x.age,
    $x.id 
  }

We will consider it!

Thanks Joshua, I will follow the 3.0 roadmap! :smiley: