D
D
dmitriy2021-07-04 12:29:17
PHP
dmitriy, 2021-07-04 12:29:17

REST API. How to display nested entities?

Hello, I have a couple of questions about the REST API. For example, there are entities Order, order status, order positions, order client The order
is an aggregate, so we build an api from it,
GET / orders
POST / orders
GET / orders / {id}
PUT / orders / {id}
DELETE GET / orders / {id}

1. Do I need to display the full response with the related state entities or is it enough to display the stateId, I assume that it is worth displaying the id of the related entities if they intersect with other aggregates, for example customerId, and get the entities through ?include=customer

{
"data":{
    "id"=>1,
    "customerId":1,
    "state":{
        "id" => 1,
         "name" => "В работе"
     }
}


2. Should the answer group the fields according to their purpose,
for example
{
"data":{
    "id"=>1,
    "dates" : {
        "dateCreate" : {
            "date": "2021-07-04 09:05:37.000000",
            "timezone_type": 1,
            "timezone": "+00:00"
        },
       "dateUpdate" : {
            "date": "2021-07-04 09:05:37.000000",
            "timezone_type": 1,
            "timezone": "+00:00"
        }
    }
}


3. If the response is grouping fields by topic, is it worth using a similar approach in a create and update request?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2021-07-04
@nokimaro

Output exactly as much information as needed by the client who works with this api and makes the data display.
For example, you need to display a list of orders and the name of the client, then it is better to immediately return customer.name, and not pull each profile by customer.id.
The rest is a matter of taste, if you want to group dates, if you don't want to, don't group them.
There is also GraphQL if you need more flexibility.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question