api-platform / api-platform

🕸️ Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.
https://api-platform.com
MIT License
8.43k stars 953 forks source link

[question] What is the good practice to create custom route (POST/PUT/GET) with nested entities, keeping in mind web-performance (less http calls is better) ? #581

Open Rebolon opened 6 years ago

Rebolon commented 6 years ago

Hi,

In my application, i take care of web-performance and when it's possible i try to do the less HTTP calls. So when i create a POST/PUT route i want to send all entities at the same time. For instance the process i can see in different sample is that i must create first sub-entities and then the main one.

Here is the model: Lite library sample

So to create a new Book my UI has one form that allows to:

To keep best performance, all creation operation (editors, authors, serie) are done locally, in the browser. When the user validate the form, then i want to do only one HTTP call. And that's where i wonder what are the best pratices:

I wonder how to do this on REST and GraphQL (because on GraphQL you recently decided to use IRIS for nested object in mutations, which require to create all sub-objects first).

This question about good practices is also available for GET operations because sometime (not evry time) i want to get all informations about a book (except reviews) with only one HTTP call. If i use API-Platform standard i have to do nth call for each IRIS found in sub-entities. I tried custom operations but i'm not really satified by my implementation. Shoul i use the normalization_context to embed full data from nested entities ?

Thanks for help, becasue i saw few questions about those good practices on StackOverflow, and there is no answaer for instance.

Rebolon commented 6 years ago

I want to bring more information about the POST Operation: here is the kind of request i want to do

For instance i didn't found the way of doing that kind of thing throuht ApiPlatform so i just added a new itemsOperations on Book Entity and a new Action BookSpecial with the related method. This method has a Route annotation to map the itemsOperations of the Eneity. It also has a ParamConverter that will transform the json content into a full Book Entity. Then the action only has to use the new Book built by the ParamConverter to persist into the DB and return a JSONResponse with the serialized book. In this operation: i don't follow ApiPlatform in fact. I don't manage all format like ApiPlatform does. The Swagger API is not up to date. It references the new route but the description is wrong. And for instance i don't manage nested entity that contains IRIS (so i don't need to create them but just link them with related entity) and i don't validate the json against a Validator or JsonSchema, and i don't manage Exception from ParamConverter (how to manage them as an HTTPException to auto send the HTTP response based on the format).

So The original question would be: does ApiPlatform can help me on that kind of request ? and how ?

edit: all code that represent my above explanation is available here https://github.com/Rebolon/php-sf-flex-webpack-encore-vuejs/tree/master/ in src/Action folder and the param converter has been extracted into its own repository: https://github.com/Rebolon/ApiJsonParamConverterComponent/tree/master/src