<commit_before>
/**
 *  Optimistic cache updates
 */
import Mingo from 'mingo';


/**
 * Safe getter
 * Must returns null if the document is absent (eg in case of validation failure)
 * TODO: use this getter
 * @param {*} mutation 
 * @param {*} mutationName 
 */
const getDocumentFromMutation = (mutation, mutationName) => {
    const mutationData = (mutation.result.data[mutationName] || {});
    const document = mutationData.data;
    return document;
};

// When using multi queries, we can't track all parameters, which are sadly needed
// by cache.readQuery for optimistic updates.
// This function can get a list of queries based on their name and should solve this issue
// @see https://gist.github.com/ngryman/6856c7eb8f9a15b1095032a6ba478c5c
// @see https://github.com/apollographql/react-apollo/issues/708#issuecomment-506975142
// @see https://github.com/apollographql/apollo-client/issues/3505
// @see https://github.com/apollographql/apollo-client/issues/3505#issuecomment-535388194
export const getVariablesListFromCache = (proxy, queryName) => {
    //const queryName = query.definitions[0].name.value;
    const rootQuery = proxy.data.data.ROOT_QUERY;

    // XXX: When using `optimisticResponse`, `proxy.data.data` resolves to
    // another cache that doesn't contain the root query.
    if (!rootQuery) return [];

    const matchQueryReducer = (names, name) => {
        // TODO: can be improved with a regex so "customer" matched only "customer(*)"
        // this may match too many items
        if (name.startsWith(queryName + '(')) {
            names.push(name);
        }
        return names;
    };

    const parseQueryNameToVariables = (name) =>
        JSON.parse((name.match(/{.*}/))[0]);

    return Object.keys(rootQuery)
        .reduce(matchQueryReducer, [])
        .map(parseQueryNameToVariables);
};
<commit_msg>comment in cacheUpdate.js<commit_after>
/**
 *  Optimistic cache updates
 */
import Mingo from 'mingo';


/**
 * Safe getter
 * Must returns null if the document is absent (eg in case of validation failure)
 * TODO: use this getter
 * @param {*} mutation 
 * @param {*} mutationName 
 */
const getDocumentFromMutation = (mutation, mutationName) => {
    const mutationData = (mutation.result.data[mutationName] || {});
    const document = mutationData.data;
    return document;
};

// When using multi queries, we can't track all parameters, which are sadly needed
// by cache.readQuery for optimistic updates.
// This function can get a list of queries based on their name and should solve this issue
// @see https://gist.github.com/ngryman/6856c7eb8f9a15b1095032a6ba478c5c
// @see https://github.com/apollographql/react-apollo/issues/708#issuecomment-506975142
// @see https://github.com/apollographql/apollo-client/issues/3505
// @see https://github.com/apollographql/apollo-client/issues/3505#issuecomment-535388194
export const getVariablesListFromCache = (proxy, queryName) => {
    //const queryName = query.definitions[0].name.value;
    const rootQuery = proxy.data.data.ROOT_QUERY;

    // XXX: When using `optimisticResponse`, `proxy.data.data` resolves to
    // another cache that doesn't contain the root query.
    if (!rootQuery) return [];

    // Customer(*) will be matched but no customer. This last one would cause an error in
    // JSON.parse. If wanted to be treated, parseQueryNameToVariables should be adapted
    const matchQueryReducer = (names, name) => {
        if (name.startsWith(queryName + '(')) {
            names.push(name);
        }
        return names;
    };

    const parseQueryNameToVariables = (name) =>
        JSON.parse((name.match(/{.*}/))[0]);

    return Object.keys(rootQuery)
        .reduce(matchQueryReducer, [])
        .map(parseQueryNameToVariables);
};

{'fraction_non_alphanumeric': 0.12413793103448276, 'fraction_numerical': 0.027586206896551724, 'mean_word_length': 3.395104895104895, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6886608', 'n_tokens_mistral': 1203, 'n_tokens_neox': 1120, 'n_words': 311}