As I mentioned in the comment I found a solution for moving to query
'ies to services or resolvers, however still no luck with watchQuery
'ies (but I also found that they are not really necessary for my application).
Below I give you some snippets that may be helpful.
I define methods that are responsible for performing queries in my service:
public allItems(): Observable<ApolloQueryResult<Object>> {
return this.apollo.query({
query: allItemsQuery
});
}
Where allItemsQuery
is a constant query string:
export const allItemsQuery = gql`
query allItems {
items {
id
name
}
}
`;
Than in my resolvers I just call the method allItems
from the service:
public resolve(route: ActivatedRouteSnapshot): Observable<ApolloQueryResult<Object>> | boolean {
return this.itemsService.allItems ? this.itemsService.allItems() : false;
}
And extract route resolved data in my component:
this.activatedRoute.data.subscribe(data: {items: Item[]}) => {
console.log(items)
}
Of course if in the routing configuration resolver is called with name items
like:
{
path: 'items',
component: ItemsComponent,
resolve: {
items: ItemsResolve
}
}
If you don't want to use the queries via resolvers you are of course able to call desired methods from the components and there subscribe to them. There is also a possibility to subscribe in the services and return plain objects/arrays instead of Observable<ApolloQueryResult<T>>
but due to subscriptions/unsubcriptions I think that the presented solution is more convenient.