Context.overwrite is being set to true meaning that the existing arg in the merge function of apollo cache is always undefined

Hi All.

This feels like a bug in apollo client or in the inMemoryCache but could of course be something I’m doing wrong. Basically, using the chrome dev tools extension for apollo I can see that for the uuid of the outgoing/incoming request there are already multiple cached results. Also when I set a breakpoint in the merge function and then step back in the execution stack I can see that “existing” DOES have entries but it is voided when execution reaches policies.ts on lines 862 to 664, due to the context.overwrite boolean being truthy:

    // If cache.writeQuery or cache.writeFragment was called with
    // options.overwrite set to true, we still call merge functions, but
    // the existing data is always undefined, so the merge function will
    // not attempt to combine the incoming data with the existing data.
    if (context.overwrite) {
      existing = void 0;
    }

After execution of the lines above, when logging out “existing” in the merge function below, it’s value is ALWAYS undefined. I don’t set the overwrite option anywhere and it’s default is supposed to be false. What am I missing, or is this a bug?

new ApolloClient({ cache: new InMemoryCache({
    typePolicies: {
        DataResult: {
            keyFields: ["UUID", "isDelta"] 
        },
        Query: {
            fields: {
                getData: {
                    keyArgs: ["request", ["UUID"]],
                    merge(existing, incoming, { readField, args, toReference }){
                        console.log(existing);
                    }
                }
            }
        }
    }
}), ApolloLink.from([
        new RetryLink({
            delay: {
                initial: 300,
                max: 30000,
                jitter: false
            },
            attempts: {
                max: retry_max,
                retryIf
            }
        }),
        new BatchHttpLink({
            uri,
            batchInterval: batch_interval,
            headers: headers
        })
    ])
});

In case this helps anyone else struggling with similar issue, there was a reasonably recent change to the default behaviour that switched context.overwrite to true for any refetch operation. The solution to this is an as yet seemingly undocumented option that can be passed to either the apollo client instantiation or the in the options to the react query hooks. The option is refetchWritePolicy: "merge" (default is “overwrite”). and This can be configured like so:

const query_options = useMemo(() => ({
        onCompleted: onResultCallback, fetchPolicy: fetch_policy, refetchWritePolicy: "merge", notifyOnNetworkStatusChange: true, returnPartialData: false, errorPolicy: "all"
    }), [fetch_policy, onResultCallback]);

const [sendRequest, { error }] = useLazyQuery(gql`${query}`, { ...query_options, variables });