Amplify-ios: Using `@auth` directive with `{ allow: owner }` prevents DataStore from operating correctly

Created on 24 Apr 2020  路  7Comments  路  Source: aws-amplify/amplify-ios

Describe the bug

Using @auth directive with { allow: owner } prevents DataStore from operating correctly -- does not pass owner to GraphQL calls resulting in errors shown in the log.

To Reproduce
Set up an Amplify iOS project with GraphQL API and DataStore
Simple model with no @auth directive
User Pools for authentication and authorization, nothing else
DataStore API works as expected, the data shows up in the DataStore DynamoDB table and the target model table.
_BUT_ add an @auth directive with rules: [{ allow: owner }] The websocket logging shows that the calls to the API are invalid and DataStore is not providing theowner` as an argument to the "On" subscription hand

Related to #340 ?

Expected behavior
DataStore should honour the @auth directive and correctly pass the owner in calls to GraphQL.

Screenshots

Model

type Receipt @model
    @auth(
        rules: [ {allow: owner}]
    )
{
    id: ID!
    state: String
    isFavorite: Boolean
    scanError: String
    created: AWSTimestamp
    extracted: AWSTimestamp
    updated: AWSTimestamp
    viewed: AWSTimestamp
    emailDocument: String
    receiptImage: String
    receiptRawText: String
    dateTime: AWSDateTime!
    merchant: String
    amount: Float!
    taxTotal: Float
    subTotalAmount: Float
    tipAmount: Float
    total: String!
}

Results in subscription API

subscription OnCreateReceipt($owner: String!) {
  onCreateReceipt(owner: $owner) {
    id
    state
    isFavorite
    scanError
    created
    extracted
    updated
    viewed
    emailDocument
    receiptImage
    receiptRawText
    dateTime
    merchant
    amount
    taxTotal
    subTotalAmount
    tipAmount
    total
    _version
    _deleted
    _lastChangedAt
    owner
  }
}

DataStore call:

            Amplify.DataStore.save(
                Receipt(state: "UPLOADED", isFavorite: false, created: 2343242423423, receiptImage: "test at 6:06", dateTime: Date(), merchant: "Starbucks", amount: 0.0, taxTotal: 0.0, subTotalAmount: 0.0, tipAmount: 0.0, total: "0.0")
            ) {
                switch $0 {
                case .success:
                    print("Added receipt")
                case .failure(let error):
                    print("Error adding receipt - \(error.localizedDescription)")
                }
            }

Xcode log:

2020-04-23 18:07:18.713409-0400 amplify-sample[93930:1528442] WebsocketDidReceiveMessage - {"id":"6EC731FA-1830-4ABA-9970-9C68EF9A57D7","type":"error","payload":{"errors":[{"message":"Validation error of type MissingFieldArgument: Missing field argument owner @ 'onUpdateReceipt'"}]}}
2020-04-23 18:07:18.713787-0400 amplify-sample[93930:1528442] The operation couldn鈥檛 be completed. (AppSyncRealTimeClient.ConnectionProviderError error 2.)
2020-04-23 18:07:18.714147-0400 amplify-sample[93930:1528425] [IncomingAsyncSubscriptionEventToAnyModelMapper] The operation couldn鈥檛 be completed. (Amplify.DataStoreError error 0.)
2020-04-23 18:07:18.714390-0400 amplify-sample[93930:1528425] [AWSModelReconciliationQueue] receiveCompletion: error: DataStoreError: subscription item event failed with error
Caused by:
APIError: subscription item event failed with error
Caused by:
subscription("6EC731FA-1830-4ABA-9970-9C68EF9A57D7", Optional(["errors": AppSyncRealTimeClient.AppSyncJSONValue.array([AppSyncRealTimeClient.AppSyncJSONValue.object(["message": AppSyncRealTimeClient.AppSyncJSONValue.string("Validation error of type MissingFieldArgument: Missing field argument owner @ \'onUpdateReceipt\'")])])]))

But before that ...

2020-04-23 18:07:10.838238-0400 amplify-sample[93930:1528428] WebsocketDidReceiveMessage - {"id":"19761720-7EC7-4482-82B7-2573F62A36DF","type":"error","payload":{"errors":[{"message":"Validation error of type MissingFieldArgument: Missing field argument owner @ 'onUpdateReceipt'"}]}}

Numerous similar errors for all Receipt calls.

Environment(please complete the following information):

CLI version 4.18.0

PODS:
  - Amplify (0.11.0)
  - AmplifyPlugins/AWSAPIPlugin (0.11.0):
    - AppSyncRealTimeClient (~> 1.1.0)
    - AWSPluginsCore (= 0.11.0)
    - ReachabilitySwift (~> 5.0.0)
  - AmplifyPlugins/AWSDataStorePlugin (0.11.0):
    - AWSPluginsCore (= 0.11.0)
    - SQLite.swift (~> 0.12.0)
  - AppSyncRealTimeClient (1.1.6):
    - Starscream (= 3.0.6)
  - AWSAppSync (3.1.2):
    - AppSyncRealTimeClient (~> 1.1)
    - AWSCore (~> 2.13.0)
    - ReachabilitySwift (~> 5.0.0)
    - SQLite.swift (~> 0.12.2)
  - AWSAuthCore (2.13.2):
    - AWSCore (= 2.13.2)
  - AWSCognitoIdentityProvider (2.13.2):
    - AWSCognitoIdentityProviderASF (= 1.0.1)
    - AWSCore (= 2.13.2)
  - AWSCognitoIdentityProviderASF (1.0.1)
  - AWSCore (2.13.2)
  - AWSMobileClient (2.13.2):
    - AWSAuthCore (= 2.13.2)
    - AWSCognitoIdentityProvider (= 2.13.2)
  - AWSPluginsCore (0.11.0):
    - Amplify (= 0.11.0)
    - AWSMobileClient (~> 2.13.0)
  - ReachabilitySwift (5.0.0)
  - SQLite.swift (0.12.2):
    - SQLite.swift/standard (= 0.12.2)
  - SQLite.swift/standard (0.12.2)
  - Starscream (3.0.6)

Device Information (please complete the following information):

  • Device: iPhone11 simulator but also happens on up to date iPhone XR
closing soon question

All 7 comments

Hi @Tom-RulesCube ,

Thanks for reaching out, and your interest in the Amplify Datastore project! I checked with the team and unfortunately, the @auth directive is not currently supported, so this is currently considered a feature enhancement.

I see the amplify-ios repo has a lot of @auth action going on in the last few days ... will we be expecting that in a release soon?

Hi @Tom-RulesCube, here's a working sample app with the schema you provided https://github.com/lawmicha/ReceiptModel

The reason the subscriptions fail is due to an unauthorized error. The sync portion of DataStore will retry a few times before failing to create the subscriptions completely. However, the moment you sign in and trigger a DataStore operation, the sync engine will start again and the subscriptions will be successful.

Thanks that's great. I'll take a look to verify. With the latest version, our @auth directives are now working well. I'm going to try a mixed cognito pools/api key mode soon. Let's see how that goes.

When can we expect to get @auth implemented with DataStore?

When can we expect to get @auth implemented with DataStore?

There's support for @auth with owner-based rules since v1.0.0. Are you having problems with it?

Thanks that's great. I'll take a look to verify. With the latest version, our @auth directives are now working well. I'm going to try a mixed cognito pools/api key mode soon. Let's see how that goes.

Good to know it worked for you @Tom-RulesCube. Let us know if you run into any other issue otherwise I'll close this one soon.

Was this page helpful?
0 / 5 - 0 ratings