-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
120 lines (105 loc) · 3.31 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
import { ApolloProvider } from '@apollo/react-hooks'
import AsyncStorage from '@react-native-community/async-storage'
import {
InMemoryCache,
// IntrospectionFragmentMatcher,
} from 'apollo-cache-inmemory'
import { ApolloClient } from 'apollo-client'
import { split } from 'apollo-link'
import { setContext } from 'apollo-link-context'
import { createHttpLink } from 'apollo-link-http'
import { WebSocketLink } from 'apollo-link-ws'
import { getMainDefinition } from 'apollo-utilities'
import React, { useEffect, useState } from 'react'
import { ActivityIndicator, View } from 'react-native'
import styles from './App.style'
import RequestsProvider from './Context/Requests'
import AuthStore from './hooks-store/auth-details'
import SearchStore from './hooks-store/search-store'
import SignupStore from './hooks-store/signup-store'
import ClientStore from './hooks-store/client-store'
import { useStore } from './hooks-store/store'
import SubApp from './SubApp'
// import introspectionQueryResultData from './fragmentTypes.json'
import { resolvers } from './GraphQL'
AuthStore()
ClientStore()
SearchStore()
SignupStore()
const App = () => {
const dispatch = useStore(false)[1]
const { token } = useStore()[0].details
const { client } = useStore()[0]
useEffect(() => {
console.ignoreWarnings = [
'Found @client directives',
'Setting a timer for a long period of time',
]
const effect = async () => {
const url = '://aakashmeshramnotesappgraphql.herokuapp.com'
let authToken = (await AsyncStorage.getItem('token')) || null
if (authToken && !token) {
const userId = (await AsyncStorage.getItem('userId')) || null
const expiresOn = (await AsyncStorage.getItem('expiresOn')) || null
dispatch('SET_DETAILS', { token: authToken, userId, expiresOn })
}
const httpLink = createHttpLink({
uri: `https${url}`,
})
const wsLink = new WebSocketLink({
uri: `wss${url}`,
options: {
reconnect: true,
...(authToken && {
connectionParams: {
Authorization: `Bearer ${authToken}`,
authToken,
},
}),
},
})
const authLink = setContext((_, { headers = {} }) => {
if (authToken) {
headers.authorization = `Bearer ${authToken}`
}
return { headers }
})
const link = split(
({ query }) => {
const definition = getMainDefinition(query)
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
)
},
wsLink,
httpLink
)
/* const fragmentMatcher = new IntrospectionFragmentMatcher({
introspectionQueryResultData,
}) */
const client = new ApolloClient({
link: authLink.concat(link),
cache: new InMemoryCache(/* { fragmentMatcher } */),
resolvers,
})
dispatch('SET_CLIENT', client)
}
effect()
}, [token])
if (client) {
return (
<ApolloProvider client={client}>
<RequestsProvider>
<SubApp token={token} />
</RequestsProvider>
</ApolloProvider>
)
}
return (
<View style={styles.App}>
<ActivityIndicator size="large" color="#D0D0D0" />
</View>
)
}
export default App