apollo-angular: Apollo has a constructor which is not compatible with Dependency Injection. It should probably not be @Injectable().

I was following documentation from the https://www.apollographql.com/docs/angular/basics/setup/ trying to install Apollo Angular.

After running ng add apollo-angular and updating API endpoint in the graphql.module.ts, I’m getting Apollo has a constructor which is not compatible with Dependency Injection. It should probably not be @Injectable(). when trying to inject Apollo from apollo-angular in any component or service.

Angular version: 8.2.11 Angular Apollo version: 1.8.0

How this could be resolved?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 11
  • Comments: 19 (2 by maintainers)

Most upvoted comments

There’s no stable Angular 9 yet so you guys please give me time to solve it. I started creating apollo-angular when Angular was in alpha, then in beta and then they changed bunch of stuff in one of RCs.

As @Vilsol pointed out. It’s no longer an issue in 9.0.0-rc.2.

It’s an open-source project and I’m the only one developing it (Apollo Team haven’t pushed a single line of code). If you want to help, please provide reproductions or even pull requests, this speeds up things a lot.

@kamilkisiela I want to personally thank you for all your work. This project is awesome. I really do appreciate you taking the time to look into this. Thank you.

I am only having this issue when running unit tests. is there any solution for this already?

It’s taking time. We need to fix that

Anyone working on this? Update: Going to post my findings as tracking down bug: File in question: src/Apollo.ts

  1. Currently dissecting the constructor: (Line 112-120)
constructor(
    private _ngZone: NgZone,
    @Optional()
    @Inject(APOLLO_OPTIONS)
    apolloOptions?: ApolloClientOptions<any>,
    @Optional()
    @Inject(APOLLO_NAMED_OPTIONS)
    apolloNamedOptions?: NamedOptions,
  ) {

Looking to see what changed with constructor to make it so that it thinks this shouldn’t be an injectable

  1. Also looking through recent history on @angular/core/core.d.ts file to see if anything recently changed in declaration file. Will tell if has to do with recent change here, or in @angular/core

At this point stuck: Trying to figure out why __ivy_ngcc__ is automatically being added to package, and new builds that I am making aren’t being picked up? At this time rolling back packages, so can continue working on app. I do not know core contributors personally, but would love if can reach out to charlie@razroo.com, so that we can talk. Thank you.

This issue only shows up for me when "angularCompilerOptions": { "enableIvy": true }, Hope it can be resolved soon!

@Maximaximum Good to know. I can confirm that I have the DI Problems when using jest and the generated queries from graphql code generator.

Yes. I was using jest with Angular

Op do 9 sep. 2021 12:50 schreef Maksym Kobieliev @.***>:

@WolfSoko https://github.com/WolfSoko No, as I’ve found out, this issue is caused by a bug in jest-preset-angular, which is not able to properly handle es6 namespace imports. And such imports are (in my case) generated by graphql code generator. This is the issue: thymikee/jest-preset-angular#963 https://github.com/thymikee/jest-preset-angular/issues/963

@Nielsb85 https://github.com/Nielsb85 @CharlieGreenman https://github.com/CharlieGreenman @Napas https://github.com/Napas could you please confirm if you were experiencing this issue when using jest with angular?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/kamilkisiela/apollo-angular/issues/1376#issuecomment-915976586, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABH6ZQKTX7577INAEZ6452TUBCGOZANCNFSM4JFRVNNQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

@kamilkisiela could you please reopen this one?

Same issue for me…

This error look like something that could be related to (https://next.angular.io/guide/ivy-compatibility)

All classes that use Angular DI must have an Angular decorator like @Directive() or @Injectable (previously, undecorated classes were allowed if an ancestor class or subclass had a decorator).

May this library could help to validate apollo ?!: https://github.com/angular/ngcc-validation