尝试获取查询片段时出现以下错误:
"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/