简体   繁体   中英

Graphql react-apollo IntrospectionFragmentMatcher issues

After adding some interface types to our graphql queries our react-apollo app was getting the fragmentMatcher error:

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

I have followed the guide and the error does not go away, it still says I am using the heuristic fragment matcher even though I am not? Any thoughts?

Using react-apollo@1.2.0 & apollo-cache-inmemory@1.0.0 and here is my apollo config:

...

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
  },
})

For the apollo-client 1.x, you should not use the cache that wrap the fragmentMatcher. use directly fragmentMatcher in the client constructor.

The answer was to include any interface schema you may have:

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

Although I recommend using apollo v2 if possible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM