最佳方式在REST API中获取关联数据是什么?

huangapple go评论66阅读模式
英文:

What is the best way to fetch relation data in REST API?

问题

I'd recommend using Method 2 for your REST API design when dealing with relationships like authors and posts. It's more efficient because it reduces the number of HTTP requests, especially when you want to display data that's related, like post titles and author names on the same page. This approach aligns with the principles of RESTful API design, which promotes resource inclusion.

By fetching posts with authors included, you minimize the need for additional requests, resulting in better performance. This method is commonly used in many applications, even in microservice architectures, to improve efficiency and reduce latency.

I hope this helps with your REST API design. If you have any more questions, feel free to ask.

英文:

I'm learning to design a REST API and am having trouble retrieving data in relations, which way should I use, and I'd like to know how big companies handle this kind of thing.

For example I'm designing a blog site.

Say I have the following 2 models:

model authors {
    id
    name
}


model posts {
    id
    title
    author_id
}

One author can have multiple posts, or one post can only have one author.

Frontend requirement: on the post/blog index page I want to display the post title and author name, more or less like this:

---------------------

[posts.title]

[authors.name]

---------------------

---------------------

[posts.title]

[authors.name]

---------------------

etc.

As I understand there are two methods:

Method 1:

GET /posts - fetch posts without author. let's say it returns 10 posts

I then iterate for each post to retrieve author data: GET /posts/{posts.id}/author

The result: I made 11 HTTP requests.

Method 2:

GET /posts - fetch posts back with author.

The result: I only made 1 HTTP request.


So far I have used method 2, regardless of whether the frontend requires an author or not, but I feel this is not the right method. Which method should I use? I feel method 1 makes more sense but I'm still unsure. Method 1 I think is more often used in applications with microservice architecture.

I really appreciate any answer, thanks in advance.

答案1

得分: 1

I'm learning to design a REST API and am having trouble retrieving data in relations, which way should I use, and I'd like to know how big companies handle this kind of thing.

在设计REST API时,与关联数据的检索有各种不同的选择。但最流行的两种方法是:

  1. 方法1:多次请求数据检索

使用这种方法,您将发送单独的请求来获取相关数据。例如,您可以发送一个请求以获取所有帖子,另一个请求以获取每个帖子的作者信息。这种方法通常被称为“懒加载”,因为只有在需要时才会加载相关数据。

  1. 方法2:单一请求中检索数据

通过使用这种方法,您可以通过单个请求获取所有所需的数据,包括相关数据。例如,您可以发送一个单一请求来检索所有帖子以及与它们相关的作者。由于加载了所有相关数据,这种策略通常被称为“急加载”。

采用哪种方法将取决于许多因素,包括数据的大小,关系的复杂性以及您的应用程序的性能需求。这两种方法都有利弊。


例如,我正在设计一个博客网站。

您可以根据您的具体情况使用其中一种技术,在您有一个包含两个模型(作者和帖子)的博客网站,并且您希望在帖子/博客索引页面上显示帖子标题和作者姓名。

对于使用方法1,每个帖子的数据需要两次获取,如果帖子很多,可能需要更长时间。但是,如果您希望减少页面的初始加载时间,这种方法可能会有所帮助。您可以在初始时仅检索帖子的数据,然后在用户与页面交互时根据需要检索作者的数据。

相比之下,方法2将涉及发送一个单一请求以一次检索所有数据。如果您只有很少的帖子,这可能会更快;但是,如果您有许多帖子和作者,可能需要更长时间。但是,如果您希望最小化请求并减少网络延迟,这种方法可能会有所帮助。

总的来说,根据您的独特需求,使用两种策略可能是最佳选择。例如,您可以使用方法2在初始时检索前几篇帖子的数据,然后在需要检索其余文章的数据时切换到方法1

英文:

> I'm learning to design a REST API and am having trouble retrieving
> data in relations, which way should I use, and I'd like to know how
> big companies handle this kind of thing.

There are various alternatives available when it comes to creating a REST API that works with relational data. But the two most popular approaches are:

  1. METHOD 1: Multiple requests for data retrieval

With this approach, you make separate requests to fetch related data. For instance, you may send one request to get all the posts and another to get each post's author information. This method is often referred to as "lazy loading," as the related data is only loaded when it is needed.

  1. METHOD 2: Fetching Data in a Single Request

By using this method, you can get all the data you require, including related data, with a single request. For instance, you could send a single request to retrieve all the posts and the writers associated with them. Due to the fact that all relevant data is loaded, this strategy is frequently referred to as "eager loading.

The choice of which approach to adopt will rely on a number of variables, including the size of the data, the intricacy of the relationships, and the performance needs of your application. Both methods offer benefits and drawbacks.


> For example I'm designing a blog site.
> ...

You can use one of the two techniques in your particular situation, where you have a blog site with two models, authors and posts, and you want to display the post title and author name on the post/blog index page.

The data for each post would need to be fetched twice using Method 1, which can take longer if there are many posts. However, this approach can be helpful if you want to reduce the time it takes for the page to load initially. You can do this by retrieving only the posts' data initially, and then the author's data as needed when a user interacts with the page.

In contrast, method 2 would involve sending a single request to retrieve all the data at once. If you only have a few posts, this may go more quickly; but, if you have many posts and authors, it may take longer. However, if you want to minimise requests and decrease network latency, this approach may be helpful.

That's it! Generally speaking, using both strategies would be the best course of action, depending on your unique needs. For instance, you could use method 2 to retrieve the data for the first few posts before switching to method 1 as necessary to retrieve the data for the remaining articles.

huangapple
  • 本文由 发表于 2023年5月7日 17:08:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定