Skip to content

fix: use non-namespaced apollo-angular imports #1186

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mhayes
Copy link

@mhayes mhayes commented Jul 25, 2025

Description

Fixes a longstanding issue with being unable to write jest tests using generated services from @graphql-codegen/typescript-apollo-angular. The generated services file will now include:

import { Apollo, Query as ApolloQuery, Mutation as ApolloMutation } from 'apollo-angular';

instead of:

import * as Apollo from 'apollo-angular';

This was the suggested approach mentioned in #223 and #28 and seemed like a relatively straightforward fix that should have minimal impact on projects already using this library.

Related #223 and #28

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

Screenshots/Sandbox (if appropriate/relevant):

Without this fix in places, users that attempt to use generated service files in their jest tests will see the following error:

NG0202: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid. This can happen if the dependency type is a primitive like a string or if an ancestor of this class is missing an Angular decorator.

How Has This Been Tested?

Up until this point I've been manually running a script to swap out the imports as part of codegen's afterAllFileWrite hook which gets rid of the issue.

Test Environment:

  • OS: Mac OS 15.5
  • @graphql-codegen/typescript-apollo-angular:
  • NodeJS: 22.14.0

Checklist:

  • I have followed the
    CONTRIBUTING doc and the
    style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Further comments

One other possible fix to this problem would be to address the namespace issue present in the jest-preset-angular plugin. There's an open issue around this issue:

thymikee/jest-preset-angular#963

However I was unsure how to fix that library and the tweak to this library seemed far more straightforward.

Copy link

changeset-bot bot commented Jul 25, 2025

⚠️ No Changeset found

Latest commit: b6111fe

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@mhayes mhayes marked this pull request as ready for review July 25, 2025 21:09
@mhayes mhayes changed the title Use non-namespaced apollo-angular imports fix: use non-namespaced apollo-angular imports Jul 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant