Apollo-client: Nested fragments throw heuristic fragment matching errors

Created on 9 Oct 2019  Â·  6Comments  Â·  Source: apollographql/apollo-client

Intended outcome:

Was trying to nest inline fragments into another fragment or use an interface, or union. Got an error about heuristic fragment matching.

Actual outcome:

When using nested fragments, unions or interfaces, InMemory will throw a heuristic fragment matching error:

WARNING: heuristic fragment matching going on!
fragment CharacterFields on Character {
  ... on Droid {
    primaryFunction
  }
  ... on Human {
    height
  }
}

query {
  characters {
    ...CharacterFields
  }
}

This works:

query {
  characters {
    ... on Droid {
      primaryFunction
    }
    ... on Human {
      height
    }
  }
}

How to reproduce the issue:

https://github.com/ledenis/apollo-introspection-fragment-matcher-issue

Versions

  System:
    OS: macOS 10.15
  Binaries:
    Node: 10.16.0 - ~/.nvm/versions/node/v10.16.0/bin/node
    Yarn: 1.16.0 - ~/.nvm/versions/node/v10.16.0/bin/yarn
    npm: 6.11.3 - ~/.nvm/versions/node/v10.16.0/bin/npm
  Browsers:
    Chrome: 77.0.3865.90
    Firefox: 68.0.2
    Safari: 13.0.2
  npmPackages:
    apollo-angular: 1.7.0 => 1.7.0 
    apollo-angular-link-http: 1.8.0 => 1.8.0 
    apollo-angular-link-persisted: 1.1.0 => 1.1.0 
    apollo-cache-inmemory: 1.6.3 => 1.6.3 
    apollo-cache-persist: 0.1.1 => 0.1.1 
    apollo-client: 2.6.4 => 2.6.4 
    apollo-link: 1.2.13 => 1.2.13 
    apollo-link-context: 1.0.19 => 1.0.19 
    apollo-link-error: 1.1.12 => 1.1.12 
    apollo-link-http: 1.5.16 => 1.5.16 
    apollo-link-ws: 1.0.19 => 1.0.19 
    apollo-server-core: 2.9.5 => 2.9.5 
    apollo-server-testing: 2.9.5 => 2.9.5

Related: https://github.com/apollographql/apollo-client/issues/3397
Partial temporary workaround (doesn't work at all for interfaces):

const fragmentMatcher = new IntrospectionFragmentMatcher({
  introspectionQueryResultData: {
    __schema: {
      types: []
    }
  }
});

Most helpful comment

@chandan-reddy-k

This problem was in existence from previous versions of apollo but the latest version ideally shouldn’t throw this warning. I solved this by fetching the entire schema into application. I wrote a detailed blog on this, Check it out:

https://medium.com/@chandan.reddy/whats-going-on-with-the-heuristic-fragment-matcher-in-graphql-apollo-client-e721075e92be

This fixed the problem for me, but just passing an empty array also works. Why fetch the schema ??

All 6 comments

Any updates on this? Above solution is working -- so far

Have you tried this? https://www.apollographql.com/docs/react/data/fragments/#fragments-on-unions-and-interfaces

From the blog post I think Apollo Client 3 new InMemoryCache fixes the above but I haven't had a chance to verify yet as it'll be quite a dangerous update for us with the whole site using it.

This problem was in existence from previous versions of apollo but the latest version ideally shouldn’t throw this warning. I solved this by fetching the entire schema into application. I wrote a detailed blog on this, Check it out:

https://medium.com/@chandan.reddy/whats-going-on-with-the-heuristic-fragment-matcher-in-graphql-apollo-client-e721075e92be

@chandan-reddy-k

This problem was in existence from previous versions of apollo but the latest version ideally shouldn’t throw this warning. I solved this by fetching the entire schema into application. I wrote a detailed blog on this, Check it out:

https://medium.com/@chandan.reddy/whats-going-on-with-the-heuristic-fragment-matcher-in-graphql-apollo-client-e721075e92be

This fixed the problem for me, but just passing an empty array also works. Why fetch the schema ??

Was this page helpful?
0 / 5 - 0 ratings