Apollo GraphQL query in service duplicates collection items?

前端 未结 1 1797
迷失自我
迷失自我 2021-01-16 03:50

I have the following method in a service within my Angular 6 app;

  public getPupilReport(): Observable {
    return this.apollo.query<         


        
相关标签:
1条回答
  • 2021-01-16 04:27

    The cause of the duplicate data is the apollo caching system correctly doing its job.

    Apollo identifies an object by its __typename and id (or _id) field. Due to an error in my hardcoded test data there are objects with duplicate IDs. Correcting my data so that all objects of the same type have unique IDs (as they should) fixed this issue.

    If the id field is unavailable or the IDs are expected to be duplicated, a custom dataIdFromObject function can be provided to the InMemoryCache constructor to tell Apollo how to correctly normalize such objects.

    See here for a better explanation

    0 讨论(0)
提交回复
热议问题