HasMany RESTfull Implementation

So I’ve been reading a lot on RESTfull design – specifically dealing with resources.

Taking the canonical example of Users, Posts, and Comments, with relationships as:

Users —(hasMany)—> Post —(hasMany)—> Comment

One may initially think to expose something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>GET /users GET /posts GET /comments
POST /users POST /posts POST /comments
GET /users/id GET /posts/id GET /comments/id
PUT /users/id PUT /posts/id PUT /comments/id
DELETE /users/id DELETE /posts/id DELETE /comments/id
</code>
<code>GET /users GET /posts GET /comments POST /users POST /posts POST /comments GET /users/id GET /posts/id GET /comments/id PUT /users/id PUT /posts/id PUT /comments/id DELETE /users/id DELETE /posts/id DELETE /comments/id </code>
GET /users               GET /posts               GET /comments 
POST /users              POST /posts              POST /comments 
GET /users/id            GET /posts/id            GET /comments/id
PUT /users/id            PUT /posts/id            PUT /comments/id
DELETE /users/id         DELETE /posts/id         DELETE /comments/id

But then, say I want all Comments of a certain Post made by a particular User. I’d need to do something like:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>GET /users/id
> someUser
> var postIds = someUser.posts()
GET /posts?id=<postIds[0]>&id=<postIds[1]>&...
> somePosts
> **application user inspects posts to see which one they care about**
> var postOfInterest = somePosts[x];
> var postId = postOfInterest.id;
GET /comments?id=postId
> someComments (finally)
</code>
<code>GET /users/id > someUser > var postIds = someUser.posts() GET /posts?id=<postIds[0]>&id=<postIds[1]>&... > somePosts > **application user inspects posts to see which one they care about** > var postOfInterest = somePosts[x]; > var postId = postOfInterest.id; GET /comments?id=postId > someComments (finally) </code>
GET /users/id
   > someUser
   > var postIds = someUser.posts()
GET /posts?id=<postIds[0]>&id=<postIds[1]>&...
   > somePosts
   >  **application user inspects posts to see which one they care about**
   > var postOfInterest = somePosts[x]; 
   > var postId = postOfInterest.id;
GET /comments?id=postId
   > someComments (finally)

Suppose though I only care about a Post or Comment in the context of it’s owner. Suppose a different resource structuring which may (or may not?) be more natural:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>GET /users
POST /users
GET /users/id
PUT /users/id
DELETE /users/id
GET /users/id/posts
POST /users/id/posts
GET /users/id/posts/id
PUT /users/id/posts/id
DELETE /users/id/posts/id
GET /users/id/posts/id/comments
POST /users/id/posts/id/comments
GET /users/id/posts/id/comments/id
GET /users/id/posts/id/comments/id
GET /users/id/posts/id/comments/id
</code>
<code>GET /users POST /users GET /users/id PUT /users/id DELETE /users/id GET /users/id/posts POST /users/id/posts GET /users/id/posts/id PUT /users/id/posts/id DELETE /users/id/posts/id GET /users/id/posts/id/comments POST /users/id/posts/id/comments GET /users/id/posts/id/comments/id GET /users/id/posts/id/comments/id GET /users/id/posts/id/comments/id </code>
GET /users
POST /users  
GET /users/id   
PUT /users/id
DELETE /users/id

GET /users/id/posts
POST /users/id/posts
GET /users/id/posts/id
PUT /users/id/posts/id
DELETE /users/id/posts/id

GET /users/id/posts/id/comments
POST /users/id/posts/id/comments
GET /users/id/posts/id/comments/id
GET /users/id/posts/id/comments/id
GET /users/id/posts/id/comments/id

Which to me, is probably a better representation of what the resources are. Then all I need is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>GET /users/id/posts
> somePosts
> **application user inspects posts to see which one they care about**
> var postOfInterest = somePosts[x];
> var postId = postOfInterest.id;
GET /users/id/posts/postId/comments
> someComments
</code>
<code>GET /users/id/posts > somePosts > **application user inspects posts to see which one they care about** > var postOfInterest = somePosts[x]; > var postId = postOfInterest.id; GET /users/id/posts/postId/comments > someComments </code>
GET /users/id/posts
   > somePosts
   > **application user inspects posts to see which one they care about**
   > var postOfInterest = somePosts[x];
   > var postId = postOfInterest.id;
GET /users/id/posts/postId/comments
   > someComments

This just seems more like navigating a file system than the previous method – but I don’t know if its RESTfull at all (perhaps this is what REST was trying to get rid of) because in order to access a Comments resource, I need to know which User and which Post it belongs to. But the former requires 3 requests, while the latter requires just 2.

Thoughts?

1

As far as I know what you’re describing, essentially nested resources, is perfectly RESTful. You might want to keep the resources such as ‘/posts/id’ and ‘/comments/id’ in-case you want to get post/s or comment/s without knowing which user you’re looking at, but there’s nothing wrong with also nesting them inside each other for getting the posts for a particular user or comments for a particular post.

Rails in particular allows you to easily nest resources in this manner by doing something like the following in your routes configuration:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>resources :users do
resources :posts do
resources :comments
end
end
</code>
<code>resources :users do resources :posts do resources :comments end end </code>
resources :users do
  resources :posts do
    resources :comments
  end
end

1

When exposing a RESTful interface, you also need to examine, what the interface is telling the user.

When examining a post, its more than likely a user doesn’t care about who made it and unless post IDs are only unique in the scope of a user (which is unlikely) its just addtional cruft in the URL. While its true that a post has a user, I’m not so sure you can say that in this context it is owned by its user, if the difference is clear.

There is also the abiquity of what happens when user X writes a post, and user Y writes a comment on that post. In this /users/idZ/post/id/comment/id does idZ refer to the user who wrote the post, or the comment? Moreso, why do you need to know the user, if you already have a post or comment ID?

Alternatively, a comment is owned by its post, even if its ID is globally unique.

Lastly, there is a readability difference between users and user, with the latter implying discreteness. Similarly, with posts and post, and comments and comment.

So personally, given this, I’d use these actions (with the appropriate verbs where required):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>ACTION /users
ACTION /user/id
ACTION /user/id/posts # A user owns these posts
ACTION /post/id # But the site owns this particular post
ACTION /post/id/comments
ACTION /post/id/comments/id
</code>
<code>ACTION /users ACTION /user/id ACTION /user/id/posts # A user owns these posts ACTION /post/id # But the site owns this particular post ACTION /post/id/comments ACTION /post/id/comments/id </code>
ACTION /users
ACTION /user/id   

ACTION /user/id/posts    # A user owns these posts
ACTION /post/id          # But the site owns this particular post

ACTION /post/id/comments
ACTION /post/id/comments/id

1

Colin, I guess the point you’re discussing is about nested REST and that is completely possible with the interface.

Users —(hasMany)—> Post —(hasMany)—> Comment

This relationship do exist, but how you defined them in next two cleared the purpose of nested representations. I designed this code under REST nested designs in this page as a demo reference, you can check them too.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật