Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Amplify AppSync Subscription not working correctly

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.

like image 628
OGi Avatar asked Oct 23 '25 02:10

OGi


1 Answers

I had the same problem with GraphQL. Thing is: we have to return Owner in mutation response in order to let Subscription know to whom to send this event.

removing {allow: owner} did worked for me but thats not the right way since we require it in order to have owner based access to data.

so the correct way i found is:

if subscription is:

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

mutation should be:

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

we must return owner in mutation's response in order to get the subscription to that event and all other properties as same as mutation response.

like image 172
Jeet Chhatrala Avatar answered Oct 26 '25 10:10

Jeet Chhatrala



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!