Skip to content

Commit bf29f13

Browse files
Support Apollo Angular 2.0 and add serviceProvidedIn option (dotansimha#4507)
* Support Apollo Angular 2.0 * Change defaults * Update test * Add serviceProvidedInRoot option to Apollo Angular plugin * fix canary publish script Co-authored-by: Dotan Simha <[email protected]>
1 parent cff7274 commit bf29f13

File tree

7 files changed

+121
-16
lines changed

7 files changed

+121
-16
lines changed

.changeset/funny-books-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript-apollo-angular': major
3+
---
4+
5+
Support Apollo Angular 2.0 by default

.changeset/tall-knives-collect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/typescript-apollo-angular': minor
3+
---
4+
5+
Allow to define the Injector of the generated SDK class in Apollo Angular plugin

dev-test/githunt/types.apolloAngular.sdk.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import gql from 'graphql-tag';
22
import { Injectable } from '@angular/core';
33
import * as Apollo from 'apollo-angular';
4-
import * as ApolloCore from 'apollo-client';
4+
import * as ApolloCore from '@apollo/client/core';
55
export type Maybe<T> = T | null;
66
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
77
/** All built-in and custom scalars, mapped to their actual values */

packages/plugins/typescript/apollo-angular/src/config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ import { RawClientSideBasePluginConfig } from '@graphql-codegen/visitor-plugin-c
1010
* To shed some more light regards this template, it's recommended to go through the this article: http://apollographql.com/docs/angular/basics/services.html , and to read the Code Generation with Apollo Angular: https://the-guild.dev/blog/apollo-angular-12
1111
*/
1212
export interface ApolloAngularRawPluginConfig extends RawClientSideBasePluginConfig {
13+
/**
14+
* @description Version of `apollo-angular` package
15+
* @default 2
16+
*
17+
* @exampleMarkdown
18+
* ```yml
19+
* config:
20+
* apolloAngularVersion: 1
21+
* ```
22+
*/
23+
apolloAngularVersion?: number;
1324
/**
1425
* @description Allows to define `ngModule` as part of the plugin's config so it's globally available.
1526
*
@@ -50,6 +61,16 @@ export interface ApolloAngularRawPluginConfig extends RawClientSideBasePluginCon
5061
* ```
5162
*/
5263
serviceProvidedInRoot?: boolean;
64+
/**
65+
* @description Define the Injector of the SDK class.
66+
*
67+
* @exampleMarkdown
68+
* ```yml
69+
* config:
70+
* serviceProvidedIn: ./path/to/module#MyModule
71+
* ```
72+
*/
73+
serviceProvidedIn?: string;
5374
/**
5475
* @description Set to `true` in order to generate a SDK service class that uses all generated services.
5576
* @default false

packages/plugins/typescript/apollo-angular/src/visitor.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ function R_DEF(directive: string) {
1919
}
2020

2121
export interface ApolloAngularPluginConfig extends ClientSideBasePluginConfig {
22+
apolloAngularVersion: number;
2223
ngModule?: string;
2324
namedClient?: string;
2425
serviceName?: string;
2526
serviceProvidedInRoot?: boolean;
27+
serviceProvidedIn?: string;
2628
sdkClass?: boolean;
2729
querySuffix?: string;
2830
mutationSuffix?: string;
@@ -59,11 +61,13 @@ export class ApolloAngularVisitor extends ClientSideBaseVisitor<
5961
ngModule: rawConfig.ngModule,
6062
namedClient: rawConfig.namedClient,
6163
serviceName: rawConfig.serviceName,
64+
serviceProvidedIn: rawConfig.serviceProvidedIn,
6265
serviceProvidedInRoot: rawConfig.serviceProvidedInRoot,
6366
querySuffix: rawConfig.querySuffix,
6467
mutationSuffix: rawConfig.mutationSuffix,
6568
subscriptionSuffix: rawConfig.subscriptionSuffix,
6669
apolloAngularPackage: rawConfig.apolloAngularPackage || 'apollo-angular',
70+
apolloAngularVersion: rawConfig.apolloAngularVersion || 2,
6771
},
6872
documents
6973
);
@@ -85,7 +89,8 @@ export class ApolloAngularVisitor extends ClientSideBaseVisitor<
8589
];
8690

8791
if (this.config.sdkClass) {
88-
imports.push(`import * as ApolloCore from 'apollo-client';`);
92+
const corePackage = this.config.apolloAngularVersion > 1 ? '@apollo/client/core' : 'apollo-client';
93+
imports.push(`import * as ApolloCore from '${corePackage}';`);
8994
}
9095

9196
const defs: Record<string, { path: string; module: string }> = {};
@@ -105,6 +110,15 @@ export class ApolloAngularVisitor extends ClientSideBaseVisitor<
105110
};
106111
});
107112

113+
if (this.config.serviceProvidedIn) {
114+
const ngModule = this._parseNgModule(this.config.serviceProvidedIn);
115+
116+
defs[ngModule.link] = {
117+
path: ngModule.path,
118+
module: ngModule.module,
119+
};
120+
}
121+
108122
Object.keys(defs).forEach(key => {
109123
const def = defs[key];
110124
// Every Angular Module that I've seen in my entire life use named exports
@@ -283,7 +297,7 @@ ${camelCase(o.node.name.value)}(variables${optionalVariables ? '?' : ''}: ${
283297
return this.${camelCase(o.serviceName)}.${actionType(o.operationType)}(variables, options)
284298
}`;
285299

286-
let watchMethod;
300+
let watchMethod: string;
287301

288302
if (o.operationType === 'Query') {
289303
watchMethod = `
@@ -306,7 +320,11 @@ ${camelCase(o.node.name.value)}Watch(variables${optionalVariables ? '?' : ''}: $
306320
.join(',\n');
307321

308322
const serviceName = this.config.serviceName || 'ApolloAngularSDK';
309-
const providedIn = this.config.serviceProvidedInRoot === false ? '' : `{ providedIn: 'root' }`;
323+
const providedIn = this.config.serviceProvidedIn
324+
? `{ providedIn: ${this._parseNgModule(this.config.serviceProvidedIn).module} }`
325+
: this.config.serviceProvidedInRoot === false
326+
? ''
327+
: `{ providedIn: 'root' }`;
310328

311329
return `
312330
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

packages/plugins/typescript/apollo-angular/tests/apollo-angular.spec.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ describe('Apollo Angular', () => {
384384
)) as Types.ComplexPluginOutput;
385385

386386
// NgModule
387-
expect(content.prepend).toContain(`import * as ApolloCore from 'apollo-client';`);
387+
expect(content.prepend).toContain(`import * as ApolloCore from '@apollo/client/core';`);
388388
expect(content.content).toBeSimilarStringTo(`
389389
@Injectable({ providedIn: 'root' })
390390
export class ApolloAngularSDK {
@@ -427,7 +427,7 @@ describe('Apollo Angular', () => {
427427
)) as Types.ComplexPluginOutput;
428428

429429
// NgModule
430-
expect(content.prepend).toContain(`import * as ApolloCore from 'apollo-client';`);
430+
expect(content.prepend).toContain(`import * as ApolloCore from '@apollo/client/core';`);
431431
// console.log('content.content', content.content);
432432
expect(content.content).toBeSimilarStringTo(`
433433
@Injectable()
@@ -447,6 +447,62 @@ describe('Apollo Angular', () => {
447447
`);
448448
await validateTypeScript(content, modifiedSchema, docs, {});
449449
});
450+
451+
it('should generate a SDK service for Apollo Angular 1.0 on demand', async () => {
452+
const modifiedSchema = extendSchema(schema, addToSchema);
453+
const myFeed = gql(`
454+
query MyFeed {
455+
feed {
456+
id
457+
}
458+
}
459+
`);
460+
const docs = [{ location: '', document: myFeed }];
461+
const content = (await plugin(
462+
modifiedSchema,
463+
docs,
464+
{
465+
sdkClass: true,
466+
apolloAngularVersion: 1,
467+
},
468+
{
469+
outputFile: 'graphql.ts',
470+
}
471+
)) as Types.ComplexPluginOutput;
472+
473+
expect(content.prepend).toContain(`import * as ApolloCore from 'apollo-client';`);
474+
});
475+
476+
it('should generate a SDK service with a requested providedIn value', async () => {
477+
const modifiedSchema = extendSchema(schema, addToSchema);
478+
const myFeed = gql(`
479+
query MyFeed {
480+
feed {
481+
id
482+
}
483+
}
484+
`);
485+
const docs = [{ location: '', document: myFeed }];
486+
const content = (await plugin(
487+
modifiedSchema,
488+
docs,
489+
{
490+
sdkClass: true,
491+
serviceProvidedIn: '../app.module#AppModule',
492+
},
493+
{
494+
outputFile: 'graphql.ts',
495+
}
496+
)) as Types.ComplexPluginOutput;
497+
498+
// NgModule import
499+
expect(content.prepend).toContain(`import { AppModule } from '../app.module';`);
500+
// NgModule in `providedIn`
501+
expect(content.content).toBeSimilarStringTo(`
502+
@Injectable({ providedIn: AppModule })
503+
export class ApolloAngularSDK {
504+
`);
505+
});
450506
});
451507

452508
describe('others', () => {

scripts/canary-release.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ async function updateVersions() {
4545
}
4646
}
4747

48-
// await applyReleasePlan(
49-
// releasePlan,
50-
// packages,
51-
// {
52-
// ...config,
53-
// commit: false,
54-
// },
55-
// false,
56-
// true
57-
// );
48+
await applyReleasePlan(
49+
releasePlan,
50+
packages,
51+
{
52+
...config,
53+
commit: false,
54+
},
55+
false,
56+
true
57+
);
5858
}
5959
}
6060
}

0 commit comments

Comments
 (0)