OData REST API and recursive queries

    In a discussion to a previous article, dezconnect raised an interesting question about OData's ability to perform recursive queries (similar to SQL CTE).

    The OData documentation in section 11.2.4.2 describes the $ expand query option . This option allows you to retrieve objects along with related objects. For example, you can get data about a company along with all the data about its president:
    https://samples.databoom.space/api1/sampledb/collections/companies(company1)?$expand=president

    Without the $ expand option, the result will include only id company president (or link with other settings).
    https://samples.databoom.space/api1/sampledb/collections/companies(company1)

    In the 4th version of OData, the $ expand option for executing recursive queries has been significantly expanded.

    For example, a person has friends, they, in turn, also have friends, and we want to get the whole tree of friends. To make it easier to write such recursive queries, OData introduces an additional $ level option that determines the depth of the recursion:

    http: // yourhost / service / yourdatabase / person (1)? $ Expand = friends ($ level = 2)

    This query will build a tree (by communication friends) to depth = 2.

    It is possible to set the search without limiting the depth of the recursion by specifying $ levels = max.
    But in this case, the problem of cyclic bonds arises. The standard does not stipulate what should happen in the presence of cyclic connections - therefore, in this case, the depth of the recursion should be limited to a specific value.

    Also popular now: