amplify-android
amplify-android copied to clipboard
Allow custom queryField in Amplify DataStore or API
It will be extremely useful to be able to allow Amplify take in custom query Field. This way one can tailor the query to get the required data in the right filter. At the moment all the sorting is done on the Android side which could be very time consuming.
type HealthCentreConsultant @model @searchable
@key(fields:["consultantID", "longitude", "latitude", "id"])
@key(name: "byConsultantByLongitudeByLatitude", fields: ["consultantID", "longitude", "latitude"], queryField: "consultantByLongitudeByLatitude")
@key(name: "byHealthCentreByConsultant", fields:["healthCentreID", "consultantID"], queryField: "healthCentreByConsultant")
@key(name: "byConsultantBySearch", fields: ["consultantID", "search"], queryField: "consultantBySearch")
@key(name: "byConsultantByLongitudeByLatitudeBySearch", fields: ["consultantID", "longitude", "latitude", "search"], queryField: "consultantByLongitudeByLatitudeBySearch")
Hi @KayIlory, the API category does support a custom query field. Please see the Fetching Data section of the API (GraphQL) docs.
For example, here's how to query for Todo
models whose name
field contains the string"first"
:
Amplify.API.query(
ModelQuery.list(Todo::class.java, Todo.NAME.contains("first")),
{ response ->
for (todo in response.data) {
Log.i("MyAmplifyApp", todo.name)
}
},
{ error -> Log.e("MyAmplifyApp", "Query failure", error) }
)
In the example, Todo.NAME.contains("first")
is called a "query predicate".
There is some additional information around forming query predicates in the DataStore Guide. (Both API and DataStore use query predicates.)
@jamesonwilliams - the above doesn't return the data if one uses a different sort key with the custom queryField
.
For example, if I wanted this sort
@key(name: "byConsultantBySearch", fields: ["consultantID", "name"], queryField: "consultantBySearch")
I wont be able to get it with the solution you mention above.
The different keys allows for different sorts.
I could not find anywhere that one could utilise different keys with different sorts when querying the data from Amplify. @jamesonwilliams keen on any direction with regards to that
The release of GraphQL Transformer v2 and custom primary key support as well as support for sort keys should enable your use case. Please let us know if you have any additional questions.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.