Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(amplify_api): add support for apiName to GraphQL requests #553

Merged
merged 27 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
633987f
chore: add multi auth mode support
jodafm May 3, 2021
901b6e9
Merge branch 'master' into issue_547
jodafm May 11, 2021
0605ce9
fix: use the callbacka in the mutation
jodafm May 19, 2021
9d89a1a
fix: add awsCredentialsProvider in AWSApiPlugin
jodafm May 19, 2021
1d76476
Merge branch 'issue_547' of github.com:jodafm/amplify-flutter into is…
jodafm May 19, 2021
e01ba52
fix - removed a modified file from pull request
jodafm May 28, 2021
970b2dc
fix: change requests related to the issue
jodafm May 28, 2021
8a40e7f
fix: add unit tests related to issue #547
jodafm May 28, 2021
93b6764
fix: android unit tests
jodafm May 28, 2021
8bc6a24
fix: android unit tests
jodafm May 28, 2021
a0343cf
fix: underlyingInvalidApiException message
jodafm May 29, 2021
0907518
Merge branch 'master' into issue_547
jodafm May 30, 2021
179a8d2
fix: remove unnecessary spaces
jodafm Jun 2, 2021
164e85e
fix: improve Dart tests
jodafm Jun 2, 2021
debbb41
fix: invalid access to a map
jodafm Jun 2, 2021
5cee2d8
fix: typo
jodafm Jun 2, 2021
225fe49
fix: add methodCallArguments
jodafm Jun 2, 2021
bdf56f5
fix: expect
jodafm Jun 2, 2021
832845d
fix: extract argument correctly
jodafm Jun 2, 2021
668d043
fix: extract argument correctly
jodafm Jun 2, 2021
816e1fd
fix: add return in the mock method call
jodafm Jun 3, 2021
1e06eee
fix: back to basics
jodafm Jun 3, 2021
e9247b0
fix: remove spaces
jodafm Jun 11, 2021
49a2489
Merge branch 'main' into issue_547
jodafm Jul 29, 2021
a34d858
fix format
jodafm Jul 30, 2021
7194d75
Merge branch 'main' into issue_547
jodafm Jul 30, 2021
bf01b46
Merge branch 'main' into issue_547
Aug 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,23 @@ class FlutterApiRequest {
}
}

@JvmStatic
fun getApiName(request: Map<String, Any>) : String? {
jodafm marked this conversation as resolved.
Show resolved Hide resolved
if (request[API_NAME_KEY] != null) {
try {
return request[API_NAME_KEY] as String
} catch (cause: Exception) {
throw AmplifyException(
"The apiName request argument was not passed as a String",
cause,
"The request should include the apiName as a String")
}
}

return null;

}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil
import com.amplifyframework.api.aws.GsonVariablesSerializer
import com.amplifyframework.api.graphql.SimpleGraphQLRequest
import com.amplifyframework.api.graphql.SubscriptionType
import com.amplifyframework.api.graphql.GraphQLOperation
import com.amplifyframework.api.graphql.GraphQLResponse
import com.amplifyframework.core.Amplify
import com.amplifyframework.core.Consumer
import com.amplifyframework.core.Action
import com.amplifyframework.api.ApiException
import io.flutter.plugin.common.MethodChannel

