Graphql react-apollo IntrospectionFragmentMatcher 问题

Graphql react-apollo IntrospectionFragmentMatcher issues

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

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

我已经按照指南进行操作,但错误并没有消失,它仍然说我正在使用启发式片段匹配器,尽管我没有使用?有什么想法吗?

使用 react-apollo@1.2.0 & apollo-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
  },
})

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

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

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

尽管我建议尽可能使用 apollo v2。