@@ -22,7 +22,12 @@ import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil
22
22
import com.amplifyframework.api.aws.GsonVariablesSerializer
23
23
import com.amplifyframework.api.graphql.SimpleGraphQLRequest
24
24
import com.amplifyframework.api.graphql.SubscriptionType
25
+ import com.amplifyframework.api.graphql.GraphQLOperation
26
+ import com.amplifyframework.api.graphql.GraphQLResponse
25
27
import com.amplifyframework.core.Amplify
28
+ import com.amplifyframework.core.Consumer
29
+ import com.amplifyframework.core.Action
30
+ import com.amplifyframework.api.ApiException
26
31
import io.flutter.plugin.common.MethodChannel
27
32
28
33
class FlutterGraphQLApi {
@@ -32,11 +37,13 @@ class FlutterGraphQLApi {
32
37
33
38
@JvmStatic
34
39
fun query (flutterResult : MethodChannel .Result , request : Map <String , Any >) {
40
+ var apiName: String?
35
41
var document: String
36
42
var variables: Map <String , Any >
37
43
var cancelToken: String
38
44
39
45
try {
46
+ apiName = FlutterApiRequest .getApiName(request)
40
47
document = FlutterApiRequest .getGraphQLDocument(request)
41
48
variables = FlutterApiRequest .getVariables(request)
42
49
cancelToken = FlutterApiRequest .getCancelToken(request)
@@ -47,45 +54,69 @@ class FlutterGraphQLApi {
47
54
}
48
55
return
49
56
}
50
- var operation = Amplify .API .query(
57
+
58
+ var operation: GraphQLOperation <String >? = null
59
+
60
+ var responseCallback = Consumer <GraphQLResponse <String >> { response ->
61
+ if (! cancelToken.isNullOrEmpty()) OperationsManager .removeOperation(cancelToken)
62
+
63
+ var result: Map <String , Any > = mapOf (
64
+ " data" to response.data,
65
+ " errors" to response.errors.map { it.message }
66
+ )
67
+ LOG .debug(" GraphQL query operation succeeded with response: $result " )
68
+ handler.post { flutterResult.success(result) }
69
+ }
70
+
71
+ var errorCallback = Consumer <ApiException > { exception ->
72
+ if (! cancelToken.isNullOrEmpty()) OperationsManager .removeOperation(cancelToken)
73
+
74
+ LOG .error(" GraphQL mutate operation failed" , exception)
75
+ handler.post {
76
+ ExceptionUtil .postExceptionToFlutterChannel(flutterResult, " ApiException" ,
77
+ ExceptionUtil .createSerializedError(exception))
78
+ }
79
+ }
80
+
81
+ if (apiName != null ) {
82
+ operation = Amplify .API .query(
83
+ apiName,
51
84
SimpleGraphQLRequest <String >(
52
85
document,
53
86
variables,
54
87
String ::class .java,
55
88
GsonVariablesSerializer ()
56
89
),
57
- { response ->
58
- if (! cancelToken.isNullOrEmpty()) OperationsManager .removeOperation(cancelToken)
59
-
60
- var result: Map <String , Any > = mapOf (
61
- " data" to response.data,
62
- " errors" to response.errors.map { it.message }
63
- )
64
- LOG .debug(" GraphQL query operation succeeded with response: $result " )
65
- handler.post { flutterResult.success(result) }
66
- },
67
- { exception ->
68
- if (! cancelToken.isNullOrEmpty()) OperationsManager .removeOperation(cancelToken)
69
-
70
- LOG .error(" GraphQL query operation failed" , exception)
71
- handler.post {
72
- ExceptionUtil .postExceptionToFlutterChannel(flutterResult, " ApiException" ,
73
- ExceptionUtil .createSerializedError(exception))
74
- }
75
- }
76
- )
90
+ responseCallback,
91
+ errorCallback
92
+ )
93
+ } else {
94
+ operation = Amplify .API .query(
95
+ SimpleGraphQLRequest <String >(
96
+ document,
97
+ variables,
98
+ String ::class .java,
99
+ GsonVariablesSerializer ()
100
+ ),
101
+ responseCallback,
102
+ errorCallback
103
+ )
104
+ }
105
+
77
106
if (operation != null ) {
78
107
OperationsManager .addOperation(cancelToken, operation)
79
108
}
80
109
}
81
110
82
111
@JvmStatic
83
112
fun mutate (flutterResult : MethodChannel .Result , request : Map <String , Any >) {
113
+ var apiName: String?
84
114
var document: String
85
115
var variables: Map <String , Any >
86
116
var cancelToken: String
87
117
88
118
try {
119
+ apiName = FlutterApiRequest .getApiName(request)
89
120
document = FlutterApiRequest .getGraphQLDocument(request)
90
121
variables = FlutterApiRequest .getVariables(request)
91
122
cancelToken = FlutterApiRequest .getCancelToken(request)
@@ -96,46 +127,70 @@ class FlutterGraphQLApi {
96
127
}
97
128
return
98
129
}
99
- var operation = Amplify .API .mutate(
130
+ var operation: GraphQLOperation <String ?>? = null
131
+
132
+ var responseCallback = Consumer <GraphQLResponse <String >> { response ->
133
+ if (! cancelToken.isNullOrEmpty()) OperationsManager .removeOperation(cancelToken)
134
+
135
+ var result: Map <String , Any > = mapOf (
136
+ " data" to response.data,
137
+ " errors" to response.errors.map { it.message }
138
+ )
139
+ LOG .debug(" GraphQL mutate operation succeeded with response : $result " )
140
+ handler.post { flutterResult.success(result) }
141
+ }
142
+
143
+ var errorCallback = Consumer <ApiException > { exception ->
144
+ if (! cancelToken.isNullOrEmpty()) OperationsManager .removeOperation(cancelToken)
145
+
146
+ LOG .error(" GraphQL mutate operation failed" , exception)
147
+ handler.post {
148
+ ExceptionUtil .postExceptionToFlutterChannel(flutterResult, " ApiException" ,
149
+ ExceptionUtil .createSerializedError(exception))
150
+ }
151
+ }
152
+
153
+ if (apiName != null ) {
154
+ operation = Amplify .API .mutate(
155
+ apiName,
100
156
SimpleGraphQLRequest <String >(
101
157
document,
102
158
variables,
103
159
String ::class .java,
104
160
GsonVariablesSerializer ()
105
161
),
106
- { response ->
107
- if (! cancelToken.isNullOrEmpty()) OperationsManager .removeOperation(cancelToken)
108
-
109
- var result: Map <String , Any > = mapOf (
110
- " data" to response.data,
111
- " errors" to response.errors.map { it.message }
112
- )
113
- LOG .debug(" GraphQL mutate operation succeeded with response : $result " )
114
- handler.post { flutterResult.success(result) }
115
- },
116
- { exception ->
117
- if (! cancelToken.isNullOrEmpty()) OperationsManager .removeOperation(cancelToken)
118
-
119
- LOG .error(" GraphQL mutate operation failed" , exception)
120
- handler.post {
121
- ExceptionUtil .postExceptionToFlutterChannel(flutterResult, " ApiException" ,
122
- ExceptionUtil .createSerializedError(exception))
123
- }
124
- }
125
- )
162
+ responseCallback,
163
+ errorCallback
164
+ )
165
+ } else {
166
+ operation = Amplify .API .mutate(
167
+ SimpleGraphQLRequest <String >(
168
+ document,
169
+ variables,
170
+ String ::class .java,
171
+ GsonVariablesSerializer ()
172
+ ),
173
+ responseCallback,
174
+ errorCallback
175
+ )
176
+ }
177
+
178
+
126
179
if (operation != null ) {
127
180
OperationsManager .addOperation(cancelToken, operation)
128
181
}
129
182
}
130
183
131
184
@JvmStatic
132
185
fun subscribe (flutterResult : MethodChannel .Result , request : Map <String , Any >, graphqlSubscriptionStreamHandler : GraphQLSubscriptionStreamHandler ) {
186
+ var apiName: String?
133
187
var document: String
134
188
var variables: Map <String , Any >
135
189
var id: String
136
190
var established = false
137
191
138
192
try {
193
+ apiName = FlutterApiRequest .getApiName(request)
139
194
document = FlutterApiRequest .getGraphQLDocument(request)
140
195
variables = FlutterApiRequest .getVariables(request)
141
196
id = FlutterApiRequest .getCancelToken(request)
@@ -146,43 +201,70 @@ class FlutterGraphQLApi {
146
201
}
147
202
return
148
203
}
149
- var operation = Amplify .API .subscribe(
204
+ var operation: GraphQLOperation <String ?>? = null
205
+
206
+ var conectionCallback = Consumer <String > {
207
+ established = true
208
+ LOG .debug(" Subscription established: $id " )
209
+ handler.post { flutterResult.success(null ) }
210
+ }
211
+
212
+ var responseCallback = Consumer <GraphQLResponse <String >> { response ->
213
+ val payload: Map <String , Any > = mapOf (
214
+ " data" to response.data,
215
+ " errors" to response.errors.map { it.message }
216
+ )
217
+ LOG .debug(" GraphQL subscription event received: $payload " )
218
+ graphqlSubscriptionStreamHandler.sendEvent(payload, id, GraphQLSubscriptionEventTypes .DATA )
219
+ }
220
+
221
+ var errorCallback = Consumer <ApiException > {
222
+ if (! id.isNullOrEmpty()) OperationsManager .removeOperation(id)
223
+ if (established) {
224
+ graphqlSubscriptionStreamHandler.sendError(" ApiException" , ExceptionUtil .createSerializedError(it))
225
+ } else {
226
+ handler.post {
227
+ ExceptionUtil .postExceptionToFlutterChannel(flutterResult, " ApiException" ,
228
+ ExceptionUtil .createSerializedError(it))
229
+ }
230
+ }
231
+ }
232
+
233
+ var disconnectionCallback = Action {
234
+ if (! id.isNullOrEmpty()) OperationsManager .removeOperation(id)
235
+ LOG .debug(" Subscription has been closed successfully" )
236
+ graphqlSubscriptionStreamHandler.sendEvent(null , id, GraphQLSubscriptionEventTypes .DONE )
237
+ }
238
+
239
+
240
+ if (apiName != null ){
241
+ operation = Amplify .API .subscribe(
242
+ apiName,
150
243
SimpleGraphQLRequest <String >(
151
244
document,
152
245
variables,
153
246
String ::class .java,
154
247
GsonVariablesSerializer ()
155
248
),
156
- {
157
- established = true
158
- LOG .debug(" Subscription established: $id " )
159
- handler.post { flutterResult.success(null ) }
160
- },
161
- {response ->
162
- val payload: Map <String , Any > = mapOf (
163
- " data" to response.data,
164
- " errors" to response.errors.map { it.message }
165
- )
166
- LOG .debug(" GraphQL subscription event received: $payload " )
167
- graphqlSubscriptionStreamHandler.sendEvent(payload, id, GraphQLSubscriptionEventTypes .DATA )
168
- },
169
- {
170
- if (! id.isNullOrEmpty()) OperationsManager .removeOperation(id)
171
- if (established) {
172
- graphqlSubscriptionStreamHandler.sendError(" ApiException" , ExceptionUtil .createSerializedError(it))
173
- } else {
174
- handler.post {
175
- ExceptionUtil .postExceptionToFlutterChannel(flutterResult, " ApiException" ,
176
- ExceptionUtil .createSerializedError(it))
177
- }
178
- }
179
- },
180
- {
181
- if (! id.isNullOrEmpty()) OperationsManager .removeOperation(id)
182
- LOG .debug(" Subscription has been closed successfully" )
183
- graphqlSubscriptionStreamHandler.sendEvent(null , id, GraphQLSubscriptionEventTypes .DONE )
184
- }
185
- )
249
+ conectionCallback,
250
+ responseCallback,
251
+ errorCallback,
252
+ disconnectionCallback
253
+ )
254
+ } else {
255
+ operation = Amplify .API .subscribe(
256
+ SimpleGraphQLRequest <String >(
257
+ document,
258
+ variables,
259
+ String ::class .java,
260
+ GsonVariablesSerializer ()
261
+ ),
262
+ conectionCallback,
263
+ responseCallback,
264
+ errorCallback,
265
+ disconnectionCallback
266
+ )
267
+ }
186
268
if (operation != null ) {
187
269
OperationsManager .addOperation(id, operation)
188
270
}
0 commit comments