AWS Amplify AppSync Subscription not working correctly

与世无争的帅哥 提交于 2021-02-19 05:26:35

问题


I wrote a small application that subscribes to DB changes using AWS Amplify CLI / AppSync. All amplify api calls work perfectly (mutations, queries) but unfortunately the observer does not receive events. I can see that the MQTT socket receives periodically binaries but I can't obtain changed objects.

I configured Amplify for amplify use. I can see in the debugger that the AppSyncProvider has been initisalised. Also tried API and PubSub but makes no difference.

        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>;

Anyone for any ideas?

**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

However, not sure whether it is suspicious that the subscription Object has no queue?

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

Many thanks in advance.


回答1:


For those who are experiencing the same behaviour. It was due to the fact that I had the owner in the auth section of the schema. Deleted the {allow: owner}, part and the subscriptions started to work immediately.




回答2:


here is a working example of AWS Amplify Subscriptions:


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

        }})    
    }



来源:https://stackoverflow.com/questions/58770359/aws-amplify-appsync-subscription-not-working-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!