aws-amplifyaws-appsync

AWS Amplify AppSync Subscription: data returning null


I was working on my Amplify App and I had subscriptions working fine with this:

graphql:

type Item @model(subscriptions: null)
    @auth(rules: [
        {allow: owner},
        {allow: groups, groups: ["Admin"], operations: [create, update, read, delete] }
  ]) {
  id: ID!
  name: String
  files: String
}


type Subscription {
    itemUpdated(id: ID): Item @aws_subscribe(mutations: ["updateItem"])
}

js:

const handleSubscription = (data) => {
  if (data.value.data.itemUpdated) {
    setItemObj(data.value.data.itemUpdated);
  }
};
useEffect(() => {
  const subscription = API.graphql(
      graphqlOperation(subscriptions.itemUpdated, {
        id,
      }),
    ).subscribe({
      next: handleSubscription,
    });
    return () => subscription.unsubscribe();
  }, []);

In the handleSubscription method, when the app made a mutation call to the Item, the return data (data.value.data.itemUpdated) would have the correct data.

Now, for reasons I am obviously unclear about, I can still see the subscription event fire when a mutation occurs, but the return data (data.value.data.itemUpdated) is consistently null.

I have tried to remove the {allow: owner} rule from the graphql schema's auth field as This Question suggests - which did not work (aside: I am still curious as to why that would work in the first place, but I do not have enough rep to comment).

While writing this, my thoughts were that I am going to try to create a new Item without the {allow: owner} rule and try again, if that works I will report back, but my question will pivot to asking why and asking then how can I ensure Items are private to the owner still? Lastly, I am almost positive I had the {allow: owner} rule in there when it was working too, but I could be mistaken.

I have also tried:

I am just not sure what is going on here. This all seems so simple and it must be something dumb I am missing...I know I will figure it out eventually, but I wanted to tap anyone here in case.

Versions:

Please let me know if I left any important information out. Thanks in advance for any help.


Solution

  • Ok.. just a dumb thing like I thought. My bad for wasting anyone's time!

    API.graphql(
          graphqlOperation(subscriptions.itemUpdated, {
             id: Id,
          }),
        ).subscribe({
          next: handleSubscription,
        });
    

    it was the id: Id, parameter. I had the Id var before named as id and js allows for shorting {name: name} to { name } - I must have changed the id var and went right to {input: { id: Id },} which is the incorrect syntax for subscriptions.

    Real bonehead move and I am appropriately embarrassed. Good lesson in bad naming even during testing.