From 9153ada71f9c31c5887c1dd11ec3d7f100d9f8be Mon Sep 17 00:00:00 2001 From: Adrien Crivelli Date: Wed, 30 Dec 2020 12:46:27 +0900 Subject: [PATCH] Avoid intermediate Observable for getOne() #7739 Similar to 2c2c8752d145695f2b3c9934aa140e9ed6454c53 but for getOne(). And for the same reason too. We must be able to catch error in our apps if needed, so we must avoid intermediate observable that do not properly forward errors. With this fix we finally restore the historical behavior of redirecting to an error page when our app resolve an non-existing object. Most likely, we should to the same for getAll/watchAll some day. --- .../lib/services/abstract-model.service.ts | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/projects/natural/src/lib/services/abstract-model.service.ts b/projects/natural/src/lib/services/abstract-model.service.ts index cd69bd6d..37e20a44 100644 --- a/projects/natural/src/lib/services/abstract-model.service.ts +++ b/projects/natural/src/lib/services/abstract-model.service.ts @@ -1,12 +1,12 @@ import {Apollo, gql} from 'apollo-angular'; -import {NetworkStatus, WatchQueryFetchPolicy, FetchResult} from '@apollo/client/core'; +import {FetchResult, NetworkStatus, WatchQueryFetchPolicy} from '@apollo/client/core'; import {AbstractControl, AsyncValidatorFn, FormControl, FormGroup, ValidatorFn} from '@angular/forms'; import {DocumentNode} from 'graphql'; import {debounce, defaults, merge, mergeWith, omit, pick} from 'lodash-es'; -import {Observable, of, OperatorFunction, ReplaySubject, Subject, Subscription} from 'rxjs'; -import {debounceTime, filter, first, map, shareReplay, switchMap, takeUntil} from 'rxjs/operators'; +import {Observable, of, OperatorFunction, ReplaySubject, Subscription} from 'rxjs'; +import {debounceTime, filter, first, map, shareReplay, switchMap, takeUntil, takeWhile} from 'rxjs/operators'; import {NaturalQueryVariablesManager} from '../classes/query-variable-manager'; import {Literal} from '../types/types'; import {makePlural, mergeOverrideArray, relationsToIds, upperCaseFirstLetter} from '../classes/utility'; @@ -169,8 +169,6 @@ export abstract class NaturalAbstractModelService< this.throwIfObservable(id); this.throwIfNotQuery(this.oneQuery); - const resultObservable = new Subject(); - const queryRef = this.apollo.watchQuery({ query: this.oneQuery, variables: this.getVariablesForOne(id), @@ -178,16 +176,11 @@ export abstract class NaturalAbstractModelService< nextFetchPolicy: 'cache-only', }); - const subscription = queryRef.valueChanges.pipe(filter(r => !!r.data)).subscribe(result => { - const data = (result.data as Literal)[this.name]; - resultObservable.next(data); - if (result.networkStatus === NetworkStatus.ready) { - resultObservable.complete(); - subscription.unsubscribe(); - } - }); - - return resultObservable; + return queryRef.valueChanges.pipe( + filter(result => !!result.data), + takeWhile(result => result.networkStatus !== NetworkStatus.ready, true), + map(result => (result.data as Literal)[this.name]), + ); } /**