REST: returns complex nested data and multiple calls

I have a REST api served by NodeJS on the fronts of AngularJS.

I work with users:

GET  /api/users  #Returns all users
POST /api/users  #Create  new user

GET   /api/users/:id   #Return a user
PUT   /api/users/:id   #Edit a user
DELTE /api/users/:id   #Delete a user

This is the user:

{
  login : "admin"
  email : "admin@admin.com"
  pass  : "hashedpassword"
  ...
}

My user can belong groups

GET   /api/users/:id/groups   #Return the groups of a user

They may also have constraintsor may inherit constraintsform their groups

GET   /api/users/:id/constraints   #Return the constraints of a user
GET   /api/groups/:id/constraints   #Return the constraints of a group

Problem:

I create an admin page, displaying all users, their groups, their restrictions.

Should I:

  • Make a lot of requests in a for loop on the front of javascript (Angular)?

Sort of:

$http.get(/api/users).then(function(result) {
  result.data.forEach(function(user) {
    $http.get('/api/users/'+user.data.id+'/groups).then(function(groups) {
      groups.forEach(function(group) {
        $http.get('/api/groups/'+group.data.id+'/constraints)
      })
    })
  })
})
  1. Create path /api/users/withConstraintsAndGroups

This will return a large list of all users with their groups and their restrictions.


I find solution 1 very enjoyable, informative, lightweight and versatile, but I'm afraid of very poor performance

2 , , , ,

?

+4
5

, , , , . 2016 , -, , .

99% 1% - , Opera ( -)?

, REST api, , , API. , , - , , .

Also, an API is an API and should not be modified when a front end application is being built on that API. , 10 , API, , , . API ? .

REST api (, HTTP-):

:

 * when you have a relation between entities

     /users/3/accounts       // should return a list of accounts from user 3
     /users/3/accounts/2     // should return account 2 from user 3

 * custom actions on your logical resources

    /users/3/approve
    /accounts/2/disable

API partial requests (, querystrings : users/3?fields=Name,FirstName), versioning, documentation (apiDocs.js ), (json), pagination, token auth compression ( :))

+3

, ( ), :

/api/users?includes=groups&includes=constraints

, , .

, , JSON: API.

JSON:API , /, .

+2

:

, HTTP- ?

( ping) . , .

, HTTP- .

, 1, , , . , . , 2.

, , , , . , , , , , API.

+1

...

SO, , , REST .

REST , "accessors/getters".

/api/users/:id/groups, , / users id , , groups / ( - ). ( REST , URL-) "" . URL- (, /api/groups), .

, ( ), , . , API, , , :

/users

/users/:id

/users/:id/groups ,

/users/groups ,

/users/groups/:id /users/groups ()

....

URL-. , REST ( ) .

==========

, , , , : , .

(, /crap UI), , (, ).

(, ), - (, ... , ).

/api/users/:id/constraints ?

BTW, withConstraintsAndGroups REST. REST - (constraints groups, ). .

+1
source

It doesn't seem to me that the admin page performance is too big a problem. The only difference between the two is that in # 1 you have 3 api calls, and # 2 only one. The data should be the same, the data should be reasonable, not extremely large. Therefore, if # 1 is easier to code and maintain, you should go with that. You should not have performance issues.

0
source

Source: https://habr.com/ru/post/1623267/


All Articles