This question is language independent. Let's not worry about frameworks or implementation, let's just say everything can be implemented and let's look at REST API in an abstract way. In other words: I'm building a framework right now and I didn't see any solution to this problem anywhere.
Question
How one can construct REST URL endpoint for intersection of two independent REST paths which return collections? Short example: How to intersect /users/1/comments
and /companies/6/comments
?
Constraint
All endpoints should return single data model entity or collection of entities.
Imho this is a very reasonable constraint and all examples of Hypermedia APIs look like this, even in draft-kelly-json-hal-07.
If you think this is an invalid constraint or you know a better way please let me know.
Example
So let's say we have an application which has three data types: products
, categories
and companies
. Each company can add some products to their profile page. While adding the product they must attach a category to the product. For example we can access this kind of data like this:
GET /categories
will return collection of all categoriesGET /categories/9
will return category of id 9GET /categories/9/products
will return all products inside category of id 9GET /companies/7/products
will return all products added to profile page of company of id 7
I've omitted _links
hypermedia part on purpose because it is straightforward, for example /
gives _links
to /categories
and /companies
etc. We just need to remember that by using hypermedia we are traversing relations graph.
How to write URL that will return: all products that are from company(7) and are of category(9)? In otherwords how to intersect /categories/9/products
and /companies/7/products
?
Assuming that all endpoints should represent data model resource or collection of them I believe this is a fundamental problem of REST Hypermedia API, because in traversing hypermedia api we are traversing relational graph going down one path so it is impossible to describe such intersection because it is a cross-section of two independent graph paths.
In other words I think we cannot represent two independent paths with only one path. Normally we traverse one path like A->B->C
, but if we have X->Y
and Z->Y
and we want all Y
s that come from X
and Z
then we have a problem.
So far my proposition is to use query strings: /categories/9/products?intersect=/companies/9
but can we do better?
Why do I want this?
Because I'm building a framework which will auto-generate REST Hypermedia API based on SQL database relations. You could think of it as a trans compiler of URLs to SELECT ... JOIN ... WHERE
queries, but the client of the API only sees Hypermedia and the client would like to have a nice way of doing intersections, like in the example.