服务中的 Apollo GraphQL 查询重复集合项?

Apollo GraphQL query in service duplicates collection items?

我的 Angular 6 应用程序中的服务中有以下方法;

  public getPupilReport(): Observable<PupilReport> {
    return this.apollo.query<any>({
      query: gql`
            query query {
              pupilReports {
                new {
                  date
                  pupilReportTemplateId
                  pupilReportTemplate {
                    id
                    name
                    sortedPupilAttributeCollections {
                      sortOrder
                      pupilAttributeCollection {
                        id
                        name
                        sortedPupilAttributes {
                          sortOrder
                          pupilAttribute {
                            id
                            name
                            type
                          }
                        }
                      }
                    }
                  }
                }
              }
            }`,
    })
      .pipe(map(result => {
        var pupilReport = new PupilReport();
        if (result && result.data.pupilReports.new) {
          pupilReport = result.data.pupilReports.new;
        }
        return pupilReport;
      }));
  }

当我 运行 使用 GraphIQL 进行上述查询时,我得到以下返回数据;

  "data": {
    "pupilReports": {
      "new": {
        "date": "0001-01-01",
        "pupilReportTemplateId": 0,
        "pupilReportTemplate": {
          "id": 99,
          "name": "KS3 Science",
          "sortedPupilAttributeCollections": [
            {
              "sortOrder": 1,
              "pupilAttributeCollection": {
                "id": 1,
                "name": "Attainment",
                "sortedPupilAttributes": [
                  {
                    "sortOrder": 1,
                    "pupilAttribute": {
                      "id": 1,
                      "name": "Physics",
                      "type": "Boolean"
                    }
                  },
                  {
                    "sortOrder": 2,
                    "pupilAttribute": {
                      "id": 1,
                      "name": "Biology",
                      "type": "Int32"
                    }
                  }
                ]
              }
            },
            {
              "sortOrder": 2,
              "pupilAttributeCollection": {
                "id": 1,
                "name": "Behaviour",
                "sortedPupilAttributes": [
                  {
                    "sortOrder": 1,
                    "pupilAttribute": {
                      "id": 3,
                      "name": "Attitude",
                      "type": "Int32"
                    }
                  },
                  {
                    "sortOrder": 2,
                    "pupilAttribute": {
                      "id": 4,
                      "name": "Effort",
                      "type": "Boolean"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }

当来自 apollo 查询的相同查询 returns 时,我得到以下数据(pupilAttributeCollections 和 pupilAttributes 都具有相同的数据);

  "data": {
    "pupilReports": {
      "new": {
        "date": "0001-01-01",
        "pupilReportTemplateId": 0,
        "pupilReportTemplate": {
          "id": 99,
          "name": "KS3 Science",
          "sortedPupilAttributeCollections": [
            {
              "sortOrder": 1,
              "pupilAttributeCollection": {
                "id": 1,
                "name": "Attainment",
                "sortedPupilAttributes": [
                  {
                    "sortOrder": 1,
                    "pupilAttribute": {
                      "id": 1,
                      "name": "Physics",
                      "type": "Boolean"
                    }
                  },
                  {
                    "sortOrder": 2,
                    "pupilAttribute": {
                      "id": 1,
                      "name": "Physics",
                      "type": "Boolean"
                    }
                  }
                ]
              }
            },
            {
              "sortOrder": 2,
              "pupilAttributeCollection": {
                "id": 1,
                "name": "Attainment",
                "sortedPupilAttributes": [
                  {
                    "sortOrder": 1,
                    "pupilAttribute": {
                      "id": 1,
                      "name": "Physics",
                      "type": "Boolean"
                    }
                  },
                  {
                    "sortOrder": 1,
                    "pupilAttribute": {
                      "id": 1,
                      "name": "Physics",
                      "type": "Boolean"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }

我正在根据检查从 apollo 查询返回的 pupilReport 对象来解释第二组数据。

任何人都可以解释为什么会这样吗?来自集合本身的数据在服务器中被硬编码,因此第二组数据不可能是正确的。我只能假设它与缓存有关。

重复数据的原因是 apollo 缓存系统正确地完成了它的工作。

Apollo 通过其 __typename 和 id(或 _id)字段来识别对象。由于我的硬编码测试数据中存在错误,因此存在具有重复 ID 的对象。更正我的数据,使同一类型的所有对象都具有唯一 ID(它们应该如此)解决了这个问题。

如果 id 字段不可用或 ID 预计会重复,可以向 InMemoryCache 构造函数提供自定义 dataIdFromObject 函数,以告诉 Apollo 如何正确规范化此类对象。

请参阅 here 以获得更好的解释