Skip to content

Instantly share code, notes, and snippets.

@ronal2do
Last active August 31, 2017 03:14
Show Gist options
  • Save ronal2do/6db9a097059c7908dbd4476a8dd18406 to your computer and use it in GitHub Desktop.
Save ronal2do/6db9a097059c7908dbd4476a8dd18406 to your computer and use it in GitHub Desktop.
/**
* @flow
*/
import {
Environment,
Network,
RecordSource,
Store,
} from 'relay-runtime';
// Define a function that fetches the results of an operation (query/mutation/etc)
// and returns its results as a Promise:
function fetchQuery(operation, variables, cacheConfig, uploadables) {
return fetch('http://localhost:5000/graphql', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
// Add authentication and other headers here
},
body: JSON.stringify({
query: operation.text, // GraphQL text from input
variables,
}),
}).then(response => {
return response.json();
});
}
// Create a network layer from the fetch function
const network = Network.create(fetchQuery);
const source = new RecordSource();
const store = new Store(source);
const environment = new Environment({
network,
store,
});
export default environment;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment