GreedyAi
GreedyAi

Reputation: 2839

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

Upvotes: 0

Views: 1691

Answers (3)

GreedyAi
GreedyAi

Reputation: 2839

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.

Upvotes: 0

Vladymyr K
Vladymyr K

Reputation: 31

In my case I created service:

@Injectable()
export class OrganizationDataService extends DefaultDataService<Organization> {
  constructor(http: HttpClient, httpUrlGenerator: DefaultHttpUrlGenerator, logger: Logger) {
    super('Organization', http, httpUrlGenerator);
  }

  getAll(): Observable<Organization[]> {
    console.log('getAll called');
    // custom call to api correct variant:
    return this.execute('GET', `${environment.apiUrl}org/${someId}/apps`)
    // custom call to api also worked variant:
    // return this.http.get<Organization[]>(`${environment.apiUrl}org/${someId}/apps`);
  }
}

Upvotes: 3

Wandrille
Wandrille

Reputation: 6801

You need to use the router store from NGRX.

With the store, you have this selectors:

selectQueryParams, selectRouteParams, selectRouteData and the selectUrl.

So you can easily get all your needs to build the different paths.

Upvotes: 0

Related Questions