Amplify-android: Usage question for AppSync to Amplify SDK migration

Created on 16 Nov 2020  路  2Comments  路  Source: aws-amplify/amplify-android

My team has been using the AWS AppSync SDK to perform our queries, subscriptions and mutations in our Android application. We're looking at what's required to switch to this SDK.

We have several schema's that include their own custom queries, e.g.

type Query {
    getCityList(version: String!): CityList
}

How can we use the Amplify.API.query() method to execute a custom GraphQL query for a schema that does not use the @model directive?

API Usage Question

Most helpful comment

Hi @ekmwilson,

To make a custom query using Amplify, take a look at the GraphQL API Advanced Workflows documentation, which describes some similar use cases. Essentially, you can use SimpleGraphQLRequest, and provide your own document String. For your getCityList method, it would look something like this:

fun getCityList() {
    var version = "123"
    val document = """
        query getCityList(\$version: String!) {
          getCityList(version: \$version) { 
            items {
              id
              name
            }
            nextToken
          }
        }"""
    var request = SimpleGraphQLRequest<PaginatedResult<City>>(
            document,
            mapOf("version" to version),
            TypeMaker.getParameterizedType(PaginatedResult::class.java, City::class.java),
            GsonVariablesSerializer()
    )
    Amplify.API.query(request, 
            { Log.d(TAG, "Request succeeded:" + it)}, 
            { Log.e(TAG, "An error occurred:", it)})
}

All 2 comments

Hi @ekmwilson,

To make a custom query using Amplify, take a look at the GraphQL API Advanced Workflows documentation, which describes some similar use cases. Essentially, you can use SimpleGraphQLRequest, and provide your own document String. For your getCityList method, it would look something like this:

fun getCityList() {
    var version = "123"
    val document = """
        query getCityList(\$version: String!) {
          getCityList(version: \$version) { 
            items {
              id
              name
            }
            nextToken
          }
        }"""
    var request = SimpleGraphQLRequest<PaginatedResult<City>>(
            document,
            mapOf("version" to version),
            TypeMaker.getParameterizedType(PaginatedResult::class.java, City::class.java),
            GsonVariablesSerializer()
    )
    Amplify.API.query(request, 
            { Log.d(TAG, "Request succeeded:" + it)}, 
            { Log.e(TAG, "An error occurred:", it)})
}

Thanks for the info @richardmcclellan , that's what I was looking for.

Was this page helpful?
0 / 5 - 0 ratings