[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
Skip to content

[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

Closed as not planned

Description

@Rebolon

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:

  • 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.

Activity

  1. Rebolon commented on Feb 28, 2018

    @Rebolon
    Author

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

    • HTTP POST on the book endoint (book is a Doctrine Entity and also declared as ApiResource)
    • Raw content of the post:
    {
        "book": {
            "title": "test depuis special4",
            "editors": [{
                "publication_date": "1519664915", 
                "collection": "Hachette collection bis", 
                "isbn": "2-87764-257-7", 
                "editor": {
                    "name": "JeanPaul Edition"
                }
            }, {
                "publication_date": "1519747464", 
                "collection": "Ma Tu vue", 
                "isbn": "2-87764-257-7", 
                "editor": {
                    "name": "JeanPaul Edition"
                }
            }],
            "authors": [{
                "role": {
                    "translation_key": "WRITER"
                }, 
                "author": {
                    "firstname": "Marc", 
                    "lastname": "Douche"
                }
            }, {
                "role": {
                    "translation_key": "DRAWER"
                }, 
                "author": {
                    "firstname": "Paul", 
                    "lastname": "TRUC"
                }
            }],
            "serie": {
                "name": "ouaou ma serie"
            }
        }
    }
    

    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 ?

    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

  2. soyuka commented on May 19, 2026

    @soyuka
    Member

    Closing per #2987. Questions are best asked on GitHub Discussions or Stack Overflow (api-platform tag). The framework now lives at api-platform/core.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions