-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(flutter): GraphQL subscriptions by id (#5187)
* chore(flutter): GraphQL subscriptions by id * fix: removed old cli method from Flutter path * Fix: updated schema * fix: added auth rules to old schema * fix: spacing and auth mode
- Loading branch information
Showing
6 changed files
with
128 additions
and
61 deletions.
There are no files selected for viewing
4 changes: 3 additions & 1 deletion
4
src/fragments/guides/api-graphql/android/subscriptions-by-id.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters