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

iOS App Using AWS Amplify DataStore Does Not Receive Subscriptions for Deletes Made via AWS Lambda #3759

Closed
rohit3d2003 opened this issue Jun 20, 2024 · 10 comments
Labels
datastore Issues related to the DataStore category question General question

Comments

@rohit3d2003
Copy link

rohit3d2003 commented Jun 20, 2024

Describe the bug

I'm using an AWS Amplify generated GraphQL API in my iOS application, which is configured to operate in offline mode using the DataStore approach. Additionally, I have several AWS Lambda functions that directly call AppSync queries and mutations to make changes to the data.

Problem:

When an object is deleted via an AWS Lambda function, the iOS app does not receive any subscription notifications, and the DataStore does not get updated with these changes. Specifically:

  1. Deleting an object via Lambda does not trigger a subscription update in the iOS app.
  2. Amplify verbose logging in Xcode does not show any errors related to this issue.
  3. Restarting the app does not help; the app still shows the deleted records in the UI, which are supposed to be filtered out.

I am not sure if the auth rule on the model plays any part in it. So the record was created by owner but deleted by Admin from Lambda. Owner who is logged in to the iOS app not receiving these delete subscriptions/notifications.

Screenshot 2024-06-20 at 8 05 11 AM

Steps To Reproduce

1. Set up an AWS Amplify project with a GraphQL API and DataStore enabled in an iOS app.
2. Create a few AWS Lambda functions that perform AppSync queries and mutations.
3. Use one of the Lambda functions to delete an object in the database.
5. Observe that the iOS app does not receive any subscription updates about the deletion.
6. Verify that the DataStore does not reflect the deletion, even after restarting the app.

Expected behavior

When an object is deleted via an AWS Lambda function, the iOS app should:

  1. Receive a subscription notification about the deletion.
  2. Update the local DataStore to reflect the deletion.
  3. Update the UI to filter out and remove the deleted records.

Amplify Framework Version

2.35.3

Amplify Categories

DataStore

Dependency manager

Swift PM

Swift version

5.0

CLI version

12.12.2

Xcode version

15.4

Relevant log output

No response

Is this a regression?

Yes

Regression additional context

No response

Platforms

iOS

OS Version

17.5

Device

iPhone 15

Specific to simulators

No response

Additional context

No response

@harsh62 harsh62 added datastore Issues related to the DataStore category question General question labels Jun 20, 2024
@harsh62
Copy link
Member

harsh62 commented Jun 20, 2024

Thank you for raising the issue with us. We look into it and provide an update.

@rohit3d2003
Copy link
Author

I did more testing and seems like Subscriptions works for create operation but update and delete aren't working.

@harsh62
Copy link
Member

harsh62 commented Jun 21, 2024

Thanks for providing that information too. To help us debug further, would you be able to provide any of the following?

  1. The logs you see in the console when the error happens. You can enable verbose logging to the console by doing this before calling Amplify.configure:
Amplify.Logging.logLevel = .verbose
  1. The contents of your amplify configuration file with sensitive information removed.

@5d
Copy link
Member

5d commented Jun 21, 2024

Hi @rohit3d2003

Have you checked whether the records are updated or deleted in DynamoDB after the admin Lambda function was invoked?
Have you set up conflict resolution in AWS AppSync?

@rohit3d2003
Copy link
Author

rohit3d2003 commented Jun 21, 2024

Hi @rohit3d2003

Have you checked whether the records are updated or deleted in DynamoDB after the admin Lambda function was invoked? Have you set up conflict resolution in AWS AppSync?

Yes, the records did get updated/deleted in dynamodb and they do show up if I restart the app. Conflict resolution has been set to Auto

@rohit3d2003
Copy link
Author

rohit3d2003 commented Jun 21, 2024

Thanks for providing that information too. To help us debug further, would you be able to provide any of the following?

  1. The logs you see in the console when the error happens. You can enable verbose logging to the console by doing this before calling Amplify.configure:
Amplify.Logging.logLevel = .verbose
  1. The contents of your amplify configuration file with sensitive information removed.

I am not seeing any activity in XCode console during updates/deleted with verbose logging. Only creates do show some activity in the console.

Amplify Config

Screenshot 2024-06-21 at 10 53 49 AM
Screenshot 2024-06-21 at 10 56 43 AM

@rohit3d2003
Copy link
Author

To provide more context, in lambda function, I am signing in using Admin credentials and using the JWT token while calling the app sync apis so that from App Sync perspective, an Admin is making the change

@5d
Copy link
Member

5d commented Jun 21, 2024

Hi @rohit3d2003 ,

How do you compose your GraphQL mutations to AppSync?

Please keep in mind that the selection set of the GraphQL mutation should align with the GraphQL selection set of the subscription used by Amplify DataStore. This is crucial because there might be filters applied to the subscription to help Amplify filter real-time events from the AppSync real-time data server. Additionally, the subscription could include metadata fields that are necessary for Amplify DataStore to accurately parse the subscription event.

Based on the GraphQL schema you provided, I suspect that the Amplify DataStore subscription includes a filter on the owner field. It might look something like this:

subscription OnCreateBlog($owner: String!) {
  onCreateBlog(owner: $owner) {
    id
    createdAt
    updatedAt
    __typename
    _version
    _deleted
    _lastChangedAt
    owner
  }
}


variables: {"owner":"test-user"}

To ensure that Amplify successfully filters data events from the AppSync real-time data server, it is crucial to align the selection set in your GraphQL request with that of the Amplify DataStore subscription. Therefore, it better to be:

mutation CreateBlog($input: CreateBlogInput!) {
  createBlog(input: $input) {
    id
    createdAt
    updatedAt
    __typename
    _version
    _deleted
    _lastChangedAt
    owner    
  }
}

variables: {
  "input" : {
    "id" : "12DDF815-22B1-4B2F-AE77-14D736C8F98E"
  }
}

@rohit3d2003
Copy link
Author

Thanks @5d! That was it, I was missing owner field in update and delete mutation and hence probably subscriptions were not received nor any error was showing up in console. This is fixed now

Copy link

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datastore Issues related to the DataStore category question General question
Projects
None yet
Development

No branches or pull requests

3 participants