amplify-cli icon indicating copy to clipboard operation
amplify-cli copied to clipboard

AppSync Subscriptions not working based on Parent's ID

Open rohit3d2003 opened this issue 7 years ago • 5 comments

** Which Category is your question related to? ** AppSync Subscriptions

** What AWS Services are you utilizing? ** AppSync

** Provide additional details e.g. code snippets ** I am trying to subscribe to a subscription for a child model based on parent model's Id. For example, if I have a 'Event' model which contains an array of comments, I need to specify subscription on 'createMessage' for a particular eventId. By default, Message model has eventCommentId but when I try to add a custom subscription by passing 'eventCommentId' as an argument, it does not work.

The subscription does work when I specify a 'key field' on the model while defining @connection and then use the defined 'key field' on subscription. All the mutations are auto-generated. Any idea?

Here is my model:

type Event @model(subscriptions: null) @versioned {
	id: ID!
	messages: [Message!] @connection(name: "EventMessages", sortField: "createdAt")
}

type Message @model(subscriptions: null) @versioned {
	id: ID!
	event: Event! @connection(name: "EventMessages", sortField: "createdAt")
}

type Subscription {
	onCreateMessage(messageEventId: ID!): Message @aws_subscribe(mutations: ["createMessage"])
	onDeleteMessage(messageEventId: ID!): Message @aws_subscribe(mutations: ["deleteMessage"])
}

rohit3d2003 avatar Mar 20 '19 05:03 rohit3d2003

@rohit3d2003 Thanks for the question. In order to trigger AppSync subscriptions, you need to provide the value that is being filtered on in the selection set of the mutation that triggers it. In your example, add the related id field to your model and then include it in the selection set of the mutation and you will start seeing results.

type Message @model(subscriptions: null) @versioned {
	id: ID!
	event: Event! @connection(name: "EventMessages", sortField: "createdAt")
        # Add this
        messageEventId: ID!
}

Then use this mutation

mutation CreateMessage {
  createMessage(input: { ... }) {
    id
    messageEventId
  }
}

mikeparisstuff avatar Mar 22 '19 20:03 mikeparisstuff

@mikeparisstuff - Thanks for the input but the additional field that you had mentioned is already created by cli on dynamodb. If I mention it explicitly in the schema, will there be any impact?

rohit3d2003 avatar Mar 22 '19 21:03 rohit3d2003

@mikeparisstuff - I tried the approach you had mentioned and it worked. Didn't create any duplicate fields. Thanks!

rohit3d2003 avatar Mar 27 '19 18:03 rohit3d2003

@mikeparisstuff I tried this approach and, while I did start seeing the subscription fire, the new items (of type Notification in my schema) were not added to the connecting array "notifications" in the type User. The correct user is mapped, such that if I do getNotification it will correctly map to the User. But if I getUser the notification will not appear in the user's notifications list.

I've gotten around it by adding a separate userId String field in the Notification and making the subscription fire on that, but figured I would post here in case you knew why the original approach you mentioned doesn't work.

My (abbreviated) schema: type User @model @searchable @versioned { id: ID! ... notifications: [Notification] @connection(name: "User_Notifications") }

type Notification @model @searchable { id: ID! ... user: User! @connection(name: "User_Notifications") notificationUserId: ID! ... }

type Subscription { subscribeToNotifications(notificationUserId: ID!): Notification @aws_subscribe(mutations: ["createNotification"]) }

aehringer avatar Apr 07 '19 19:04 aehringer

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

github-actions[bot] avatar May 27 '21 12:05 github-actions[bot]