reactjs - Apollo GraphQL - 数据有时是未定义的,并且保持不变

有时 Apollo Client 决定获取数据,有时它给我 undefined 并且它保持不变 - 即使查询完全在后端工作并且在调试器中我看到查询已启动。为什么?控制台中没有错误,一切都很好。

编辑:如果我启用快速刷新并更改组件中的某些内容,它将神奇地获取数据,但几乎从不获取第一个应用程序渲染时的数据。

我使用 React-Native。

const TEST = gql`
    query NotesFetch {
      NotesFetch {
        notes {
          description
          id
          status
          images {
            filename
            mimetype
            encoding
            uri
          }
          timestamp
          username
        }
      }
    }
  `;

  const { loading, error, data } = useQuery(TEST);

  console.log(data) - sometimes okay, sometimes it stays undefined and doesn't change

零件:

<View style={styles.cards}>
      {data && data.NotesFetch.notes &&
          data.NotesFetch.notes.map((p: any) =>  <NotesCard archive={true} key={p.id} {...p} />
        )
      }
    </View>

回答1

问题直接来自 Apollo Client 库以及它如何处理 React Native 的 observables - 简而言之。将其更新到最新版本,因为它已在 2022 年 5 月 16 日更新 - 3.6.4 中修复。

提供每个@apollo/client//*.cjs bundle 的@apollo/client//*.cjs.native.js 版本(包括依赖项 ts-invariant 和 zen-observable-ts)来帮助 React Native 的 Metro bundler 自动解析 CommonJS 入口点模块。这些更改应该使我们在下面的 v3.5.4 部分中给出的关于 metro.config.js 的建议变得不必要。 @benjamn in #9716

相似文章

随机推荐

最新文章