Skip to content
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

chore(flutter): GraphQL subscriptions by id #5187

Merged
merged 5 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Now you can create a custom subscription for comment creation with a specific post id:
import all0 from "/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx";

<Fragments fragments={{all: all0}} />

<BlockSwitcher>
<Block name="Java">
Expand Down
58 changes: 58 additions & 0 deletions src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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
}
```

By default, subscriptions will be created for the following mutations:

```graphql
# Post type
onCreatePost
onUpdatePost
onDeletePost

# Comment type
onCreateComment
onUpdateComment
onDeleteComment
```

One operation that is not covered is if you wanted to only subscribe to comments for a specific post.

Because the schema has a one to many relationship enabled between posts and comments, you can use the auto-generated field `postCommentsId` that defines the relationship between the post and the comment to set this up in a new Subscription definition.

To implement this, you could update the schema with the following:

```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
postCommentsId: ID!
}

type Subscription {
onCommentByPostId(postCommentsId: ID!): Comment
@aws_subscribe(mutations: ["createComment"])
}

```

Now you can create a custom subscription for comment creation with a specific post id:
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
Now you can create a custom subscription for comment creation with a specific post id:
<BlockSwitcher>

<Block name="Stable (Mobile)">

import all0 from "/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx";

<Fragments fragments={{all: all0}} />

```dart
Future<void> subscribeByPostId(String postId) async {
Expand Down Expand Up @@ -27,4 +33,55 @@ Future<void> subscribeByPostId(String postId) async {
print('Error in subscription stream: $e');
}
}
```
```

</Block>

<Block name="Developer Preview (Mobile, Web & Desktop)">

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<void> 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');
}
}
```

</Block>

</BlockSwitcher>
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Now you can create a custom subscription for comment creation with a specific post id:
import all0 from "/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx";

<Fragments fragments={{all: all0}} />


```swift
extension GraphQLRequest {
Expand Down
4 changes: 4 additions & 0 deletions src/fragments/guides/api-graphql/js/subscriptions-by-id.mdx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import all0 from "/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx";

<Fragments fragments={{all: all0}} />

```js
import { API } from 'aws-amplify';
import { onCommentByPostId } from './graphql/subscriptions';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,6 @@ In this guide you will learn how to create a custom GraphQL subscription that wi

When using the Amplify GraphQL transform library, there will often be times when you need to expand the GraphQL schema and operations created by the `@model` directive. A common use case is when fine grained control is needed for GraphQL subscriptions.

Take for example the following GraphQL schema:

```graphql
type Post @model {
id: ID!
title: String!
content: String
comments: [Comment] @hasMany
}

type Comment @model {
id: ID!
content: String
}
```

By default, subscriptions will be created for the following mutations:

```graphql
# Post type
onCreatePost
onUpdatePost
onDeletePost

# Comment type
onCreateComment
onUpdateComment
onDeleteComment
```

One operation that is not covered is if you wanted to only subscribe to comments for a specific post.

Because the schema has a one to many relationship enabled between posts and comments, you can use the auto-generated field `postCommentsId` that defines the relationship between the post and the comment to set this up in a new Subscription definition.

To implement this, you could update the schema with the following:

```graphql
type Post @model {
id: ID!
title: String!
content: String
comments: [Comment] @hasMany
}

type Comment @model {
id: ID!
content: String
postCommentsId: ID!
}

type Subscription {
onCommentByPostId(postCommentsId: ID!): Comment
@aws_subscribe(mutations: ["createComment"])
}

```

import ios0 from "/src/fragments/guides/api-graphql/ios/subscriptions-by-id.mdx";

<Fragments fragments={{ios: ios0}} />
Expand Down