apollo-angular-services icon indicating copy to clipboard operation
apollo-angular-services copied to clipboard

Fetch() and watchQuery() don't return a type

Open Hganavak opened this issue 5 years ago • 0 comments

Hello,

I really like this project - very cool! 👍

  • In all your YouTube videos/articles/docs you always store the results of fetch() or watchQuery() into an object of type any.
  • Is there a way we can specify that the query is going to return something of a predefined type w/o having to do explicit typecasting?

For example, I have a query:

query AllEquipment {
    equipmentCollection {
        items {
            title
            summary
        }
    }
}

So logically I would like to be able to store the result of using this service's fetch() into an object of type EquipmentCollection (which is generated from our schema), e.g:

let allEquipment: ArticleCollection = this.allEquipmentGQL.fetch().subscribe(res => res.data.equipmentCollection);

But that doesn't work, so the only ways I've been able to get it to work is with explicit typecasting, e.g:

this.allEquipmentGQL.fetch()
  .pipe(pluck('data', 'equipmentCollection'))
  .subscribe(res => {
      this.allEquipment = <EquipmentCollection>res;
  });

// OR:
let allEquipment: EquipmentCollection;
this.allEquipmentGQL.fetch().subscribe(res => allEquipment = <EquipmentCollection>res.data.equipmentCollection);

Or if that's not possible, can I just want to return an observable instead somehow:

public allEquipment$: Observable<EquipmentCollection>;
...
// Something like this:
this.allEquipment = this.allEquipmentGQL.fetch().pipe(pluck('data', 'equipmentCollection');

Thanks!

Hganavak avatar Jul 16 '20 00:07 Hganavak