angularngrxngrx-data

NxRx-Data custom DataService does not use DataSeviceConfiguration


I extended the DefaultDataService like this:

    @Injectable()
    export class DidDocumentDataService extends DefaultDataService<DidDocument> {
      constructor(http: HttpClient, httpUrlGenerator: HttpUrlGenerator) {
        super("DidDocument", http, httpUrlGenerator, defaultDataServiceConfig);
      }

      getById(): Observable<DidDocument> {
        return this.http.get("/identifiers").pipe(
          map(res => res["didDocument"])
        );
      }
    }

The app-store.module.ts is setup like this:

// this used to work before overriting the DefaultDataService
export const defaultDataServiceConfig: DefaultDataServiceConfig = {
  root: `${HOST}/1.0/`
};

@NgModule({
  imports: [
    EntityDataModule.forRoot(entityConfig),
    ... // other store related imports
  ],
  providers: [
    DidDocumentDataService,
    { provide: DefaultDataServiceConfig, useValue: defaultDataServiceConfig }
  ],
  declarations: [StoreComponent],
  exports: [StoreComponent]
})
export class AppStoreModule {
  constructor(
    private entityDataService: EntityDataService,
    private didDocService: DidDocumentDataService
  ) {
    this.entityDataService.registerService("DidDocument", this.didDocService);
  }
}

Before I extended the DefaultDataService, the defaultDataServiceConfig was used and the requests were triggered against the custom root. Now every request hit localhost (seems to just use the current host).

The AppStoreModule is not a lazy-loaded module and is loaded in the AppModule. The component which is triggering the request is the StoreComponent. My first guess is that something is not happening in the order I expect it.

When is the CustomDataService with the CustomConfiguration created and does overwriting the Service only work in lazy-loaded modules?

Solution

I set the absolute URL in the httpClient directly and also pass the identiefer to the getById method:

getById(id: string): Observable<DidDocument> {
  return this.http.get(`${HOST}/1.0/identifiers/${id}`).pipe(
    map(res => res["didDocument"])
  );
}

Solution

  • The root cause of your issue is probably in this code :

    getById(): Observable<DidDocument> {
      return this.http.get("/identifiers").pipe(
        map(res => res["didDocument"])
      );
    }
    

    In this case, even if the DefaultDataServiceConfig is correctly set with the correct url, this code is overriding the GET url.

    http.get("/identifiers")
    

    is equivalent to

    http.get("http://localhost/identifiers")
    

    (if your dev app is serving on localhost)

    You should either set absolute url or use ancestor methods of DefaultDataService with super :

    getById(): Observable<DidDocument> {
      return super.getById(id).pipe(
        map(res => res["didDocument"])
      );
    }
    

    You can find more details inside official doc, with this code snippet example :

    @Injectable()
    export class HeroDataService extends DefaultDataService<Hero> {
      constructor(http: HttpClient, httpUrlGenerator: HttpUrlGenerator, logger: Logger) {
        super('Hero', http, httpUrlGenerator);
        logger.log('Created custom Hero EntityDataService');
      }
    
      getById(id: string | number): Observable<Hero> {
        return super.getById(id).pipe(map(hero => this.mapHero(hero)));
      }
    }