【问题标题】:AWS Amplify AppSync Subscription not working correctlyAWS Amplify AppSync 订阅无法正常工作
【发布时间】:2020-03-05 07:27:19
【问题描述】:

我编写了一个使用 AWS Amplify CLI / AppSync 订阅数据库更改的小型应用程序。所有放大 api 调用都可以正常工作(突变、查询),但不幸的是观察者没有收到事件。我可以看到 MQTT 套接字定期接收二进制文件,但我无法获取更改的对象。

我配置了 Amplify 以供放大使用。我可以在调试器中看到 AppSyncProvider 已被初始化。也尝试过 API 和 PubSub,但没有任何区别。

        const awsmobile = {
            "aws_appsync_graphqlEndpoint": "https://[...].appsync-api.[...]/graphql",
            "aws_appsync_region": "[...]",
            "aws_appsync_authenticationType": "AMAZON_COGNITO_USER_POOLS",
        };

        Amplify.configure(awsmobile);

        ngOnInit() 
            {
                try {

                  this.apiService.OnUpdateA.subscribe(
                    {
                        next: (x) => {[...]},
                        error: (e) => {[...]},
                        complete: () => {[...]}
                    });
                } 
                catch (error) {[...]    }
            }

        ***Schema***
        type A
        @model 
        @auth(rules: [
              {allow: owner},
              {allow: groups, groups: ["A"], operations: [create, update, read]},
              {allow: groups, groups: ["B"], operations: [read]},
          ]) 
        {
          id: ID!
          entry: AnotherType! @connection(name: "AnotherConnection")
    [...]
        }

 OnUpdateAListener: Observable<
    OnUpdateASubscription
  > = API.graphql(
    graphqlOperation(
      `subscription OnUpdateA($owner: String) {
        onUpdateA(owner: $owner) {
          __typename
          id
          owner
       [...]
        }
      }`
    )
  ) as Observable<OnUpdateASubscription>;

有人有什么想法吗?

**Logs:**
{mqttConnections: Array(1), newSubscriptions: {…}, provider: Symbol(INTERNAL_AWS_APPSYNC_PUBSUB_PROVIDER)}
mqttConnections: Array(1)
0: {url: "wss://[...]-ats.iot.[...].amazonaws…[...]%3D%3D", topics: Array(2), client: "[...]"}
length: 1
__proto__: Array(0)
newSubscriptions:
onUpdate:
expireTime: 1573313050000
topic: "[....]/22tmaezjv5555h4o7yreu24f7u/onUpdate/1cd033bad555ba55555a20690d3e04e901145776d3b8d8ac95a0aea447b273c3"
__proto__: Object
__proto__: Object
provider: Symbol(INTERNAL_AWS_APPSYNC_PUBSUB_PROVIDER)
__proto__: Object

但是,不确定订阅对象没有队列是否可疑?

Subscription {_observer: {…}, _queue: undefined, _state: "ready", _cleanup: ƒ}
_cleanup: ƒ ()
_observer:
next: (x) => {…}
__proto__: Object
_queue: ***undefined***
_state: "ready"
closed: (...)
__proto__: Object

非常感谢。

