[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) ? · Issue #581 · api-platform/api-platform · GitHub
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[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
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:
So to create a new Book my UI has one form that allows to:
add Book informations
select Editors or ceate new one (no limit)
add ProjectBookEdition information related to the editors
select Authors or create new one (max 3 authors)
select Serie or create a new one
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:
use custom operations ? but i don't find how to manage this.
use DTO ? same thing, should i do a custom entity not related to doctrine and then manage everything manually ?
use a specific controller outside the Api-platform (and this time i know exactly how to do this) ?
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.
So for this request i want to create all related entities: Author, Authors (which are ProjectBookCreation Entity), Editor, Editors (which are ProjectBookEdition Entity, Serie, Role (which is Job Entity), and Book.
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 ?
Activity
Rebolon commentedon Feb 28, 2018
I want to bring more information about the POST Operation:
here is the kind of request i want to do
So for this request i want to create all related entities: Author, Authors (which are ProjectBookCreation Entity), Editor, Editors (which are ProjectBookEdition Entity, Serie, Role (which is Job Entity), and Book.
For instance i didn't found the way of doing that kind of thing throuht ApiPlatform so i just added a new
itemsOperationson 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
soyuka commentedon May 19, 2026
Closing per #2987. Questions are best asked on GitHub Discussions or Stack Overflow (api-platform tag). The framework now lives at
api-platform/core.