-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Connect.js
165 lines (137 loc) · 3.95 KB
/
Connect.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import regeneratorRuntime from 'regenerator-runtime/runtime';
import { Component } from 'react';
import API from '@aws-amplify/api';
export default class Connect extends Component {
constructor(props) {
super(props);
this.state = this.getInitialState();
this.subSubscription = null;
}
getInitialState() {
const { query } = this.props;
return {
loading: query && !!query.query,
data: {},
errors: [],
mutation: () => console.warn('Not implemented'),
};
}
getDefaultState() {
return {
loading: false,
data: {},
errors: [],
mutation: () => console.warn('Not implemented'),
};
}
async _fetchData() {
this._unsubscribe();
this.setState({ loading: true });
const {
query: { query, variables = {} } = {},
mutation: { query: mutation, mutationVariables = {} } = {},
subscription,
onSubscriptionMsg = prevData => prevData,
} = this.props;
let { data, mutation: mutationProp, errors } = this.getDefaultState();
if (
!API ||
typeof API.graphql !== 'function' ||
typeof API.getGraphqlOperationType !== 'function'
) {
throw new Error(
'No API module found, please ensure @aws-amplify/api is imported'
);
}
const hasValidQuery =
query && API.getGraphqlOperationType(query) === 'query';
const hasValidMutation =
mutation && API.getGraphqlOperationType(mutation) === 'mutation';
const hasValidSubscription =
subscription &&
API.getGraphqlOperationType(subscription.query) === 'subscription';
if (!hasValidQuery && !hasValidMutation && !hasValidSubscription) {
console.warn('No query, mutation or subscription was specified');
}
if (hasValidQuery) {
try {
data = null;
const response = await API.graphql({ query, variables });
data = response.data;
} catch (err) {
data = err.data;
errors = err.errors;
}
}
if (hasValidMutation) {
mutationProp = async variables => {
const result = await API.graphql({ query: mutation, variables });
this.forceUpdate();
return result;
};
}
if (hasValidSubscription) {
const { query: subsQuery, variables: subsVars } = subscription;
try {
const observable = API.graphql({
query: subsQuery,
variables: subsVars,
});
this.subSubscription = observable.subscribe({
next: ({ value: { data } }) => {
const { data: prevData } = this.state;
const newData = onSubscriptionMsg(prevData, data);
if (this.mounted) {
this.setState({ data: newData });
}
},
error: err => console.error(err),
});
} catch (err) {
errors = err.errors;
}
}
this.setState({ data, errors, mutation: mutationProp, loading: false });
}
_unsubscribe() {
if (this.subSubscription) {
this.subSubscription.unsubscribe();
}
}
async componentDidMount() {
this._fetchData();
this.mounted = true;
}
componentWillUnmount() {
this._unsubscribe();
this.mounted = false;
}
componentDidUpdate(prevProps) {
const { loading } = this.state;
const { query: newQueryObj, mutation: newMutationObj } = this.props;
const { query: prevQueryObj, mutation: prevMutationObj } = prevProps;
// query
const { query: newQuery, variables: newQueryVariables } = newQueryObj || {};
const { query: prevQuery, variables: prevQueryVariables } =
prevQueryObj || {};
const queryChanged =
prevQuery !== newQuery ||
JSON.stringify(prevQueryVariables) !== JSON.stringify(newQueryVariables);
// mutation
const { query: newMutation, variables: newMutationVariables } =
newMutationObj || {};
const { query: prevMutation, variables: prevMutationVariables } =
prevMutationObj || {};
const mutationChanged =
prevMutation !== newMutation ||
JSON.stringify(prevMutationVariables) !==
JSON.stringify(newMutationVariables);
if (!loading && (queryChanged || mutationChanged)) {
this._fetchData();
}
}
render() {
const { data, loading, mutation, errors } = this.state;
return this.props.children({ data, errors, loading, mutation }) || null;
}
}