-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should subscribe be working for <Connect>? #3978
Comments
The Connect component does not update its internal state with the data that it receives from subscriptions because it does not know how. It is possible to subscribe to create, update or delete events. How could Connect know which object should be updated or removed? It can‘t. That‘s what the onSubscriptionMsg callback is for. It receives the old state (prev) and the subscription data. It‘s up to you, to change prev in order to reflect the necessary changes. This should work for you: onSubscriptionMsg={(prev, { onCreateComment }) => {
prev.listComments.items.push(onCreateComment);
return prev;
}} Similarly, if you‘d decide to subscribe to update events, you could do something like this: onSubscriptionMsg={(prev, { onUpdateComment }) => {
prev.listComments.items = prev.listComments.items.map(comment => comment.id === onUpdateComment.id ? onUpdateComment : comment);
return prev;
}} Using Amplify, you can write a subscription (manually) which subscribes two multiple events. This is actually forbidden by the GraphQL spec (single root field rule) but AppSync doesn‘t care. If you mix multiple types, you can do this: onSubscriptionMsg={(prev, { onCreateComment, onUpdateComment, onDeleteComment }) => {
if (onCreateComment) {
prev.listComments.items.push(onCreateComment);
} else if (onUpdateComment) {
prev.listComments.items = prev.listComments.items.map(comment => comment.id === onUpdateComment.id ? onUpdateComment : comment);
} else if (onDeleteComment) {
prev.listComments.items = prev.listComments.items.filter(comment => comment.id !== onDeleteComment.id);
}
return prev;
}} |
@visusnet what does the "subscription" argument look like for multiple events? |
@rlimberger See the example above for multiple events in onSubscriptionMsg. However, you are asking for the subscription argument of . It is the same as for single subscriptions. The difference is the subscription itself.
Please note: This is not allowed by the GraphQL spec. However, AppSync supports it. |
@visusnet Danke! Got it working. But where/how do I add this custom (multi) subscription? The subscriptions.js file in src/graphql is auto-generated, so can't add it there. I got it working by creating this subscription manually inside App.js like this:
Whats the right place for this? Thanks again! |
@rlimberger There is no right or wrong. Put it where it works for you. You can put it in the graphql folder if you want though. It won‘t be overwritten. Just create a new file, e.g. graphql/customSubscriptions.js and put it there. That‘s what I do. However, you are free to put it wherever you want (except for automatically overwritten files). |
Looks like @visusnet helped out here. Thank you! Closing out. |
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 |
** Which Category is your question related to? **
React Connect element
** What AWS Services are you utilizing? **
Amplify, Appsync, DynamoDB
** Provide additional details e.g. code snippets **
I've followed the docs pretty closely, but I'm not quite sure why my list of data doesn't update to reflect the latest comment once I click submit. I must manually refresh the page to get the new list. The subscription is firing and the subscription message is showing the prev list plus the newly created object.
What am I missing here? Should I be programmatically refreshing the component somehow? Should I be using the onSubscriptionMsg to push the new object onto the prev? Any help is appreciated.
The code I'm using is VERY close to the docs:
The text was updated successfully, but these errors were encountered: