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?
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.
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 useSimpleGraphQLRequest, and provide your own documentString. For yourgetCityListmethod, it would look something like this: