【问题标题】:Getting wrong data using Apollo使用 Apollo 获取错误数据
【发布时间】:2020-08-20 04:35:33
【问题描述】:

我正在尝试使用 Apollo 获取一些数据。这是简单的查询:

export const CurrentUserForProfile = gql`

    query getGroupDetails($groupId: Long, $groupType: String, $tags: [Tag]) {
        nodes: NodesRelay (
          first: 50
          where: {
            tags: { CONTAINS: $tags },
            OR: {
              AND: {
                nsParentId: { EQ: $groupId }
                nsParentType: { EQ: $groupType }
              }
              superGroupNodeLinks: {
                groupId: { EQ: $groupId }
                groupType: { EQ: $groupType }
              }
            }
          }
        ) {
          elements {
            data {
              ...nodeFields
              ext {
                ...nodeExtFields
              }
            }
          }
          pageInfo {
            ...pageInfoFields
          }
        }
      }

      fragment nodeFields on Node {
        id {
          id
          type
        }
        subtype
        internalId
        businessId
        scadaId
        isGroup
        isTemporary
        nsParentId
        nsParentType
        ouId
        description
        shortDescription
        status
        isNsPowered
        tags
        owners
        notes
        version
      }

      fragment nodeExtFields on NodeExt {
        id {
          id
          type
        }
        asParentId
        asParentType
        isAsPowered
        isAsInconsistent
        isAsPoweredInParallel
        isAsPoweredByExtSource
        isAsPoweredByDiffSource
        maneuverId
        entityStatusMap
        createdAt
        createdBy
        updatedAt
        updatedBy
        version
      }

      fragment pageInfoFields on PageInfo {
        hasNextPage
        hasPreviousPage
        startCursor
        endCursor
      }
    `;

我使用它的方式就像教程所说的那样:

this.data = this.apollo
            .watchQuery({
                query: CurrentUserForProfile,
                variables: {
                    "groupId": 15,
                    "groupType": "sec_subst",
                    "tags": ["GIS"]
                },
                fetchResults: true,
                returnPartialData: false,
            }).valueChanges
            .pipe(
                map((res: any) => res.data.nodes.elements)
            )

HTML:

<ng-container *ngFor="let a of data | async">
    {{a.data.id.type}} <br>
  </ng-container>

我得到的只是 10 行 "lv_line"。 如果我更改逻辑,订阅组件并执行console.log(data),我会看到同样的事情。这可能是正确的,但不是。我得到的数据是完全错误的,如果我在网络选项卡中检查,无论我的应用程序中的逻辑是什么,数据都是正确的,而不仅仅是具有属性 type 的 10 个元素的数组总是“lv_line”。

这是网络结果:

而 HTML 结果看起来像这样:

我做错了什么?我已经尝试了所有我能想到的方法,但都没有成功,一次又一次地得到同样的错误结果。

【问题讨论】:

    标签: angular apollo


    【解决方案1】:

    几个小时后,我设法解决了这个问题。只需使用no-cache fetchPolicy,所有问题都解决了。

    this.data = this.apollo
                .watchQuery({
                    query: CurrentUserForProfile,
                    variables: {
                        "groupId": 15,
                        "groupType": "sec_subst",
                        "tags": ["GIS"]
                    },
                    fetchResults: true,
                    returnPartialData: false,
                    fetchPolicy: "no-cache" // <-------
                }).valueChanges
                .pipe(
                    map((res: any) => res.data.nodes.elements)
                )
    

    【讨论】:

      猜你喜欢
      • 2019-05-08
      • 2018-06-30
      • 2019-07-03
      • 2020-01-30
      • 2021-10-06
      • 2021-04-29
      • 1970-01-01
      • 2020-09-25
      • 2021-08-24
      相关资源
      最近更新 更多