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

DataStore query from a "public" table returns always an empty list #5649

Open
6 of 14 tasks
JannicMeetYu opened this issue Nov 5, 2024 · 4 comments
Open
6 of 14 tasks
Assignees
Labels
GraphQL API Issues related to the API (GraphQL) Category question A question about the Amplify Flutter libraries

Comments

@JannicMeetYu
Copy link

JannicMeetYu commented Nov 5, 2024

Description

For our project, we are using AWS Amplify DataStore and we created two models which are included below. We added data to the Mood table in Launch Studio and made it public with reading rights for every user. So they query this table and get the data displayed. The user can then select Moods to create MoodRecordings which are getting saved. That's the idea/goal.

The problem is that if I query/fetch the Moods table, I'm always getting an empty list back.
Here is the code example:
Future<List<Mood>> fetchAllMoods() async { try { final moods = await Amplify.DataStore.query(Mood.classType); print('Fetched moods from DataStore: $moods'); return moods; } on DataStoreException catch (e) { throw DataStoreException('Failed to fetch moods: ${e.message}'); } }

The print statement returns: "Fetched moods from DataStore: []"

We have other models in our schema, where all the CRUD operations are working just fine. It's just that case.
In Launch Studio -> Content, I can select the Mood table and I see that the data is there. If I use the "Test GraphQL API" next to it and query the table
query MyQuery { listMoods { items { id moodName moodDescription isPositiveMood } } }
Im getting the expected data from the table, so this works.

I also tried printing out some events to get some insights:
Amplify.Hub.listen(HubChannel.DataStore, (hubEvent) { if (hubEvent.eventName == "modelSynced") { final syncEvent = hubEvent.payload as ModelSyncedEvent; if (syncEvent.modelName == "Mood") { print('Mood model synced. Record count: ${syncEvent.added}'); } } else if (hubEvent.eventName == "syncQueriesFailed") { print('Sync failed: ${hubEvent.payload}'); } else { print('DataStore event: ${hubEvent.eventName}'); } });

Mood model synced. Record count is always 0. Even after DataStore.clear() and .start().

Are there any rights missing? For me, it seems that everything is set up how it needs to be. I just don't get why it is always querying an empty list.

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Notifications (Push)
  • Storage

Steps to Reproduce

  1. Create a table in Launch Studio and add public reading rights.
  2. Add data respective data to this table.
  3. Query the table in the codebase by using Amplify.DataStore.query(...)
  4. Check if any data gets fetched.

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Flutter Version

3.24.4

Amplify Flutter Version

2.3.0

Deployment Method

Amplify CLI (Gen 1)

Schema

type Mood @model @auth(rules: [{allow: public, operations: [read]}]) {
  id: ID!
  moodName: String!
  moodDescription: String
  MoodRecordings: [MoodRecording] @hasMany(indexName: "byMood", fields: ["id"])
  isPositiveMood: Boolean!
}

type MoodRecording @model @auth(rules: [{ allow: owner, ownerField: "ownerID", operations: [create, update, delete, read] }]) {
  id: ID!
  date: AWSDateTime!
  moodID: ID! @index(name: "byMood")
  mood: Mood! @belongsTo(fields: ["moodID"])
  ownerID: ID @index(name: "byOwnerID", queryField: "getMoodRecordingsByOwnerID")
}
@github-actions github-actions bot added pending-triage This issue is in the backlog of issues to triage pending-maintainer-response Pending response from a maintainer of this repository labels Nov 5, 2024
@tyllark
Copy link
Member

tyllark commented Nov 6, 2024

Hello @JannicMeetYu I'm sorry you are experiencing this issue. I will attempt to reproduce the issue and get back to you when I have an update.

@github-actions github-actions bot removed the pending-maintainer-response Pending response from a maintainer of this repository label Nov 6, 2024
@Equartey Equartey added the GraphQL API Issues related to the API (GraphQL) Category label Nov 12, 2024
@Equartey
Copy link
Contributor

Hi @JannicMeetYu, I suspect the issue you're seeing is due the permissions you've set on Mood. The Studio UI doesn't have additional rights granted to it to enable creating a model in table when only read rights are specified on in the GraphQL schema.

You can verify this by inspecting the network tab in the web dev tools and observing the GraphQL network event created when attempting to create a model. I suspect that the request is failing because of a permissions error and the UI doesn't show this error leading to the confusion.

@Equartey Equartey added question A question about the Amplify Flutter libraries and removed pending-triage This issue is in the backlog of issues to triage labels Nov 13, 2024
@JannicMeetYu
Copy link
Author

Thanks for your message. We have set these rules manually. So should we update the rights of the Mood Table by "create"?
Currently, I cannot verify if the request is failing because there is no active web version.
print('Mood model synced. Record count: ${syncEvent.added}'); I guess this does somehow the same thing and it always returns a 0. So those moods are not created.

@github-actions github-actions bot added the pending-maintainer-response Pending response from a maintainer of this repository label Nov 14, 2024
@Equartey Equartey self-assigned this Nov 14, 2024
@Equartey
Copy link
Contributor

Hi @JannicMeetYu, Studio uses private auth rule when making a request. To get your models to sync via the Studio UI add the private rule, then restrict it as necessary (eg. read, create, etc):

type Mood @model @auth(rules: [{allow: public, operations: [read]},  { allow: private }]) {
  id: ID!
  moodName: String!
  moodDescription: String
  MoodRecordings: [MoodRecording] @hasMany(indexName: "byMood", fields: ["id"])
  isPositiveMood: Boolean!
}

@github-actions github-actions bot removed the pending-maintainer-response Pending response from a maintainer of this repository label Nov 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GraphQL API Issues related to the API (GraphQL) Category question A question about the Amplify Flutter libraries
Projects
None yet
Development

No branches or pull requests

3 participants