apirestbackend

Structuring linked backend queries and storage


This is a basic question I'm hoping to get some insight on. I'm building the api controller on my front end have yet to build a backend before (ever).

I have this simplified User object.

User = {
  id: number,
  name: string,
  followers: User[]
}

This isn't feasible to store though since it's recursive. Instead, this makes more sense in the backend:

User = {
  id: number,
  name: string,
  followerIDs: number[]
}

In my front end though, if I wanted to list the names of a user's followers, I'd need two requests, one to get the ID's and one to map them to Users. It makes more sense to me to optimize this in the backend and return all the data in one request.

This would mean, though, that my end point would return two different shapes depending on the query. The shape with followerIDs above and:

User = {
  id: number,
  name: string,
  followers: (User: {id, name, followerIDs})[]
}

Is this done in practice? Or is it more feasible to just have multiple requests? If I want to list x users and some followers from each one, that would need 1 + x requests to get all the info, vs 1 request in the third shape.

I'm a beginner when it comes to API design, so feel free to drop some quick knowledge I can use to move forward. Recommendations on fruitful resources to learn would also eb great (books welcomed as well).


Solution

  • It can, and is, done both ways.

    However, if you want an example of a fantastic API that is very well documented which you could use as an example as you design your API, I suggest looking at stripe's API.

    Here is their documentation regarding your use case, which they call 'expansion'. They make it optional. By default, you get the ids, or you can request the results to be expanded inline, in which case they get the whole object.

    https://stripe.com/docs/api/expanding_objects