Skip to content

Commit e95be4d

Browse files
authored
chore(amplify_api): add support for apiName to GraphQL requests (#553)
1 parent ef25029 commit e95be4d

File tree

8 files changed

+579
-78
lines changed

8 files changed

+579
-78
lines changed

packages/amplify_api/android/src/main/kotlin/com/amazonaws/amplify/amplify_api/FlutterApiRequest.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,23 @@ class FlutterApiRequest {
115115
}
116116
}
117117

118+
@JvmStatic
119+
fun getApiName(request: Map<String, Any>) : String? {
120+
if (request[API_NAME_KEY] != null) {
121+
try {
122+
return request[API_NAME_KEY] as String
123+
} catch (cause: Exception) {
124+
throw AmplifyException(
125+
"The apiName request argument was not passed as a String",
126+
cause,
127+
"The request should include the apiName as a String")
128+
}
129+
}
130+
131+
return null;
132+
133+
}
134+
135+
118136
}
119137
}

packages/amplify_api/android/src/main/kotlin/com/amazonaws/amplify/amplify_api/FlutterGraphQLApi.kt

Lines changed: 155 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil
2222
import com.amplifyframework.api.aws.GsonVariablesSerializer
2323
import com.amplifyframework.api.graphql.SimpleGraphQLRequest
2424
import com.amplifyframework.api.graphql.SubscriptionType
25+
import com.amplifyframework.api.graphql.GraphQLOperation
26+
import com.amplifyframework.api.graphql.GraphQLResponse
2527
import com.amplifyframework.core.Amplify
28+
import com.amplifyframework.core.Consumer
29+
import com.amplifyframework.core.Action
30+
import com.amplifyframework.api.ApiException
2631
import io.flutter.plugin.common.MethodChannel
2732

2833
class FlutterGraphQLApi {
@@ -32,11 +37,13 @@ class FlutterGraphQLApi {
3237

3338
@JvmStatic
3439
fun query(flutterResult: MethodChannel.Result, request: Map<String, Any>) {
40+
var apiName: String?
3541
var document: String
3642
var variables: Map<String, Any>
3743
var cancelToken: String
3844

3945
try {
46+
apiName = FlutterApiRequest.getApiName(request)
4047
document = FlutterApiRequest.getGraphQLDocument(request)
4148
variables = FlutterApiRequest.getVariables(request)
4249
cancelToken = FlutterApiRequest.getCancelToken(request)
@@ -47,45 +54,69 @@ class FlutterGraphQLApi {
4754
}
4855
return
4956
}
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,
5184
SimpleGraphQLRequest<String>(
5285
document,
5386
variables,
5487
String::class.java,
5588
GsonVariablesSerializer()
5689
),
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+
77106
if (operation != null) {
78107
OperationsManager.addOperation(cancelToken, operation)
79108
}
80109
}
81110

82111
@JvmStatic
83112
fun mutate(flutterResult: MethodChannel.Result, request: Map<String, Any>) {
113+
var apiName: String?
84114
var document: String
85115
var variables: Map<String, Any>
86116
var cancelToken: String
87117

88118
try {
119+
apiName = FlutterApiRequest.getApiName(request)
89120
document = FlutterApiRequest.getGraphQLDocument(request)
90121
variables = FlutterApiRequest.getVariables(request)
91122
cancelToken = FlutterApiRequest.getCancelToken(request)
@@ -96,46 +127,70 @@ class FlutterGraphQLApi {
96127
}
97128
return
98129
}
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,
100156
SimpleGraphQLRequest<String>(
101157
document,
102158
variables,
103159
String::class.java,
104160
GsonVariablesSerializer()
105161
),
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+
126179
if (operation != null) {
127180
OperationsManager.addOperation(cancelToken, operation)
128181
}
129182
}
130183

131184
@JvmStatic
132185
fun subscribe(flutterResult: MethodChannel.Result, request: Map<String, Any>, graphqlSubscriptionStreamHandler: GraphQLSubscriptionStreamHandler) {
186+
var apiName: String?
133187
var document: String
134188
var variables: Map<String, Any>
135189
var id: String
136190
var established = false
137191

138192
try {
193+
apiName = FlutterApiRequest.getApiName(request)
139194
document = FlutterApiRequest.getGraphQLDocument(request)
140195
variables = FlutterApiRequest.getVariables(request)
141196
id = FlutterApiRequest.getCancelToken(request)
@@ -146,43 +201,70 @@ class FlutterGraphQLApi {
146201
}
147202
return
148203
}
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,
150243
SimpleGraphQLRequest<String>(
151244
document,
152245
variables,
153246
String::class.java,
154247
GsonVariablesSerializer()
155248
),
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+
}
186268
if (operation != null) {
187269
OperationsManager.addOperation(id, operation)
188270
}

0 commit comments

Comments
 (0)