class FlutterGraphQLApi {
Expand All @@ -32,11 +37,13 @@ class FlutterGraphQLApi {

@JvmStatic
fun query(flutterResult: MethodChannel.Result, request: Map<String, Any>) {
var apiName: String?
var document: String
var variables: Map<String, Any>
var cancelToken: String

try {
apiName = FlutterApiRequest.getApiName(request)
document = FlutterApiRequest.getGraphQLDocument(request)
variables = FlutterApiRequest.getVariables(request)
cancelToken = FlutterApiRequest.getCancelToken(request)
Expand All @@ -47,45 +54,69 @@ class FlutterGraphQLApi {
}
return
}
var operation = Amplify.API.query(

var operation: GraphQLOperation<String>? = null

var responseCallback = Consumer<GraphQLResponse<String>> { response ->
if (!cancelToken.isNullOrEmpty()) OperationsManager.removeOperation(cancelToken)

var result: Map<String, Any> = mapOf(
"data" to response.data,
"errors" to response.errors.map { it.message }
)
LOG.debug("GraphQL query operation succeeded with response: $result")
handler.post { flutterResult.success(result) }
}

var errorCallback = Consumer<ApiException> { exception ->
if (!cancelToken.isNullOrEmpty()) OperationsManager.removeOperation(cancelToken)

LOG.error("GraphQL mutate operation failed", exception)
handler.post {
ExceptionUtil.postExceptionToFlutterChannel(flutterResult, "ApiException",
ExceptionUtil.createSerializedError(exception))
}
}

if (apiName != null) {
operation = Amplify.API.query(
apiName,
SimpleGraphQLRequest<String>(
document,
variables,
String::class.java,
GsonVariablesSerializer()
),
{ response ->
if (!cancelToken.isNullOrEmpty()) OperationsManager.removeOperation(cancelToken)

var result: Map<String, Any> = mapOf(
"data" to response.data,
"errors" to response.errors.map { it.message }
)
LOG.debug("GraphQL query operation succeeded with response: $result")
handler.post { flutterResult.success(result) }
},
{ exception ->
if (!cancelToken.isNullOrEmpty()) OperationsManager.removeOperation(cancelToken)

LOG.error("GraphQL query operation failed", exception)
handler.post {
ExceptionUtil.postExceptionToFlutterChannel(flutterResult, "ApiException",
ExceptionUtil.createSerializedError(exception))
}
}
)
responseCallback,
errorCallback
)
} else {
operation = Amplify.API.query(
SimpleGraphQLRequest<String>(
document,
variables,
String::class.java,
GsonVariablesSerializer()
),
responseCallback,
errorCallback
)
}

if (operation != null) {
OperationsManager.addOperation(cancelToken, operation)
}
}

@JvmStatic
fun mutate(flutterResult: MethodChannel.Result, request: Map<String, Any>) {
var apiName: String?
var document: String
var variables: Map<String, Any>
var cancelToken: String

try {
apiName = FlutterApiRequest.getApiName(request)
document = FlutterApiRequest.getGraphQLDocument(request)
variables = FlutterApiRequest.getVariables(request)
cancelToken = FlutterApiRequest.getCancelToken(request)
Expand All @@ -96,46 +127,70 @@ class FlutterGraphQLApi {
}
return
}
var operation = Amplify.API.mutate(
var operation: GraphQLOperation<String?>? = null

var responseCallback = Consumer<GraphQLResponse<String>> { response ->
if (!cancelToken.isNullOrEmpty()) OperationsManager.removeOperation(cancelToken)

var result: Map<String, Any> = mapOf(
"data" to response.data,
"errors" to response.errors.map { it.message }
)
LOG.debug("GraphQL mutate operation succeeded with response : $result")
handler.post { flutterResult.success(result) }
}

var errorCallback = Consumer<ApiException> { exception ->
if (!cancelToken.isNullOrEmpty()) OperationsManager.removeOperation(cancelToken)

LOG.error("GraphQL mutate operation failed", exception)
handler.post {
ExceptionUtil.postExceptionToFlutterChannel(flutterResult, "ApiException",
ExceptionUtil.createSerializedError(exception))
}
}

if (apiName != null ) {
operation = Amplify.API.mutate(
apiName,
SimpleGraphQLRequest<String>(
document,
variables,
String::class.java,
GsonVariablesSerializer()
),
{ response ->
if (!cancelToken.isNullOrEmpty()) OperationsManager.removeOperation(cancelToken)

var result: Map<String, Any> = mapOf(
"data" to response.data,
"errors" to response.errors.map { it.message }
)
LOG.debug("GraphQL mutate operation succeeded with response : $result")
handler.post { flutterResult.success(result) }
},
{ exception ->
if (!cancelToken.isNullOrEmpty()) OperationsManager.removeOperation(cancelToken)

LOG.error("GraphQL mutate operation failed", exception)
handler.post {
ExceptionUtil.postExceptionToFlutterChannel(flutterResult, "ApiException",
ExceptionUtil.createSerializedError(exception))
}
}
)
responseCallback,
errorCallback
)
} else {
operation = Amplify.API.mutate(
SimpleGraphQLRequest<String>(
document,
variables,
String::class.java,
GsonVariablesSerializer()
),
responseCallback,
errorCallback
)
}


if (operation != null) {
OperationsManager.addOperation(cancelToken, operation)
}
}

@JvmStatic
fun subscribe(flutterResult: MethodChannel.Result, request: Map<String, Any>, graphqlSubscriptionStreamHandler: GraphQLSubscriptionStreamHandler) {
var apiName: String?
var document: String
var variables: Map<String, Any>
var id: String
var established = false

try {
apiName = FlutterApiRequest.getApiName(request)
document = FlutterApiRequest.getGraphQLDocument(request)
variables = FlutterApiRequest.getVariables(request)
id = FlutterApiRequest.getCancelToken(request)
Expand All @@ -146,43 +201,70 @@ class FlutterGraphQLApi {
}
return
}
var operation = Amplify.API.subscribe(
var operation: GraphQLOperation<String?>? = null

var conectionCallback = Consumer<String> {
established = true
LOG.debug("Subscription established: $id")
handler.post { flutterResult.success(null) }
}

var responseCallback = Consumer<GraphQLResponse<String>> { response ->
val payload: Map<String, Any> = mapOf(
"data" to response.data,
"errors" to response.errors.map { it.message }
)
LOG.debug("GraphQL subscription event received: $payload")
graphqlSubscriptionStreamHandler.sendEvent(payload, id, GraphQLSubscriptionEventTypes.DATA)
}

var errorCallback = Consumer<ApiException> {
if (!id.isNullOrEmpty()) OperationsManager.removeOperation(id)
if (established) {
graphqlSubscriptionStreamHandler.sendError("ApiException", ExceptionUtil.createSerializedError(it))
} else {
handler.post {
ExceptionUtil.postExceptionToFlutterChannel(flutterResult, "ApiException",
ExceptionUtil.createSerializedError(it))
}
}
}

var disconnectionCallback = Action {
if (!id.isNullOrEmpty()) OperationsManager.removeOperation(id)
LOG.debug("Subscription has been closed successfully")
graphqlSubscriptionStreamHandler.sendEvent(null, id, GraphQLSubscriptionEventTypes.DONE)
}


if (apiName != null){
operation = Amplify.API.subscribe(
apiName,
SimpleGraphQLRequest<String>(
document,
variables,
String::class.java,
GsonVariablesSerializer()
),
{
established = true
LOG.debug("Subscription established: $id")
handler.post { flutterResult.success(null) }
},
{response ->
val payload: Map<String, Any> = mapOf(
"data" to response.data,
"errors" to response.errors.map { it.message }
)
LOG.debug("GraphQL subscription event received: $payload")
graphqlSubscriptionStreamHandler.sendEvent(payload, id, GraphQLSubscriptionEventTypes.DATA)
},
{
if (!id.isNullOrEmpty()) OperationsManager.removeOperation(id)
if (established) {
graphqlSubscriptionStreamHandler.sendError("ApiException", ExceptionUtil.createSerializedError(it))
} else {
handler.post {
ExceptionUtil.postExceptionToFlutterChannel(flutterResult, "ApiException",
ExceptionUtil.createSerializedError(it))
}
}
},
{
if (!id.isNullOrEmpty()) OperationsManager.removeOperation(id)
LOG.debug("Subscription has been closed successfully")
graphqlSubscriptionStreamHandler.sendEvent(null, id, GraphQLSubscriptionEventTypes.DONE)
}
)
conectionCallback,
responseCallback,
errorCallback,
disconnectionCallback
)
} else {
operation = Amplify.API.subscribe(
SimpleGraphQLRequest<String>(
document,
variables,
String::class.java,
GsonVariablesSerializer()
),
conectionCallback,
responseCallback,
errorCallback,
disconnectionCallback
)
}
if (operation != null) {
OperationsManager.addOperation(id, operation)
}
Expand Down
Loading