3
votes

I'm new to NgRx. After preliminary research NgRx and particularly the NgRx Data package appear to be a good fit for an application I am working on. One caveat is our entities are backed by micro-services with unconventional routes.

Each entity has its own micro-service, and any operation is further isolated with a base record ID within the URL params.

[microServiceRoute]/[baseRecordId]/entityEndpoint

I have a lookup table for the routes, but the baseRecordId would be held in the store. The NgRx Data docs has a simple example outlining the replacement of the URLGenerator.

How can I specify my microService routes on a per Entity basis, while also having this generator read from the store? Can this be done easily or should I approach this from a custom service per entity perspective?

https://ngrx.io/guide/data/extension-points#replace-the-httpurlgenerator

1

1 Answers

2
votes

You are totally right, you need to implement your own HttpUrlGenerator class that serves desired endpoints and is provided in the module that imports EntityDataModule.forRoot like

providers: [
  {
    provide: HttpUrlGenerator,
    useClass: MyApiHttpUrlGenerator,
  },
],

Currently there's a configuration key that changes prefix of API, but it doesn't allow to specify endpoints per entity: https://ngrx.io/guide/data/entity-dataservice#provide-a-custom-configuration

const defaultDataServiceConfig: DefaultDataServiceConfig = {
  root: 'https://my-api-domain.com:8000/api/v1',
}

@NgModule({
  providers: [{
    provide: DefaultDataServiceConfig,
    useValue: defaultDataServiceConfig,
  }]
})