reyesoft / ngx-jsonapi

JSON API client library for Angular 5+ 👌 :: Production Ready 🚀
https://ngx-jsonapi.reyesoft.com/
MIT License
101 stars 52 forks source link

Autoregister causes AOT issues #193

Closed GIANTCRAB closed 4 years ago

GIANTCRAB commented 5 years ago

I'm following the guide on README page and I'm experiencing issues with Autoregister whilst on AOT.

Error message

ERROR Error: Uncaught (in promise): NullInjectorError: StaticInjectorError(AppModule)[CampaignsComponent -> newCtor]: 
  StaticInjectorError(Platform: core)[CampaignsComponent -> newCtor]: 
    NullInjectorError: No provider for newCtor!

Code

import {Autoregister, Service} from 'ngx-jsonapi';
import {Campaign} from './campaign.resource';
import {Injectable} from '@angular/core';

@Injectable({
  providedIn: 'root',
})
@Autoregister()
export class CampaignsService extends Service<Campaign> {
  public resource = Campaign;
  public type = 'campaigns';
}

Am I doing something wrong? It works on non-AOT mode.

Tonio-lavanda commented 5 years ago

Did you find a solution for this ?

GIANTCRAB commented 5 years ago

@Tonio-lavanda no, I just switched to another library.

yringler commented 5 years ago

@GIANTCRAB what did you switch to?

GIANTCRAB commented 5 years ago

@yringler I'm currently using https://github.com/ghidoz/angular2-jsonapi . It doesn't have all the features that this has but it works.

Tonio-lavanda commented 5 years ago

I experimented with https://github.com/ghidoz/angular2-jsonapi but the fact that I can't lazy-load relationship is a huge blocker for me: https://github.com/ghidoz/angular2-jsonapi/issues/103

That's why I came here...

GIANTCRAB commented 5 years ago

I would rather have AOT working for me than a nice-to-have cache feature that'll definitely improve performance by a lot.

It boils down to what you need.

You could also make a fork that fix this issue but I think that's very time consuming.

iamchrismiller commented 4 years ago

@pablorsk Any insight into this issue? I currently shim it by providing all the "Auto-registered" services in app.module and it compiles as expected but this seems to be an anti-pattern.

maxi7587 commented 4 years ago

Hi @iamchrismiller !

The issues with @Autoregister have been solved. Since this PR there is no need to use @Autoregister and it will be deprecated in future releases. Please continue reading to understand the issue.

The library needs to register the services in order to create the corresponding instances for the resources received from API. We couldn't find a better approach than providing the resource's Services with the corresponding relatioship's Services.

At the moment, Services are auto registering when instantiated (actually, not when instantiated, but immediately after the call Stack gets empty after instantiation... you can see the code in Service.ts constructor). If you need the service to be registered in instantiation (before the call stack gets empty), you can still register services manually using this.register(), but this is rarely needed.

I know that it's not very tranparent, I encourage you to create a Fork and help us improve the library. PR's are welcome!