I have searched in SO and found similar questions around Observables, but none helped my specific use-case, which, I have to assume is a pretty common use-case.
I am calling a REST API (via the http module) to get a Quote object. Naturally it returns an Observable<Quote>.
I am also calling another REST API (again via the http module) to get QuoteLineItem[]. Again, this naturally returns an Observable<QuoteLineItem[]>.
Ideally the client code should only be retrieving Observable<Quote> and be able to use Quote.LineItems.
What is the best practice for this? I can come up with two options:
1- Populate Quote.LineItems from the Observable<QuoteLineItem[]> when the Observable is subscribed to. I have been trying to get this done, but I don't seem to be doing that. Here is the code, but not sure what is wrong?
// This will change to an http call
const subj: BehaviorSubject<Quote> = new BehaviorSubject(quote)
const obs: Observable<Quote> = subj.asObservable();
// Now try to get LineItems within the observable and populate Quote.LineItems
obs.pipe(switchMap((q: Quote) => {
this.quoteLineItemService.getLineItems(quoteId).subscribe((data: QuoteLineItem[]) => {
q.lineItems = data
return q
},
error => console.error(error)
)
}))
return obs;
This code is getting a compilation error: Argument of type '(q: any) => void' is not assignable to parameter of type '(value: any, index: number) => ObservableInput'. Type 'void' is not assignable to type 'ObservableInput'.
My first question is, what am I doing wrong here? How can I get that done?
2- Is the above the best practice or should I have Observable<QuoteLineItem[]> instead of QuoteLineItem[] as a Property within the Quote object?
Sorry if these are very basic questions. Your help, as always would be very much appreciated!