dmawrey
dmawrey

Reputation: 23

AWS AppSync Subscription Arguments

What can the arguments on an AppSync GraphQL subscription be used for? According to the real-time data page on the docs:

An important part of using GraphQL subscriptions is understanding when and how to use arguments, as subtle changes will allow you to modify how and when clients are notified of mutations that have occured. (...) In the default sample, clients can subscribe to Comments when a specific eventId argument is passed through

I can't find any way to access the subscription arguments, though. I have different 'channels' of comments that I'd like users to be able to subscribe to individually, optionally with a password. The subscription I've set up responds to comments in all channels instead.

Is there any way to 'filter' the data coming through the subscription with a mapping template or similar? If not, what's the intended use of the subscription arguments? Must the filtering be done client side?

Upvotes: 2

Views: 5954

Answers (2)

mparis
mparis

Reputation: 3683

The name of the argument is expected to be the same as the name of the field in the mutation response that triggered the event. If your mutation returns a value of type "Post" that contains a field "title" then passing an argument named "title" to the subscription that is subscribed to that mutation will only get pushed values where the "title" passed to the subscription equals the value of field named "title" returned by the mutation.

Upvotes: 14

Richard
Richard

Reputation: 1780

The arguments control what data clients will get subscription notifications from. For example if you put in an argument via the schema which is required by using the bang (!) symbol then clients can only subscribe to data on a specific mutation for that parameter.

GraphQL arguments, including those passed in a subscription, should be available via $ctx.args in your resolver (this is shorthand for $context.arguments). For example if you have a query of getThing(name:"XYZ") then you can access in your resolver with $ctx.args.name.

For your use case I would suggest using arguments along with a resolver on the subscription so that users can only subscribe to a channel by that argument, if they match some authorization criteria, such as the password or looking at the logged in user. You can find an example of this here: https://docs.aws.amazon.com/appsync/latest/devguide/security-authorization-use-cases.html#real-time-data

Upvotes: 1

Related Questions