Y
Y
Yuri Gorbunov2019-10-22 11:02:48
RESTful API
Yuri Gorbunov, 2019-10-22 11:02:48

How to correctly request additional information on the received resources in the Rest API?

Greetings!
There was a question on logic of Restful API.
There is a standard scheme:
Tasks /api/tasks
Authors /api/users
Tasks are associated with authors by ID.
Displaying a list of tasks by making a request to /api/tasks
The task has an author ID.
How to display the last name and first name of the author when displaying a list of tasks in an application?
1. Make a grouped query to users, passing a list of author IDs from the resulting list of tasks, getting information on them?
2. Is it easy to add the necessary information on the author to /api/tasks on the backend?
How else is it better? How is it better?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
grinat, 2019-10-22
@irtek

Immediately put users in tasks as an object, and other garbage that is needed. Many frameworks support fetching out of the box via the https://jsonapi.org/format/#fetching-includes parameters , that is, in your case /api/tasks?include=user and if the model has a relation to task->user, then it will be added as object:
{id: 1, taskName: 'foo', user: {id: 1, firstName: 'bar'}}

I
Ivan Shumov, 2019-10-22
@inoise

Put GraphQL in front of your api and aggregate everything on the fly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question