Picture of the author

Basics

Resolving Entities

On some occations when querying Notion databases, blocks and pages you want to retreive information on parents, relations or users. Usually only the id and type are given, however informations like title or further attributes is not available.

That is why we created the endpoint Notion::resolve.

Accessing Parents

When getting the parent of an entity, it will always be a NotionParent::class. This NotionParent::class can be used directly or indirectly to resolve the full entity of this parent.

$page = Notion::pages()->find('91f70932-ee63-47b5-9bc2-43e09b4cc9b0');
$notionParent = $page->getParent();

Resolving Parents

Blocks, databases and pages always have a parent. The parents are mostly pages, blocks and databases and sometimes workspaces.

The resolved $parent will be on of the following types for parents of Page::class:

  • Page::class
  • Block::class
  • Database::class
  • NotionParent::class
$page = Notion::pages()->find('91f70932-ee63-47b5-9bc2-43e09b4cc9b0');
$parent = Notion::resolve()->parentOf($page);

Workspace is not resolvable

Currently there is no endpoint for workspaces within the Notion API.

That is why - if workspace is a parent - the NotionParent::class instance will be returned.

The resolved $parent will be on of the following types for parents of Block::class:

  • Block::class
  • Page::class
$block = Notion::block('d5f9419b44204c909501b1e2b7569503')->retrieve();
$parent = Notion::resolve()->parentOf($block);

It is also possible to resolve the NotionParent::class directly.

$page = Notion::pages()->find('91f70932-ee63-47b5-9bc2-43e09b4cc9b0');
$notionParent = $page->getParent();
$parent = Notion::resolve()->parent($notionParent);

In order to check the dynamic parent type, various methods can help.

$page = Notion::pages()->find('91f70932-ee63-47b5-9bc2-43e09b4cc9b0');

if($page->getParent()->isBlock()){
        //...
}
else if($page->getParent()->isDatabase()){
       //...
}
else if($page->getParent()->isPage()){
       //...
}
else if($page->getParent()->isWorkspace()){
       //...
}

If you rather use switch, you can use the getParentType() method.

$page = Notion::pages()->find('91f70932-ee63-47b5-9bc2-43e09b4cc9b0');

switch($page->getParentType()){
    case: 'block':
            //...
            break;
    case: 'database':
            //...
            break;
        //...
}

Resolving Relations

The relation within database properties only returns a list of the ids of related pages.

By resolving these properties, the hassle of iterating through them yourself is not required.

$page = Notion::pages()->find('1c56e2ad-3d95-458c-935d-ae6d57769037');

// Collection of ids
$relationPropertyItems = $page->getProperty('Parent Relation Database');

// Collection of `Notion::class`
$relationPages = Notion::resolve()->relations($relationPropertyItems);

In case you only need the titles, we have a solution for that too.

$page = Notion::pages()->find('1c56e2ad-3d95-458c-935d-ae6d57769037');

// Collection of ids
$relationPropertyItems = $page->getProperty('Parent Relation Database');

// Collection of strings
$relationPages = Notion::resolve()->relations($relationPropertyItems, true);

Potential loading time and request limit

Since each page has to be retreived by the API, it can lead to request limit errors and long loading time.

Please be aware: In case you have more than about 15 relation items in your page relation property, there needs to be some kind of custom processing.

Currently there is unfortunately no bulk retrieval endpoint within the Notion API.

Resolving Users

Database properties like CreatedBy, LastEditedBy, and People only return the id.

In case you want the full User instance, it requires fetching it.

To make this a process smoother, we added Notion::resolve()->user(), which returns the full User instance for these properties.

$page = Notion::pages()->find('8890c263-e97c-4533-9ef5-616d5e75360e');

$createdBy = $page->getProperty('Created by');
$lastEditedBy = $page->getProperty('Last edited by');
$person = $page->getProperty('Person');

$createdByUser = Notion::resolve()->user($createdBy->getUser());
$lastEditedByUser = Notion::resolve()->user($lastEditedBy->getUser());
$personUser = Notion::resolve()->user($person->getPeople()->first());
Previous
Handling Results