Apollo introspectionFragmentMatcher : how to use?


#1

Hello everyone,

I am using interfaces in my graphql server and want to get rid of the heuristic matcher warnings in the console.
I think I need to use introspectionFragmentMatcher with reason-apollo, but I don’t know how…

In the source code of apollo-reason there is :

    type fragmentMatcher;

        [@bs.module "apollo-cache-inmemory"] [@bs.new]
        external introspectionFragmentMatcher : Js.t({..}) => fragmentMatcher =
          "IntrospectionFragmentMatcher";

        let createIntrospectionFragmentMatcher = (~data) =>
          introspectionFragmentMatcher({"introspectionQueryResultData": data});

        /* Instantiate a new cache object */
        [@bs.obj] external
        makeApolloInMemoryCacheParams : 
        (
          ~dataIdFromObject: (Js.t({..}) => string)=?, 
          ~fragmentMatcher: fragmentMatcher=?
        ) => _ = "";

        let createInMemoryCache = (~dataIdFromObject=?, ~fragmentMatcher=?, ()) => {
            /* Apollo Client, looks for key in Object. Doesn't check if value is null  */
          apolloInMemoryCache(
             makeApolloInMemoryCacheParams(~dataIdFromObject?, ~fragmentMatcher?)
           );
        };

So I should probably use :
let inMemoryCache = ApolloInMemoryCache.createInMemoryCache( ~fragmentMatcher = ApolloInMemoryCache.createIntrospectionFragmentMatcher(~data=whatToPutHere) )

which will call :
let createIntrospectionFragmentMatcher = (~data) =>
introspectionFragmentMatcher({“introspectionQueryResultData”: data});

In apollo docs they use
import introspectionQueryResultData from ‘./fragmentTypes.json’;

What should I pass as the “data” argument to createIntrospectionFragmentMatcher?

Thanks for your help


#2

You can looks here:

your data is typed as Js.t({..}). so you would pass this:

[@bs.module] external introspectionQueryResultData: Js.t({..}) = "./fragmentTypes.json";

#3

Thank you for your answer. I’m trying to use your answer.

So at the top of the file I use :
[@bs.module] external introspectionQueryResultData: Js.t({..}) = "./fragmentTypes.json";

and then

let inMemoryCache = ApolloInMemoryCache.createInMemoryCache( 
~fragmentMatcher= ApolloInMemoryCache.createIntrospectionFragmentMatcher(~data=introspectionQueryResultData ),                                                                                                                                                                                                    
     );   

But I got an error :

  41 ┆ ReasonApollo.createApolloClient(
  42 ┆   ~link=ApolloLinks.from([|webSocketHttpLink|]),
  43 ┆   ~cache=inMemoryCache,
  44 ┆   (),
  45 ┆ ); 
 This call is missing arguments of type:
  (~?dataIdFromObject: option({.. } => string)),
  unit

Why would I get an error : if I understand correctly dataIdFromObject is an optional labeled argument so I should be able to not provide it right?

When I passed no argument I had no error but when I pass one I get the error…
If I try to do :

~dataIdFromObject=None

it doesnt work either (I dont understand why either cause it is of type " option({… } => string))" so I should be able to pass None?

UPDATE : My bad didn’t read the docs well enough.
I had to pass () at the end of the function.

From https://reasonml.github.io/docs/en/function:

Note the unit () at the end of drawCircle. Writing this particular function without the unit () would lead to the following problem. Because radius and color are both labeled, the function can be curried, and it can be applied out-of-order, it’s unclear what the following means: