Search code examples
angularngrxangular-ngrx-data

NGRX data entityResourceUrl depending on other entity. dynamic urls?


Usually when I want to specify url of the entity I have one specific url for it like serverURL/heroes. But now I have a case where I want to use entity belonging to other entity. For example lets say I want to use weapons of heroes. My http urls for swords look something like: serverURL/heroes/heroID/weapons. So lets say I need to add new sword, my only option is to add it to a specific hero, so I need to know heroesID and sword information to do something like: HTTP POST http.post('serverURL/heroes/heroID/weapons', swordObject);

Currently when I just add heroes url in entityHttpResourceUrls in DefaultDataServiceConfig. You can see the example on stackblitz. go to app->store->entity-> entity-store.module.ts. that's where I usually specify the url.

How do I approach my case? how can I have dynamic urls for child entities?

Update

from what I've found in this issue, ngrx-data doesn't support parameters yet.

https://github.com/ngrx/platform/issues/1934


Solution

  • I ended up using getWithQuery function:

    getWithQuery(params: QueryParams): Observable<Weapon[]> {
    
        const hero= params['heroId'] || '';
        const pageIndex = params['pageIndex'] || '0';
        const pageSize = params['pageSize'] || '25';
        const apiUrl = `api/hero/${heroId}/weapons/?page=${pageIndex}&size=${pageSize}`
    
        return this.http.get(apiUrl)
    }
    

    instead of using getAll.