-
Notifications
You must be signed in to change notification settings - Fork 2
/
App.js
127 lines (107 loc) · 3.26 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
124
125
126
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import Main from './App/Containers/Main';
import { split } from 'apollo-link';
import { ApolloProvider } from 'react-apollo';
import { setContext } from 'apollo-link-context';
import ApolloClient from 'apollo-client';
import { InMemoryCache } from 'apollo-cache-inmemory';
import gql from 'graphql-tag';
import ActionCableAdaper from './App/Services/actioncable-adapter';
import ActionCableLink from 'graphql-ruby-client/subscriptions/ActionCableLink';
import { onError } from 'apollo-link-error';
import { createStackNavigator } from 'react-navigation';
import UploadFile from './App/Containers/UploadFile';
import Home from './App/Containers/Home';
import { createUploadLink } from 'apollo-upload-client'
const token = 'your token';
const cable = ActionCableAdaper.createConsumer('wss://xxxxx/cable?');
const httpLink = new createUploadLink({
uri: 'https://xxxx/graphql'
});
const authLink = setContext(async (req, { headers }) => {
// const token = await AsyncStorage.getItem('token');
// header 中带上 token 信息,及设定的 APP DeviceUuid
return {
...headers,
headers: {
Authorization: `your token`
}
};
});
const errorLink = onError(({ networkError, graphQLErrors }) => {
if (graphQLErrors) {
graphQLErrors.map(({ message, locations, path, code }) => {
console.tron.log(
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`
)
// 假定 401 为 token 失效 code
if (code === 401) {
// 跳转至登录页面
// const index = R.path(['nav', 'index'])(store.getState());
// const routeName = R.path(['nav', 'routes', index, 'routeName'])(store.getState());
// routeName !== 'Login' && routeName !== 'LoginForm' && store.dispatch(logoutNavigationAction);
}
// Toast.show(`请求错误:${message}`)
})
};
if (networkError) {
console.tron.log(`[Network error]: ${networkError}`);
// Toast.show('与服务器断开连接,请检查您的网络状态')
};
});
const concatLink = authLink.concat(errorLink.concat(httpLink));
const hasSubscriptionOperation = ({ query: { definitions } }) => {
return definitions.some(
({ kind, operation }) => kind === 'OperationDefinition' && operation === 'subscription'
);
};
const link = split(
// split based on operation type
hasSubscriptionOperation,
new ActionCableLink({ cable }),
concatLink,
);
const client = new ApolloClient({
link,
cache: new InMemoryCache()
// defaultOptions
});
var subs2 = gql`
subscription {
ping
}
`;
type Props = {};
const RootStack = createStackNavigator(
{
Home: Home,
UploadFile: UploadFile,
},
{
initialRouteName: 'Home',
}
);
export default class App extends Component<Props> {
componentDidMount() {
const queryObservable = client.subscribe({
query: subs2,
});
console.tron.log(queryObservable.subscribe);
queryObservable.subscribe({
next: data => console.tron.log(data),
error: error => console.tron.log(error)
});
}
render() {
return (
<ApolloProvider client={client}>
<RootStack />
</ApolloProvider>
);
}
}