Closed zamarwells closed 1 year ago
I've not yet worked with the premade requests ModelQueries
but a common error/issue is expecting DynamoDB/AppSync to work as a relational database when querying.
In this case, you are filtering the results of a list request, something you should have in mind is that the filters are applied after the resolver has fetched the first n
items from DynamoDB (being n
the limit applied to the number of items to compare, usually 100).
It works in the following order:
n
items are fetched from DynamoDBIf none of the fetched items match your filter, you'll receive an empty list as a response with a non-null nextToken
. You should re-send the same query with the received nextToken
value until nextToken == null
or you've received the desired number of items.
Hope this helps.
try {
final request = ModelQueries.list(IndividualRecipee.classType,
where: IndividualRecipee.MEALTEMPLATEIDNUMBER
.eq(activemealTemplateID));
final response = await Amplify.API.query(request: request).response;
List<IndividualRecipee?>? todos = response.data?.items;
if (todos == null) {
print('errors: ' + response.errors.toString());
return;
}
print('Query result: ' + todos.toString());
} on ApiException catch (e) {
print('Query failed: $e');
}
I'm sorry, above comment did not help me at all, take a look at the query above, even after removing two query conditions, it still does not return the right number of results.
Hi @zamarwells,
I get the same issue when using Swift on iOS.
If I use the Datastore it works fine, but querying using the API with a custom query doesn't return the results shown in the Amplify Studio Content section.
Even if I go to AppSync's Query section and run the GraphQL query, it returns the wrong results.
It would be great if we could get to the bottom of this issue soonest!
There seems to be a difference in how the Datastore queries the data compared to querying the API directly.
@zamarwells maybe this part in the docs will help. As mentioned in my previous comment, you need to fetch all the results not just the first page, maybe that is the issue you are facing.
@spbarber did you check in the AppSync's Query section if the returned nextToken
is set to null
? Otherwise, it could be the issue that I previously mentioned and you would need to fetch all paginated results.
Hi @Lorenzohidalgo
Ah ha, if I query with the limit of 10000 then I get all the results I see in the Amplify Studio portal and on local storage.
query MyQuery { listInboxs(filter: {propertyId: {eq: 186}}, limit: 10000) { items { id } } }
That is strange, a better way to do that?
I've not yet worked with the premade requests
ModelQueries
but a common error/issue is expecting DynamoDB/AppSync to work as a relational database when querying.In this case, you are filtering the results of a list request, something you should have in mind is that the filters are applied after the resolver has fetched the first
n
items from DynamoDB (beingn
the limit applied to the number of items to compare, usually 100).It works in the following order:
n
items are fetched from DynamoDB- The filter is applied to the fetched items
- You'll get the items that match your filter
If none of the fetched items match your filter, you'll receive an empty list as a response with a non-null
nextToken
. You should re-send the same query with the receivednextToken
value untilnextToken == null
or you've received the desired number of items.Hope this helps.
Hey @spbarber, as per my first comment, the limit applies to the number of items that will be scanned in DynamoDB, not the number of items to be returned.
A DynamoDB Scan is also a fairly expensive operation, take a look at the following post. I would recommend you to create GSIs to avoid that.
For example, if you create a GSI on propertyId
you will be able to query by the GSI and not need to scan the whole table.
Another options to kind of force DynamoDB to work as a relational DB would be to use the @searchable
directive, take a look at the docs for more info.
The differences you might be seeing between querying DataStore
and AppSync
might be caused because DataStore
transforms the data into a local SQLlite DB
(or at least that was the case the last time I worked with DataStore
)
Thank you @Lorenzohidalgo for all the incredible resources and insights.
@zamarwells @spbarber - do you feel like you have enough info to mitigate the issue your seeing with the list query result set? If you feel like the experience of DataStore is correct, then the pagination docs should help you replicate that and/or increasing the search limit (noting the considerations above).
If you feel like neither is giving the results you want, you may also wish to explore custom indexes, as Lorenzo mentioned, or custom resolvers which allow for attaching arbitrary logic to your GraphQL API.
Indexes helped me out in my use-case, it will also save you from looping over unnecessary number of rows on the server. https://docs.amplify.aws/cli/graphql/data-modeling/#configure-a-secondary-index I tried with a single index and it returns up to 100 items based on the specified filter in the above example it is accountRepresentativeID.
I don't know if it will work when filtering by multiple fields.
Hey Folks - I am going to close this out since I think the original issue/question was resolved. If you are facing an further issues, please open a new issue.
Description
I am accessing my database via API and I have noticed that when I query the database from my flutter app, I don't get all the desired query results. At first I thought this issue was because datastore was not syncing, I checkded the admin console, when I navigate to the content area, I can actually see all the desired results. I have also tried to access data through datastore and it comes out fine. I only have issue when I decide to use API.
P.S, I opened an issue on this sometime ago but I have been away from this project for a while and I don't have access to the GitHub account anymore, hence I had to open a new issue.
Categories
Steps to Reproduce
below is the code I am using to query the database
Screenshots
No response
Platforms
Android Device/Emulator API Level
API 31
Environment
Dependencies
Device
Iphone 12
OS
IOS 15.5
CLI Version
8.4.0
Additional Context
No response