首页 文章

Graphql react-apollo IntrospectionFragmentMatcher问题

提问于
浏览
0

在我们的graphql查询中添加一些接口类型之后,我们的react-apollo应用程序收到了fragmentMatcher错误:

您正在使用简单(启发式)片段匹配器,但您的查询包含联合或接口类型 . Apollo客户端将无法准确映射片段 . 要使此错误消失,请使用文档中描述的IntrospectionFragmentMatcher:http://dev.apollodata.com/react/initialization.html#fragment-matcher

我已经按照指南,错误没有消失,它仍然说我正在使用启发式片段匹配器,即使我不是?有什么想法吗?

使用 react-apollo@1.2.0apollo-cache-inmemory@1.0.0 ,这是我的apollo配置:

...

import {
  ApolloClient,
  createNetworkInterface,
  IntrospectionFragmentMatcher,
} from 'react-apollo'
import {InMemoryCache} from 'apollo-cache-inmemory'
import SchemaData from '../data/schema.json'

const filteredData = SchemaData.data.__schema.types.filter(
  type => type.possibleTypes !== null,
)
const fragmentMatcher = new IntrospectionFragmentMatcher({
  introspectionQueryResultData: {
    __schema: {
      types: filteredData,
    },
  },
})
const cache = new InMemoryCache({fragmentMatcher})
...

const client = new ApolloClient({
  cache,
  networkInterface,
  dataIdFromObject: result => {
    if (result.uuid && result.__typename) {
      return result.__typename + result.uuid
    }
    return null
  },
})

2 回答

  • 0

    对于apollo-client 1.x,您不应该使用包装fragmentMatcher的缓存 . 在客户端构造函数中直接使用fragmentMatcher .

  • 0

    答案是包含您可能具有的任何接口架构:

    fragmentMatcher: new IntrospectionFragmentMatcher({
      introspectionQueryResultData: {
        __schema: {
          types: [
            {
              kind: 'INTERFACE',
              name: 'Document',
              possibleTypes: [
                {name: 'MyInterface1'},
                {name: 'SomeInterface2'},
              ],
            },
          ],
        },
      },
    }),
    

    虽然我建议尽可能使用apollo v2 .

相关问题