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

"Not Authorized to access on type Subscription" by custom subscription #10150

Closed diguini closed 2 years ago

diguini commented 2 years ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Authentication, GraphQL API

Amplify Categories

auth, api

Environment information

``` # Put output below this line System: OS: Windows 10 10.0.19043 CPU: (20) x64 Intel(R) Core(TM) i9-10850K CPU @ 3.60GHz Memory: 14.12 GB / 31.90 GB Binaries: Node: 16.4.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.17 - C:\Program Files\nodejs\yarn.CMD npm: 7.18.1 - C:\Program Files\nodejs\npm.CMD Browsers: Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.71) Internet Explorer: 11.0.19041.1566 npmPackages: @babel/core: ^7.12.9 => 7.16.5 (7.18.5) @babel/runtime: ^7.12.5 => 7.16.5 @react-native-async-storage/async-storage: ^1.15.5 => 1.15.14 @react-native-community/datetimepicker: ^6.1.3 => 6.1.3 @react-native-community/eslint-config: ^2.0.0 => 2.0.0 @react-native-community/netinfo: ^6.0.4 => 6.2.1 @react-native-community/slider: ^3.0.3 => 3.0.3 @react-native-masked-view/masked-view: ^0.2.6 => 0.2.6 @react-navigation/bottom-tabs: ^6.0.9 => 6.0.9 @react-navigation/native: ^6.0.6 => 6.0.6 @react-navigation/stack: ^6.0.11 => 6.0.11 HelloWorld: 0.0.1 amazon-cognito-identity-js: ^5.2.2 => 5.2.3 aws-amplify: ^4.2.5 => 4.3.11 aws-amplify-react-native: ^5.0.5 => 5.0.5 babel-jest: ^26.6.3 => 26.6.3 eslint: 7.28.0 => 7.28.0 eslint-config-prettier: ^8.3.0 => 8.3.0 (6.15.0) expo: ~43.0.0 => 43.0.4 expo-barcode-scanner: ^11.3.0 => 11.3.0 expo-file-system: ~13.0.3 => 13.0.3 hermes-inspector-msggen: 1.0.0 jest: ^26.6.3 => 26.6.3 metro-react-native-babel-preset: ^0.66.2 => 0.66.2 (0.59.0, 0.64.0) prettier: ^2.5.1 => 2.5.1 react: 17.0.2 => 17.0.2 react-native: 0.65.1 => 0.65.1 react-native-calendars: ^1.1285.0 => 1.1285.0 react-native-codegen: 0.0.8 => 0.0.8 react-native-floating-action: ^1.21.0 => 1.22.0 react-native-gesture-handler: ^2.1.0 => 2.1.0 react-native-image-picker: ^4.7.3 => 4.8.3 react-native-masked-text: ^1.13.0 => 1.13.0 react-native-modal-selector: ^2.1.1 => 2.1.1 react-native-onesignal: ^4.3.11 => 4.3.11 react-native-qrcode-svg: ^6.1.1 => undefined (6.1.2, ) react-native-reanimated: ^2.8.0 => 2.8.0 react-native-safe-area-context: ^4.2.5 => 4.2.5 react-native-screens: ^3.10.1 => 3.10.1 react-native-svg: ^12.1.1 => 12.1.1 react-native-text-ticker: ^1.13.0 => 1.14.0 react-native-webview: ^11.17.2 => 11.21.1 react-native-youtube-iframe: ^2.1.2 => 2.2.1 react-test-renderer: 17.0.2 => 17.0.2 npmGlobalPackages: @aws-amplify/cli: 8.1.0 expo-cli: 4.12.12 localtunnel: 2.0.2 npm: 7.18.1 pm2: 5.2.0 yarn: 1.22.17 ```

Describe the bug

Trying to subscribe to a custom subscription throws the error : Not Authorized to access onStudentNotificationByStudentId on type Subscription, it's curiously because i able to subscribe to other models before trying this one. I'm logged with auth by cognito_user_pool as well.

Expected behavior

Should be able to subscribe without error.

Reproduction steps

Call a custom subscription with a logged user.

Code Snippet

My schema.graphql:

type StudentNotification
  @model
  @auth(
    rules: [
      { allow: owner }
      { allow: private, operations: [read, delete] }
      { allow: custom, operations: [create, read] }
    ]
  ) {
  id: ID!
  text: String!
  studentId: ID @index(name: "byStudent")
  teacherId: ID @index(name: "byTeacher")
  action: NotificationAction
  studioLessonId: ID @index(name: "byStudioLesson")
  teacherLessonId: ID @index(name: "byTeacherLesson")
  studioId: ID @index(name: "byStudio")
  createdAt: AWSDateTime
  studioLesson: StudioLesson @belongsTo(fields: ["studioLessonId"])
  teacherLesson: TeacherLesson @belongsTo(fields: ["teacherLessonId"])
  studio: Studio @belongsTo(fields: ["studioId"])
  teacher: Teacher @belongsTo(fields: ["teacherId"])
}

type Subscription {
  onStudentNotificationByStudentId(studentId: ID!): StudentNotification
    @aws_subscribe(mutations: ["createStudentNotification"])
}

My subscription query:

export const onStudentNotificationByStudentId = /* GraphQL */ `
  subscription OnStudentNotificationByStudentId($studentId: ID!) {
    onStudentNotificationByStudentId(studentId: $studentId) {
      text
      studentId
    }
  }
`;

And my code:

useFocusEffect(
    React.useCallback(() => {
        const subscription = API.graphql({
          query: onStudentNotificationByStudentId,
          variables: {
            studentId: studentId,
          },
        }).subscribe({
          next: ({provider, value}) => {
            console.log(value);
          },
          error: error => {
            console.error(error.error.errors[0]);
          },
        });

        return () => {
          subscription.unsubscribe();
        };
    }, []),
  );

### Log output

<details>

// Put your logs below this line



</details>

### 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_
diguini commented 2 years ago

I resolved by this solution here: https://github.com/aws-amplify/amplify-cli/issues/2715#issuecomment-553550734 changing my schema.graphql to:

type Subscription {
  onStudentNotificationByStudentId(studentId: ID!): StudentNotification
    @aws_subscribe(mutations: ["createStudentNotification"])
    @aws_cognito_user_pools
}