Search code examples
restrestful-architecture

Does a collection have less details than a single resource?


I am wondering if this is correct or should be avoided?

GET /products/

  {
    "id": 1,
    "name": "phone",
    "price" : "10.00 GBP"
  },
  {
    "id": 2,
    "name": "car",
    "price" : "1000.00 GBP"
  }

GET /products/1

  {
    "id": 1,
    "name": "phone",
    "colour" : "blue",
    "memory: "2GB",
    "storage": "32GB",
    "connectivity" : "3G/4G",
    "price" : "10.00 GBP"
  }

The collections products does not return all the details about a single product but when retrieving the single product you can see all the details.

Is this wrong?


Solution

  • It's not wrong. In fact, you can use collections to give an "overview" of your entities, while the actual details can be found within the entities themselves. Normally, you also provide a link to the given entity in that case.

    However, there is a downside: what constitutes an "overview". For one view, it might be enough just to display the name and the price, but another overview might other details like color etc. IN the first case, fetching the collection would be enough, in the latter, you must download each entity separately to fetch the color. Normally, the collection is "enriched" with the color so you don't have to anymore, but very (very!) soon, it would mean you are just returning all data from the entity in the collection anyway.

    It's possible to overcome this by using the query string for filtering on WHAT you want to return (for instance: GET /articles?page=5&limit=10&fields=memory,name,colour,storage, which would make it more dynamically and allows you to serve any detailed view out there, including mobile devices with limited bandwidth etc.