Skip to content

Commit

Permalink
Avoid intermediate Observable for getOne() #7739
Browse files Browse the repository at this point in the history
Similar to 2c2c875 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.
  • Loading branch information
PowerKiKi committed Dec 30, 2020
1 parent 6ce9cd9 commit 9153ada
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions projects/natural/src/lib/services/abstract-model.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -169,25 +169,18 @@ export abstract class NaturalAbstractModelService<
this.throwIfObservable(id);
this.throwIfNotQuery(this.oneQuery);

const resultObservable = new Subject<Tone>();

const queryRef = this.apollo.watchQuery<Tone, Vone>({
query: this.oneQuery,
variables: this.getVariablesForOne(id),
fetchPolicy: 'cache-and-network',
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]),
);
}

/**
Expand Down

0 comments on commit 9153ada

Please sign in to comment.