I
I
igor11122019-03-25 19:05:09
RESTful API
igor1112, 2019-03-25 19:05:09

Relationships in RESTful API?

Hello. I'm making (trying) a SPA running on a REST API. I have a Post entity that is related to a Comment entity (one-to-many), and a Comment entity is related to a Comment_Likes entity (one-to-many):
Post
id
title
content
Comment
id
content
post_id
Comment_Likes
id
user_id
comment_id
-request at the url site.com/posts, can I return such a response, which will contain all the relationships at once? -

[{
  "id": "1",
  "title": "First post",
  "content": "Content of first post",
  "comments": [{
    "id": "1",
    "content": "First comment",
    "likes": [{
      "user_id": 1,
      "comment_id": 1
    }]
  }]
}]

Is it REST or not REST? And if not REST, then how to make it more like it? I've read that related models should be returned as links to this resource. But what does it mean that instead of making one http request, I have to make hundreds of requests so that my service looks more like Restful?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Nesmeyanov, 2019-03-25
@SerafimArts

Well, a huge bunch of articles on the Internet on this topic, for example: https://habr.com/ru/company/hexlet/blog/274675/
And the question that you ask refers to API design, and here you already have to break a huge one about this stone bunch of copies. Do what makes it easier to use. On the other hand, OData, Protobuf\GData, RAML, GraphQL and other add-ons over REST solve one of these problems.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question