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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/remove-angular-namespace.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
@graphql-codegen/typescript-apollo-angular: patch
---

Change `apollo-angular` imports to named non-namespace imports
6 changes: 3 additions & 3 deletions packages/plugins/typescript/apollo-angular/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export class ApolloAngularVisitor extends ClientSideBaseVisitor<
}
}

const dependencyInjections = ['apollo: Apollo.Apollo'].concat(this.config.additionalDI);
const dependencyInjections = ['apollo: Apollo'].concat(this.config.additionalDI);
const dependencyInjectionArgs = dependencyInjections.map(content => {
return content.split(':')[0];
});
Expand All @@ -129,7 +129,7 @@ export class ApolloAngularVisitor extends ClientSideBaseVisitor<

const imports = [
`import { Injectable } from '@angular/core';`,
`import * as Apollo from '${this.config.apolloAngularPackage}';`,
`import { Apollo, Query as ApolloQuery, Mutation as ApolloMutation } from '${this.config.apolloAngularPackage}';`,
];

if (this.config.sdkClass) {
Expand Down Expand Up @@ -316,7 +316,7 @@ export class ApolloAngularVisitor extends ClientSideBaseVisitor<
@Injectable({
providedIn: ${this._providedIn(node)}
})
export class ${serviceName} extends Apollo.${operationType}<${operationResultType}, ${operationVariablesTypes}> {
export class ${serviceName} extends Apollo${operationType}<${operationResultType}, ${operationVariablesTypes}> {
${this.config.addExplicitOverride ? 'override ' : ''}document = ${this._getDocumentNodeVariable(
node,
documentVariableName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ describe('Apollo Angular', () => {
},
)) as Types.ComplexPluginOutput;

expect(content.prepend).toContain(`import * as Apollo from 'apollo-angular';`);
expect(content.prepend).toContain(
`import { Apollo, Query as ApolloQuery, Mutation as ApolloMutation } from 'apollo-angular';`,
);
expect(content.prepend).toContain(`import { Injectable } from '@angular/core';`);
await validateTypeScript(content, schema, docs, {});
});
Expand All @@ -101,7 +103,7 @@ describe('Apollo Angular', () => {
)) as Types.ComplexPluginOutput;

expect(content.content).toBeSimilarStringTo(`
constructor(apollo: Apollo.Apollo) {
constructor(apollo: Apollo) {
super(apollo);
}
}
Expand All @@ -123,7 +125,7 @@ describe('Apollo Angular', () => {
)) as Types.ComplexPluginOutput;

expect(content.content).toBeSimilarStringTo(`
constructor(apollo: Apollo.Apollo, testService: TestService, testService1: TestService1) {
constructor(apollo: Apollo, testService: TestService, testService1: TestService1) {
super(apollo, testService, testService1);
}
}
Expand Down Expand Up @@ -163,7 +165,9 @@ describe('Apollo Angular', () => {
},
)) as Types.ComplexPluginOutput;

expect(content.prepend).toContain(`import * as Apollo from 'my-custom-apollo-angular';`);
expect(content.prepend).toContain(
`import { Apollo, Query as ApolloQuery, Mutation as ApolloMutation } from 'my-custom-apollo-angular';`,
);
expect(content.prepend).toContain(`import { Injectable } from '@angular/core';`);
await validateTypeScript(content, schema, docs, {});
});
Expand Down Expand Up @@ -344,12 +348,16 @@ describe('Apollo Angular', () => {
it('Should be able to use root schema object', async () => {
const rootSchema = buildSchema(`
type RootQuery { f: String }
schema { query: RootQuery }
type RootMutation { g(input: String!): String! }
schema { query: RootQuery, mutation: RootMutation }
`);
const query = gql`
query test {
f
}
mutation testMutation {
g(input: "foo")
}
`;
const docs = [{ location: '', document: query }];
const content = (await plugin(
Expand All @@ -365,7 +373,13 @@ describe('Apollo Angular', () => {
@Injectable({
providedIn: 'root'
})
export class TestGQL extends Apollo.Query
export class TestGQL extends ApolloQuery
`);
expect(content.content).toBeSimilarStringTo(`
@Injectable({
providedIn: 'root'
})
export class TestMutationGQL extends ApolloMutation
`);
await validateTypeScript(content, rootSchema, docs, {});
});
Expand Down Expand Up @@ -756,10 +770,10 @@ describe('Apollo Angular', () => {
expect(content.content).toBeSimilarStringTo(`@Injectable({
providedIn: 'root'
})
export class TestGQL extends Apollo.Query<TestQuery, TestQueryVariables> {
export class TestGQL extends ApolloQuery<TestQuery, TestQueryVariables> {
document = Operations.TestDocument;

constructor(apollo: Apollo.Apollo) {
constructor(apollo: Apollo) {
super(apollo);
}
}`);
Expand Down