【问题讨论】:

    标签: ionic-framework aws-amplify aws-appsync


    【解决方案1】:

    这是一个 AWS Amplify 订阅的工作示例:

    
    import Amplify from 'aws-amplify';
    import API from '@aws-amplify/api';
    import PubSub from '@aws-amplify/pubsub';
    import awsconfig from './aws-exports';
    
    Amplify.configure(awsconfig);
    API.configure(awsconfig);
    PubSub.configure(awsconfig);
    
    // put above in root
    
    // below is example 
    
    import { API, graphqlOperation } from 'aws-amplify';
    
    var  onAddNote = `subscription OnCreateNote {
        onCreateNote {
            id
            patient {
                id
                organization {
                    id
                }
            }   
        }
    }
    `;
    
    listenForNoteAdd() {
            return API.graphql(graphqlOperation(onAddNote) ).subscribe({next: (noteData) => {
                console.log("new note so reload consider reload")
                let note = noteData.value.data.onCreateNote
                console.log(JSON.stringify(note))
    
                // now that you have indication of something happening 
                // do what you must next
    
            }})    
        }
    
    

    【讨论】:

    • 感谢您的快速回复。显然,它与我的配置没有区别。您的 awsconfig 中有什么?
    • const awsmobile = { aws_project_region": "", "aws_cognito_identity_pool_id": "", "aws_cognito_region": "", "aws_user_pools_id": "", "aws_user_pools_web_client_id": "", "oauth": {},“aws_appsync_graphqlEndpoint”:“”,“aws_appsync_region”:“”,“aws_appsync_authenticationType”:“”,};
    • 所以我在 Angular 中创建了一个 API.service.ts 并且我的更新/创建只返回全局和二级索引,没有我的其他字段。你有你使用的示例模式吗?
    • 更新:我的突变需要返回所有字段。我只返回了 id 和 gsi,所以订阅受到了影响。
    • @Mark 很高兴您能够弄清楚您是否仍需要架构或想查看它让我知道
    【解决方案2】:

    对于那些正在经历相同行为的人。这是因为我在模式的 auth 部分拥有所有者。删除了{allow: owner},部分订阅立即开始工作。

    【讨论】:

    • 谢谢!这是我第二次遇到这个问题,每次我都忘记它。我可能会用这个解决方案来提问和回答问题。
    • 不能删除授权规则。您的配置中还有其他问题
    【解决方案3】:

    对于那些来到这里遇到此错误的人,请勿删除{allow: owner}。允许所有者确保只有通过 Cognito 用户池进行身份验证的用户才能运行查询、突变等。

    看起来 OP 正在使用 amplify codegen 来生成他的 API 服务,并且如果您查看侦听器具有所有者的参数。它是可选的,但如果您的 @auth 是 {allow: owner},则它是必需的。

    附加说明:不确定他是否使用了存储在其数据存储中的正确所有者字段。如果尚未创建所有者字段(或指定不同的字段),它将创建具有唯一 uuid 的所有者字段。所以他可能传递了错误的所有者或根本没有传递。

    运行一个简单的调用来获取所有者...

    export const GetOwner = async (id: string): Promise<GetOwnerQuery> => {
      const statement = `query GetAppData($id: ID!) {
          getAppData(id: $id) {
            owner
          }
        }`;
      const gqlAPIServiceArguments: any = {
        id,
      };
      const response = (await API.graphql(graphqlOperation(statement, gqlAPIServiceArguments))) as any;
      return <GetOwnerQuery>response.data.getAppData;
    };
    

    ...并将其传递到您的订阅中。

    const { owner } = await GetOwner(id);
    if (owner) {
      this.apiUpdateListener$ = this.api.OnUpdateAppDataListener(owner).subscribe((data) => {
        console.log('api update listener ===> ', data);
      });
    }
    

    【讨论】:

      【解决方案4】:

      我在使用 GraphQL 时遇到了同样的问题。问题是:我们必须在变异响应中返回 Owner,以便让 Subscription 知道将这个事件发送给谁。

      删除 {allow: owner} 确实对我有用,但这不是正确的方法,因为我们需要它才能拥有基于所有者的数据访问权限。

      所以我找到的正确方法是:

      如果订阅是:

          subscription MySubscription {
        onCreateuser(owner: "$userName") {
          id
          name
          number
        }
      }
      

      变异应该是:

      mutation MyMutation {
        createUser(input: {name: "xyz", id: "user123", number: "1234567890"}) {
          id
          name
          number
          owner
        }
      }
      

      我们必须在突变响应中返回所有者,以便获得对该事件的订阅以及与突变响应相同的所有其他属性。

      【讨论】:

        猜你喜欢
        • 2020-12-28
        • 1970-01-01
        • 2016-07-26
        • 2021-03-11
        • 2018-02-01
        • 2019-12-27
        • 2019-04-14
        • 2016-10-06
        • 1970-01-01
        相关资源
        最近更新 更多