aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

v6 upgrade causing errors on subscription, mutations #12621

Closed OperationalFallacy closed 10 months ago

OperationalFallacy commented 10 months ago

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

Authentication

Amplify Categories

auth

Environment information

``` # Put output below this line System: OS: macOS 13.6.1 CPU: (8) arm64 Apple M2 Memory: 51.16 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.17.1 - ~/.n/bin/node Yarn: 1.22.19 - ~/.n/bin/yarn npm: 9.6.7 - ~/.n/bin/npm Browsers: Brave Browser: 116.1.57.47 Chrome: 118.0.5993.117 Safari: 17.1 npmPackages: @aws-amplify/api-graphql: ^4.0.0 => 4.0.4 @aws-amplify/api-graphql/internals: undefined () @aws-amplify/api-graphql/internals/server: undefined () @aws-amplify/cli: 12.8.2 => 12.8.2 @aws-sdk/client-secrets-manager: ^3.418.0 => 3.429.0 @aws-sdk/client-sso: ^3.418.0 => 3.429.0 @aws-sdk/credential-provider-sso: ^3.418.0 => 3.429.0 @smithy/protocol-http: ^3.0.5 => 3.0.7 @smithy/signature-v4: ^2.0.9 => 2.0.11 plus some npmGlobalPackages: npm: 10.2.4 yarn: 1.22.21 ```

Describe the bug

Getting unauthorized for the models that were working ok with v5

For example

type User @model @auth(rules: [
    { allow: owner, operations: [read, update], identityClaim: "email::sub" },
  ]) {
  userEmail: ID! @primaryKey
  name: String!
  xxx: [xxx] @hasMany(indexName: "userxxxByxxx", fields: ["userEmail"] )
  owner: String @auth(rules: [{ allow: owner, operations: [read, delete], identityClaim: "email::sub"  }])
  openRequests: Int @auth(rules: [{ allow: owner, operations: [read], identityClaim: "email::sub"  }])
}
{
    "recoverySuggestion": "Ensure app code is up to date, auth directives exist and are correct on each model, and that server-side data has not been invalidated by a schema change. If the problem persists, search for or create an issue: https://github.com/aws-amplify/amplify-js/issues",
    "localModel": null,
    "message": "Connection failed: {\"errors\":[{\"errorType\":\"Unauthorized\",\"message\":\"Not Authorized to access onUpdateUser on type Subscription\"}]}",
    "model": "User",
    "operation": "Update",
    "errorType": "Unauthorized",
    "process": "subscribe",
    "remoteModel": null,
    "cause": {
        "errors": [
            {
                "message": "Connection failed: {\"errors\":[{\"errorType\":\"Unauthorized\",\"message\":\"Not Authorized to access onUpdateUser on type Subscription\"}]}"
            }
        ]
    }
}

Expected behavior

Given the custom mutation with Lambda works, as well as refreshing data (refresh page) - the subscription should be working, too

Reproduction steps

upgrade amplify to v6...

Code Snippet


      <Flex justifyContent="space-between" maxWidth="42rem" padding="1rem" width="100%">
        <xxxCardCollection
          overrideItems={({ item, index }) => {
            return xxxComponent({
              item,
              handleXxxSubmission,
              generatingXxx: state.generatingXxx,
            });
          }}
        />
      </Flex>

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

cwomack commented 10 months ago

@OperationalFallacy, thank you for opening this issue. Did you get a chance to see the v6 Migration Guide (specifically the "API (GraphQL) section)"? The syntax will be difference for any subscriptions and mutations, but if you can share the frontend code that is calling the GraphQL API's that would help too!

OperationalFallacy commented 10 months ago

I had only one mutation via lambda (below), I've updated it - thanks for pointing out.

Still getting these errors. I believe they are from the code for collections

      const response= await client.graphql({
        query: requestxxx,
        variables: {
          input: {
            xxx: xxx,
          }
        }
      })
      console.log('response', response.data.requestxxx)
      // const response: xxxResponse | null = await xxx({
      //   xxx: xxx,
      // });

There is bunch of errors for all models I believe, which is strange because I use only a few in collection - not all. Why is it even complaining about subscription to every model?

chrisbonifacio commented 10 months ago

Hi @OperationalFallacy can you provide some more relevant code snippets and share the output errors?

I had only one mutation via lambda (below)

Can you explain this a bit more? Are you calling client.graphql inside of a Lambda function?

Lastly, I'm curious if you have DataStore enabled. Do you see that there are errors for create, update, and delete subscriptions on all models? If DataStore is enabled, that might explain those errors and we can work from there.

OperationalFallacy commented 10 months ago

Hey @chrisbonifacio, please see that linked ticket. The author mentioned how to patch this bug in the amplify lib. It fixed the problem described here (collection component with datastore enabled). I'm closing this issue in favor of #12590