-
Notifications
You must be signed in to change notification settings - Fork 14
/
createRelayEnvironment.js
45 lines (39 loc) · 1.1 KB
/
createRelayEnvironment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* @flow
*/
import {
Environment,
Network,
RecordSource,
Store,
} from 'relay-runtime';
import { AsyncStorage } from "react-native";
// Define a function that fetches the results of an operation (query/mutation/etc)
// and returns its results as a Promise:
const prodUrl = 'https://conference-server.herokuapp.com/graphql';
const devUrl = 'http://localhost:5000/graphql';
const fetchQuery = async (operation, variables, cacheConfig, uploadables) => {
return fetch(devUrl, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
'Authorization': await AsyncStorage.getItem('token'),
}, // 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 env = new Environment({
network,
store,
});
export default env;