Last active
June 18, 2018 10:11
-
-
Save ErrorPro/e45016c943b0ccf4146c16662d7bb013 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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