javascript - readFragment 导致 IntrospectionFragmentMatcher 错误,尽管没有使用接口(interface)或联合

标签 javascript reactjs apollo react-apollo apollo-client

尝试获取查询片段时出现以下错误:

"You are using the simple (heuristic) fragment matcher, but your queries contain union or interface types. Apollo Client will not be able to accurately map fragments. To make this error go away, use the IntrospectionFragmentMatcher as described in the docs: https://www.apollographql.com/docs/react/recipes/fragment-matching.html"

这是触发错误的调用。

const sourceData = cache.readFragment({
        id: `${typename}:${idToAdd}`,
        fragment: gql`
          ${sourceFragment}
        `,
      });

sourceFragment 在哪里

fragment series on PriceSeries {
    id
    title
    description
    commodity
    region
    location
    isDiscontinued
    order
}

Apollo 似乎将片段的返回值归类为联合或接口(interface)。我不认为添加 IntrospectionFragmentMatcher 是必要的,因为我们不需要联合或接口(interface),尽管也许我错了。将 __typename 添加到片段中没有帮助。

  • 如何消除此错误(我可能只是缺少一些用于输入的 gql 语法?)
  • 使用片段是否意味着我们应该设置 IntrospectionFragmentMatcher 无论如何..(文档说它应该只需要在接口(interface)或联合上使用片段时我们不是)
  • 错误是错误吗,我应该只是隐藏错误吗(如果您忽略它,它确实会按原样工作)

更新

ApolloClient创建如下

 client = new ApolloClient({
      link: createLink('MY_URL', Apollo.fetch),
      cache: new InMemoryCache(),
    });

最佳答案

您需要使用 dataIdFromObject 创建 apollo 缓存和设置规范化功能。

import { InMemoryCache, IntrospectionFragmentMatcher } from 'apollo-cache-inmemory';

const fragmentMatcher = new IntrospectionFragmentMatcher({
  // Here you may want to pass your introspection query result data,
});

const cache = new InMemoryCache({
  dataIdFromObject: o => o.id,
  fragmentMatcher,
});

关于javascript - readFragment 导致 IntrospectionFragmentMatcher 错误,尽管没有使用接口(interface)或联合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55297699/

相关文章:

javascript - 忽略对象中未定义的值

javascript - 处理两个完全独立的 React 组件之间的数据

javascript - 使用 Date 对象的组件在不同的时区产生不同的快照

reactjs - 如何在不发生突变的情况下重新获取查询

javascript - 编辑 JavaScript 函数,使其仅适用于指定的 div,而不适用于所有 div

javascript - 正则表达式 - 查找排除某些模式的所有唯一字符串

reactjs - 在 url 更改时重新渲染组件

reactjs - 初始化后更新 ApolloClient header

postgresql - 如何在 prisma 中将 createMany 与外键一起使用?

javascript - 使用javascript在屏幕中间显示弹出窗口