-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.js
123 lines (110 loc) · 3.39 KB
/
App.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { AppLoading } from "expo";
import { Asset } from "expo-asset";
import * as Font from "expo-font";
import React, { useState } from "react";
import {
Platform,
StatusBar,
StyleSheet,
View,
AsyncStorage,
} from "react-native";
import { Ionicons } from "@expo/vector-icons";
//this is where apollo starts
import { ApolloProvider } from "react-apollo";
import { ApolloClient } from "apollo-client";
import { createHttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import { Query } from "react-apollo";
import gql from "graphql-tag";
import { setContext } from "apollo-link-context";
import AppNavigator from "./navigation/AppNavigator";
import { USER_TOKEN } from "./constants/constants";
export default function App(props) {
// Apollo Client
const httpLink = createHttpLink({
uri: "http://oasis1909.herokuapp.com/",
// uri: "http://localhost:4000/",
});
// This middleware gets the authentication token from AsyncStorage if it exists.
// This middleware will be invoked every time ApolloClient sends a request to the server (as seen below).
const authLink = setContext(async (_, { headers }) => {
const token = await AsyncStorage.getItem(USER_TOKEN);
return {
headers: {
...headers,
authorization: token ? `Bearer ${token}` : "",
},
};
});
// Apollo Links allow you to create middlewares that let you modify requests before they are sent to the server.
// https://github.com/apollographql/apollo-link
// We return the headers to the context so httpLink can read them.
const defaultOptions = {
watchQuery: {
fetchPolicy: "network-only",
errorPolicy: "ignore",
},
query: {
fetchPolicy: "network-only",
errorPolicy: "all",
},
mutate: {
errorPolicy: "all",
},
};
const client = new ApolloClient({
// ssrMode: true,
// ssr: true,
// disableNetworkFetches: true,
// resultCaching: true,
link: authLink.concat(httpLink),
defaultOptions: defaultOptions,
cache: new InMemoryCache(),
});
const [isLoadingComplete, setLoadingComplete] = useState(false);
if (!isLoadingComplete && !props.skipLoadingScreen) {
return (
<AppLoading
startAsync={loadResourcesAsync}
onError={handleLoadingError}
onFinish={() => handleFinishLoading(setLoadingComplete)}
/>
);
} else {
return (
//Passing apollo client to components/screens/navigation
<ApolloProvider client={client}>
<View style={styles.container}>
{Platform.OS === "ios" && <StatusBar barStyle="default" />}
<AppNavigator />
</View>
</ApolloProvider>
);
}
}
async function loadResourcesAsync() {
await Promise.all([
Font.loadAsync({
// This is the font that we are using for our tab bar
...Ionicons.font,
// We include SpaceMono because we use it in RecScreen.js. Feel free to
// remove this if you are not using it in your app
"space-mono": require("./assets/fonts/SpaceMono-Regular.ttf"),
}),
]);
}
function handleLoadingError(error) {
// In this case, you might want to report the error to your error reporting
// service, for example Sentry
console.warn(error);
}
function handleFinishLoading(setLoadingComplete) {
setLoadingComplete(true);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
},
});