import all0 from "/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx";
```dart
Future subscribeByPostId(String postId) async {
const graphQLDocument = r'''
subscription onCreateCommentByPostId($id: ID!) {
onCommentByPostId(postCommentsId: $id) {
content
id
postCommentsId
}
}
''';
final Stream> operation = Amplify.API.subscribe(
GraphQLRequest(
document: graphQLDocument,
variables: {'id': postId},
),
onEstablished: () => print('Subscription established'),
);
try {
await for (var event in operation) {
print('Subscription event data received: ${event.data}');
}
} on Exception catch (e) {
print('Error in subscription stream: $e');
}
}
```
Take for example the following GraphQL schema:
```graphql
type Post @model @auth(rules: [{ allow: public, provider: apiKey }]){
id: ID!
title: String!
content: String
comments: [Comment] @hasMany
}
type Comment @model @auth(rules: [{ allow: public, provider: apiKey }]){
id: ID!
content: String
post: Post @belongsTo(fields: ["postCommentsId"])
postCommentsId: ID!
}
```
You can subscribe to comments from a specific post with the following:
```dart
Future subscribeByPostId(String postId) async {
final subscriptionRequest = ModelSubscriptions.onCreate(
Comment.classType,
where: Comment.POST.eq(postId),
authorizationMode: APIAuthorizationType.apiKey,
);
final operation = Amplify.API.subscribe(
subscriptionRequest,
onEstablished: () => print('Subscription established'),
);
try {
await for (var event in operation) {
print('Subscription event data received: ${event.data}');
}
} on Exception catch (e) {
print('Error in subscription stream: $e');
}
}
```