Skip to content

Commit

Permalink
ApolloClient does not delay the application becoming stable
Browse files Browse the repository at this point in the history
Otherwise, we might encounter a delay of 10 seconds, and this might lead
to suboptimal experience as described in https://angular.io/errors/NG0506

Fixes #2251
  • Loading branch information
PowerKiKi committed May 2, 2024
1 parent 20be177 commit a95a5f0
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-pigs-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"apollo-angular": patch
---

ApolloClient does not delay the application becoming stable
12 changes: 8 additions & 4 deletions packages/apollo-angular/src/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ export class Apollo extends ApolloBase<any> {
private map: Map<string, ApolloBase<any>> = new Map<string, ApolloBase<any>>();

constructor(
private _ngZone: NgZone,
ngZone: NgZone,
@Optional()
@Inject(APOLLO_OPTIONS)
apolloOptions?: ApolloClientOptions<any>,
@Inject(APOLLO_NAMED_OPTIONS) @Optional() apolloNamedOptions?: NamedOptions,
@Inject(APOLLO_FLAGS) @Optional() flags?: Flags,
) {
super(_ngZone, flags);
super(ngZone, flags);

if (apolloOptions) {
this.createDefault(apolloOptions);
Expand Down Expand Up @@ -180,7 +180,7 @@ export class Apollo extends ApolloBase<any> {
throw new Error('Apollo has been already created.');
}

this.client = new ApolloClient<TCacheShape>(options);
this.client = this.ngZone.runOutsideAngular(() => new ApolloClient<TCacheShape>(options));
}

/**
Expand All @@ -194,7 +194,11 @@ export class Apollo extends ApolloBase<any> {
}
this.map.set(
name,
new ApolloBase(this._ngZone, this.flags, new ApolloClient<TCacheShape>(options)),
new ApolloBase(
this.ngZone,
this.flags,
this.ngZone.runOutsideAngular(() => new ApolloClient<TCacheShape>(options)),
),
);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-angular/tests/Apollo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ describe('Apollo', () => {
});

ngZone = {
run: jest.fn(cb => cb()),
run: (cb: () => unknown) => cb(),
runOutsideAngular: (cb: () => unknown) => cb(),
} as any;
});

Expand Down

0 comments on commit a95a5f0

Please sign in to comment.