【问题标题】:Apollo GraphQL query in service duplicates collection items?apollo graphql查询服务重复收集项目?
【发布时间】:2020-01-05 18:13:12
【问题描述】:

我的 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"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }

当从阿波罗查询返回相同的查询时,我得到以下数据(瞳孔属性集合和瞳孔属性都有相同的数据);

  "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 查询返回的学生报告对象来解释第二组数据。

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

【问题讨论】:

    标签: angular graphql apollo


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2021-07-04
      • 2017-12-09
      • 2016-08-31
      • 2018-10-19
      • 2018-04-25
      • 2020-10-01
      • 2018-03-02
      • 2021-07-04
      • 2018-06-13
      相关资源
      最近更新 更多