Skip to content

Instantly share code, notes, and snippets.

@ErrorPro
Last active June 18, 2018 10:11
Show Gist options
  • Select an option

  • Save ErrorPro/e45016c943b0ccf4146c16662d7bb013 to your computer and use it in GitHub Desktop.

Select an option

Save ErrorPro/e45016c943b0ccf4146c16662d7bb013 to your computer and use it in GitHub Desktop.
import RelayQueryResponseCache from 'relay-runtime/lib/RelayQueryResponseCache';
const cache = new RelayQueryResponseCache({ size: 250, ttl: 60 * 5 * 1000 });
export default new Environment({
network: Network.create(fetchQuery),
store: new Store(new RecordSource()),
});
const fetchQuery = async function wrappedFetchQuery(
operation,
variables,
) {
const headers = {
...defaultHeaders,
};
const queryID = operation.name;
const cachedData = cache.get(queryID, variables);
if (cachedData !== null) return cachedData;
return fetch(url, {
method: 'POST',
headers,
body: JSON.stringify({
query: operation.text,
variables,
}),
})
.then(response => response.json())
.then((data) => {
if (operation.operationKind !== 'mutation') {
cache.set(queryID, variables, data);
}
return data;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment