Reference field to any Persistable type instance

#1

I would like to define a field on my type which can hold a reference to any Persistable object. My requirement is that I need to be able to filter on both the type name and the id.

I tried the following solutions:

entity type MyType {
  parent: anyof(Persistable)
}

But I encountered an error when fetching with a filter:

MyType.fetch({filter: Filter.eq('parent.id', '11851aaf-5a42-4d2f-e1ae-0160e9b54cbd-1')});
Error: wrapped RuntimeException: Field 'id' does not exist in expression: 'meta.tenantTagId == 9 && (parent.id == "11851aaf-5a42-4d2f-e1ae-0160e9b54cbd-1")' for type 'MyType'.

Then I tried this:

entity type MyType {
  parent: Ref
}

But I encountered a different error when fetching with a filter:

MyType.fetch({filter: Filter.eq('parent.id', '11851aaf-5a42-4d2f-e1ae-0160e9b54cbd-1')});
Error: Filtering on fields with an included reference type not supported: parent

Obviously a workaround is to define two different fields:

entity type MyType {
  parentRef: TypeRef
  parentId: string
}

And filter on those: Filter.eq('parentRef.typeName', 'AnotherType').and.eq('parentId', 'some_id')

But I’m wondering if there is a more elegant solution.

Note that a parametric type will not work for this solution because MyType.parent should be able to refer to ANY Persistable object, depending on the application’s / users’ needs at runtime.

0 Likes