vendredi 22 novembre 2019

How to design API for getting collections objects?

I have a lot of different lists. Objects of lists have fields that are objects too.

When I do request to the server, I get a response that sees like(people):

[
   {
      id: 1,
      fistName: "Steve",
      lastName: "Jobs",
      companyId: 1
   },
   {
      id: 2,
      fistName: "Warren",
      lastName: "Buffett",
      companyId: 2
   },
   {
      id: 3,
      fistName: "Bill",
      lastName: "Gates",
      companyId: 3
   }
]

Here companyId is ID of another object. The company, for example:

[
   {
      id: 1,
      title: "Apple",
      cityId: 1
   },
   {
      id: 2,
      title: "Berkshire Hathaway",
      cityId: 2
   },
   {
      id: 3,
      title: "Microsoft",
      cityId: 3
   },
]

Here cityId is ID of objects cities etc.

When I get a list of people, I need to know the titles of companies and cities.

How can I design my API for it?

  1. One request that will send all the information. For example:
{
   data: [], //Array of people
   payloads: {
      companies: [], //Array of companies
      cities: []     //Array of cities
   }
}
  1. Several requests each of which will send its information. Then I have to make several requests from client to server, each time requesting additional information.

Aucun commentaire:

Enregistrer un commentaire