Recommended way to get related items from a querystring-search?

I'm currently trying to write a frontend using the REST API and would like to list some items based on a call to @querystring-search. As expected, the list of items returned does not include any related items on it as these are not returned with the brain. What would be the recommended best practise to include this information in the REST API call?

Additionally, what would be the current best-practise to extend the information returned by the above extended relations metadata, or from an @relations call? For example, if I have some custom fields on my related item and I want to avoid having to make additional REST API calls to get that information

Answering my own question!

If you're only interested in extending the results of a related item from a querystring search, the Plone docs for the @querystring-search endpoint discusses how to use the metadata_fields parameter to include additional information.

If you want the additional info on a normal content GET request, you need to do a little backend work. By implementing an adapter for the the DefaultJSONSummarySerializer provided by plone.restapi with a more specific content type, you can add any additional information you may need. Example code:

from plone.restapi.interfaces import ISerializeToJsonSummary
from plone.restapi.serializer.summary import DefaultJSONSummarySerializer
from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface

from my.backend.package.content_types import ICustomContentType

@implementer(ISerializeToJsonSummary)
@adapter(ICustomContentType, Interface)
class CustomContentTypeJSONSummarySerializer(DefaultJSONSummarySerializer):
    """ISerializeToJsonSummary adapter for the CustomContentType."""

    def __call__(self):
        summary = super().__call__()
        summary['my_additional_info'] = self.context.additional_info
        return summary
2 Likes