<commit_before>// @flow

import urlJoin from 'url-join';
import { Environment, Network, RecordSource, Store } from 'relay-runtime';
import md5 from 'blueimp-md5';
import { API } from '@ncigdc/utils/constants';

const source = new RecordSource();
const store = new Store(source);
const simpleCache = {};
const handlerProvider = null;

function fetchQuery(operation, variables, cacheConfig) {
  const body = JSON.stringify({
    query: operation.text, // GraphQL text from input
    variables,
  });

  const hash = md5(body);
  const [componentName] = operation.name.split('_relayQuery');

  if (simpleCache[hash]) {
    return Promise.resolve(simpleCache[hash]);
  }

  return fetch(urlJoin(API, `graphql/${componentName}?hash=${hash}`), {
    method: 'POST',
    headers: {
      'content-type': 'application/json',
    },
    body,
  })
    .then(response => response.json())
    .then(json => {
      simpleCache[hash] = json;
      return json;
    });
}

// Create a network layer from the fetch function
const network = Network.create(fetchQuery);

export default new Environment({
  handlerProvider, // Can omit.
  network,
  store,
});<commit_msg>🐛 don't cache failed requests (#2081)<commit_after>// @flow

import urlJoin from 'url-join';
import { Environment, Network, RecordSource, Store } from 'relay-runtime';
import md5 from 'blueimp-md5';
import { API } from '@ncigdc/utils/constants';

const source = new RecordSource();
const store = new Store(source);
const simpleCache = {};
const handlerProvider = null;

function fetchQuery(operation, variables, cacheConfig) {
  const body = JSON.stringify({
    query: operation.text, // GraphQL text from input
    variables,
  });

  const hash = md5(body);
  const [componentName] = operation.name.split('_relayQuery');

  if (simpleCache[hash]) {
    return Promise.resolve(simpleCache[hash]);
  }

  return fetch(urlJoin(API, `graphql/${componentName}?hash=${hash}`), {
    method: 'POST',
    headers: {
      'content-type': 'application/json',
    },
    body,
  }).then(response =>
    response.json().then(json => {
      if (response.status === 200) {
        simpleCache[hash] = json;
      }

      return json;
    }),
  );
}

// Create a network layer from the fetch function
const network = Network.create(fetchQuery);

export default new Environment({
  handlerProvider, // Can omit.
  network,
  store,
});
{'fraction_non_alphanumeric': 0.14453781512605043, 'fraction_numerical': 0.0054621848739495795, 'mean_word_length': 3.75249500998004, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '672548', 'n_tokens_mistral': 726, 'n_tokens_neox': 711, 'n_words': 195}