Skip to content

Commit 6a45784

Browse files
committed
New provideApollo() and provideNamedApollo()
Those two providers make it simpler to provide Apollo with options and flags to the application. Token injections, `APOLLO_OPTIONS`, `APOLLO_NAMED_OPTIONS` and `APOLLO_FLAGS` are not needed anymore in application code. Though they might still be used for custom providing techniques. `ApolloModule` is deprecated because it brings no value. Especially with the introduction of the two providing functions.
1 parent b1bef7d commit 6a45784

File tree

23 files changed

+367
-349
lines changed

23 files changed

+367
-349
lines changed

.changeset/green-eels-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'apollo-angular': minor
3+
---
4+
5+
New `provideApollo()` and `provideNamedApollo()`

packages/apollo-angular/http/tests/http-link.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { HttpClientModule, HttpHeaders } from '@angular/common/http';
44
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
55
import { TestBed } from '@angular/core/testing';
66
import { ApolloLink, execute, gql, InMemoryCache } from '@apollo/client/core';
7-
import { Apollo, ApolloModule } from '../../src';
7+
import { Apollo } from '../../src';
88
import { HttpLink } from '../src/http-link';
99

1010
const noop = () => {
@@ -17,8 +17,8 @@ describe('HttpLink', () => {
1717

1818
beforeEach(() => {
1919
TestBed.configureTestingModule({
20-
imports: [ApolloModule, HttpClientModule, HttpClientTestingModule],
21-
providers: [HttpLink],
20+
imports: [HttpClientModule, HttpClientTestingModule],
21+
providers: [HttpLink, Apollo],
2222
});
2323
httpLink = TestBed.inject(HttpLink);
2424
httpBackend = TestBed.inject(HttpTestingController);

packages/apollo-angular/schematics/install/files/standalone/graphql.provider.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

packages/apollo-angular/schematics/install/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,16 @@ function importSetup(options: Schema): Rule {
194194
const mainPath = await getMainFilePath(host, options.project);
195195
if (isStandaloneApp(host, mainPath)) {
196196
return addRootProvider(options.project, ({ code, external }) => {
197-
return code`${external('graphqlProvider', './graphql.provider')}`;
197+
return code`${external('provideApollo', 'apollo-angular')}(() => {
198+
const httpLink = inject(${external('HttpLink', 'apollo-angular/http')});
199+
200+
return {
201+
link: httpLink.create({
202+
uri: '<%= endpoint %>',
203+
}),
204+
cache: new ${external('InMemoryCache', '@apollo/client/core')}(),
205+
};
206+
})`;
198207
});
199208
} else {
200209
await addModuleImportToRootModule(host, 'GraphQLModule', './graphql.module', options.project);

packages/apollo-angular/schematics/tests/ng-add.spec.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,10 @@ describe('ng-add with standalone', () => {
9090
}
9191
});
9292

93-
test('should add graphqlProviders with GraphQL setup', async () => {
94-
const providerPath = '/projects/apollo/src/app/graphql.provider.ts';
95-
expect(tree.files).toContain(providerPath);
96-
97-
const content = getFileContent(tree, providerPath);
98-
expect(content).toMatch('export const graphqlProvider');
99-
});
100-
101-
test('should import the NgModule with GraphQL setup to the root module', async () => {
93+
test('should use `provideApollo()` to provide Apollo', async () => {
10294
const content = getFileContent(tree, '/projects/apollo/src/app/app.config.ts');
10395

104-
expect(content).toMatch(/import { graphqlProvider } from '.\/graphql.provider'/);
96+
expect(content).toMatch(/provideApollo\(\(\) => {/);
10597
});
10698

10799
test('should import HttpClientModule to the root module', async () => {
Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,52 @@
1-
import { NgModule } from '@angular/core';
1+
import { NgModule, Provider } from '@angular/core';
2+
import { ApolloClientOptions } from '@apollo/client/core';
23
import { Apollo } from './apollo';
4+
import { APOLLO_FLAGS, APOLLO_NAMED_OPTIONS, APOLLO_OPTIONS } from './tokens';
5+
import { Flags, NamedOptions } from './types';
36

4-
export const PROVIDERS = [Apollo];
5-
7+
/**
8+
* This is deprecated and will be removed in the next major version, because
9+
* Angular is moving toward a moduleless ecosystem.
10+
*
11+
* Instead, use either `provideApollo()` or `provideNamedApollo()`.
12+
*
13+
* @deprecated
14+
*/
615
@NgModule({
7-
providers: PROVIDERS,
16+
providers: [Apollo],
817
})
918
export class ApolloModule {}
19+
20+
export function provideApollo<TCacheShape = any>(
21+
optionsFactory: () => ApolloClientOptions<TCacheShape>,
22+
flags: Flags = {},
23+
): Provider {
24+
return [
25+
Apollo,
26+
{
27+
provide: APOLLO_OPTIONS,
28+
useFactory: optionsFactory,
29+
},
30+
{
31+
provide: APOLLO_FLAGS,
32+
useValue: flags,
33+
},
34+
];
35+
}
36+
37+
export function provideNamedApollo(
38+
optionsFactory: () => NamedOptions,
39+
flags: Flags = {},
40+
): Provider {
41+
return [
42+
Apollo,
43+
{
44+
provide: APOLLO_NAMED_OPTIONS,
45+
useFactory: optionsFactory,
46+
},
47+
{
48+
provide: APOLLO_FLAGS,
49+
useValue: flags,
50+
},
51+
];
52+
}

packages/apollo-angular/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export type { TypedDocumentNode } from '@apollo/client/core';
2-
export { ApolloModule } from './apollo-module';
2+
export { ApolloModule, provideApollo, provideNamedApollo } from './apollo-module';
33
export { Apollo, ApolloBase } from './apollo';
44
export { QueryRef, QueryRefFromDocument } from './query-ref';
55
export { Query } from './query';

packages/apollo-angular/tests/index.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as api from '../src';
22
import { Apollo } from '../src/apollo';
3-
import { ApolloModule } from '../src/apollo-module';
3+
import { ApolloModule, provideApollo, provideNamedApollo } from '../src/apollo-module';
44
import { gql, graphql } from '../src/gql';
55
import { QueryRef } from '../src/query-ref';
66

@@ -14,6 +14,12 @@ describe('public api', () => {
1414
test('should export ApolloModule', () => {
1515
expect(api.ApolloModule).toBe(ApolloModule);
1616
});
17+
test('should export provideApollo', () => {
18+
expect(api.provideApollo).toBe(provideApollo);
19+
});
20+
test('should export provideNamedApollo', () => {
21+
expect(api.provideNamedApollo).toBe(provideNamedApollo);
22+
});
1723
test('should export gql', () => {
1824
expect(api.gql).toBe(gql);
1925
});

packages/apollo-angular/tests/integration.spec.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,20 @@ import { HttpClientModule } from '@angular/common/http';
22
import { TestBed } from '@angular/core/testing';
33
import { InMemoryCache } from '@apollo/client/core';
44
import { mockSingleLink } from '@apollo/client/testing';
5-
import { Apollo, APOLLO_OPTIONS, ApolloModule } from '../src';
5+
import { Apollo, provideApollo } from '../src';
66

77
describe('Integration', () => {
88
describe('default', () => {
99
beforeEach(() => {
1010
TestBed.configureTestingModule({
11-
imports: [ApolloModule, HttpClientModule],
11+
imports: [HttpClientModule],
1212
providers: [
13-
{
14-
provide: APOLLO_OPTIONS,
15-
useFactory: () => {
16-
return {
17-
link: mockSingleLink(),
18-
cache: new InMemoryCache(),
19-
};
20-
},
21-
},
13+
provideApollo(() => {
14+
return {
15+
link: mockSingleLink(),
16+
cache: new InMemoryCache(),
17+
};
18+
}),
2219
],
2320
});
2421
});

packages/demo/src/app/app.config.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1+
import { provideApollo } from 'apollo-angular';
2+
import { HttpLink } from 'apollo-angular/http';
13
import { provideHttpClient } from '@angular/common/http';
2-
import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
4+
import { ApplicationConfig, inject, provideZoneChangeDetection } from '@angular/core';
35
import { provideRouter } from '@angular/router';
6+
import { InMemoryCache } from '@apollo/client/core';
47
import { routes } from './app.routes';
5-
import { graphqlProvider } from './graphql.provider';
68

79
export const appConfig: ApplicationConfig = {
810
providers: [
911
provideZoneChangeDetection({ eventCoalescing: true }),
1012
provideRouter(routes),
1113
provideHttpClient(),
12-
graphqlProvider,
14+
provideApollo(() => {
15+
const httpLink = inject(HttpLink);
16+
17+
return {
18+
link: httpLink.create({
19+
uri: 'https://swapi-graphql.netlify.app/.netlify/functions/index',
20+
}),
21+
cache: new InMemoryCache(),
22+
};
23+
}),
1324
],
1425
};

0 commit comments

Comments
 (0)