Apollo-angular: Iterating through QueryRef

Created on 21 Mar 2018  路  4Comments  路  Source: kamilkisiela/apollo-angular

I have the following:
public posts: QueryRef<PostsInterface>;
this.posts = this._postService.get(); //in ngOnInit

And in the corresponding HTML:

And then am able to display each post's fields in html using {{post.name}}, etc.

My question is, how can I achieve the same iteration through the posts QueryRef inside typescript? When I try the following:

for (let post of this.posts) {
      console.log("Post name is: " + post.name);
    }

I get the error "Type 'QueryRef>' is not an array type or a string type."
If ngFor is able to iterate through the QueryRef, there must be some way to do it in the typescript itself?

Ultimately I want to display the data in a table with a paginator. Having trouble achieving this with mat-table. Are there any examples of displaying data grabbed with apollographql in a mat-table?

Most helpful comment

Impossibru. QuerRef is just an object, it's impossible to iterate an object so it means you're doing something wrong or you're returning an Observable in a service but you're defining it as QueryRef in a component. No other options.

All 4 comments

QueryRef is not an Observable or even an iterable object so it cannot by used with AsyncPipe.

What you're looking for is valueChanges prop of QueryRef.

If QueryRef is not Observable or Iterable, how is ngFor able to iterate through it with no issues?

Impossibru. QuerRef is just an object, it's impossible to iterate an object so it means you're doing something wrong or you're returning an Observable in a service but you're defining it as QueryRef in a component. No other options.

Thanks, I realize now the problem is I incorrectly migrated to Apollo 2.0

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GlauberF picture GlauberF  路  6Comments

maplion picture maplion  路  4Comments

kobojsaren picture kobojsaren  路  6Comments

TobiasKrogh picture TobiasKrogh  路  4Comments

bkinsey808 picture bkinsey808  路  3Comments