From 963e56d5be07f0d5877ea931522684486a738583 Mon Sep 17 00:00:00 2001 From: equartey Date: Thu, 2 Mar 2023 09:35:15 -0600 Subject: [PATCH 1/5] chore(flutter): GraphQL subscriptions by id --- .../flutter/subscriptions-by-id.mdx | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx index 4169e95ebef..a08de1c1d07 100644 --- a/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx @@ -1,5 +1,9 @@ Now you can create a custom subscription for comment creation with a specific post id: + + + + ```dart Future subscribeByPostId(String postId) async { const graphQLDocument = r''' @@ -27,4 +31,34 @@ Future subscribeByPostId(String postId) async { print('Error in subscription stream: $e'); } } -``` \ No newline at end of file +``` + + + + + +```dart +Future subscribeByPostId(String postId) async { + final subscriptionRequest = ModelSubscriptions.onCreate( + Comment.classType, + where: Comment.POST.eq(postId), + ); + + 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'); + } +} +``` + + + + \ No newline at end of file From 8b24dd8a1f839050d027fe70e20e1c8248ec53c1 Mon Sep 17 00:00:00 2001 From: equartey Date: Thu, 2 Mar 2023 14:38:20 -0600 Subject: [PATCH 2/5] fix: removed old cli method from Flutter path --- .../android/subscriptions-by-id.mdx | 4 +- .../common/subscriptions-by-id.mdx | 42 +++++++++++++++++++ .../flutter/subscriptions-by-id.mdx | 8 +++- .../api-graphql/ios/subscriptions-by-id.mdx | 5 ++- .../api-graphql/js/subscriptions-by-id.mdx | 4 ++ .../q/platform/[platform].mdx | 39 ----------------- 6 files changed, 59 insertions(+), 43 deletions(-) create mode 100644 src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx diff --git a/src/fragments/guides/api-graphql/android/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/android/subscriptions-by-id.mdx index bc4a56052cf..eae6f90bfd0 100644 --- a/src/fragments/guides/api-graphql/android/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/android/subscriptions-by-id.mdx @@ -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"; + + diff --git a/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx new file mode 100644 index 00000000000..04b4d33292d --- /dev/null +++ b/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx @@ -0,0 +1,42 @@ +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"]) +} + +``` + +Now you can create a custom subscription for comment creation with a specific post id: diff --git a/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx index a08de1c1d07..14c1e3162e6 100644 --- a/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx @@ -1,9 +1,11 @@ -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"; + + + ```dart Future subscribeByPostId(String postId) async { const graphQLDocument = r''' @@ -37,6 +39,8 @@ Future subscribeByPostId(String postId) async { +You can subscribe to only comments for a specific post with the following: + ```dart Future subscribeByPostId(String postId) async { final subscriptionRequest = ModelSubscriptions.onCreate( diff --git a/src/fragments/guides/api-graphql/ios/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/ios/subscriptions-by-id.mdx index cf2a3599d5f..373101488e8 100644 --- a/src/fragments/guides/api-graphql/ios/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/ios/subscriptions-by-id.mdx @@ -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"; + + + ```swift extension GraphQLRequest { diff --git a/src/fragments/guides/api-graphql/js/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/js/subscriptions-by-id.mdx index 748f1a658ad..a9d07788e23 100644 --- a/src/fragments/guides/api-graphql/js/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/js/subscriptions-by-id.mdx @@ -1,3 +1,7 @@ +import all0 from "/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx"; + + + ```js import { API } from 'aws-amplify'; import { onCommentByPostId } from './graphql/subscriptions'; diff --git a/src/pages/guides/api-graphql/subscriptions-by-id/q/platform/[platform].mdx b/src/pages/guides/api-graphql/subscriptions-by-id/q/platform/[platform].mdx index f9af90b5bca..3717ed23229 100644 --- a/src/pages/guides/api-graphql/subscriptions-by-id/q/platform/[platform].mdx +++ b/src/pages/guides/api-graphql/subscriptions-by-id/q/platform/[platform].mdx @@ -23,46 +23,7 @@ type Comment @model { } ``` -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"; From cd0c09e6e35b508ad9d73487379fc79a16c73dbb Mon Sep 17 00:00:00 2001 From: equartey Date: Mon, 13 Mar 2023 10:01:47 -0500 Subject: [PATCH 3/5] Fix: updated schema --- .../common/subscriptions-by-id.mdx | 16 +++++++++++++++ .../flutter/subscriptions-by-id.mdx | 20 ++++++++++++++++++- .../q/platform/[platform].mdx | 18 ----------------- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx index 04b4d33292d..2b53839cdfe 100644 --- a/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx @@ -1,3 +1,19 @@ +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 diff --git a/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx index 14c1e3162e6..3480f92ffc7 100644 --- a/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx @@ -39,7 +39,25 @@ Future subscribeByPostId(String postId) async { -You can subscribe to only comments for a specific post with the following: +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 { diff --git a/src/pages/guides/api-graphql/subscriptions-by-id/q/platform/[platform].mdx b/src/pages/guides/api-graphql/subscriptions-by-id/q/platform/[platform].mdx index 3717ed23229..4e650917cf3 100644 --- a/src/pages/guides/api-graphql/subscriptions-by-id/q/platform/[platform].mdx +++ b/src/pages/guides/api-graphql/subscriptions-by-id/q/platform/[platform].mdx @@ -7,24 +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 -} -``` - - - import ios0 from "/src/fragments/guides/api-graphql/ios/subscriptions-by-id.mdx"; From e99ff79c63f83d9ffb82d1edcd670864be452760 Mon Sep 17 00:00:00 2001 From: equartey Date: Mon, 13 Mar 2023 12:38:27 -0500 Subject: [PATCH 4/5] fix: added auth rules to old schema --- .../guides/api-graphql/common/subscriptions-by-id.mdx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx index 2b53839cdfe..d89caa089be 100644 --- a/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx @@ -1,14 +1,14 @@ Take for example the following GraphQL schema: ```graphql -type Post @model { +type Post @model @auth(rules: [{allow: public, provider: apiKey}]){ id: ID! title: String! content: String comments: [Comment] @hasMany } -type Comment @model { +type Comment @model @auth(rules: [{allow: public, provider: apiKey}]){ id: ID! content: String } @@ -35,14 +35,14 @@ Because the schema has a one to many relationship enabled between posts and comm To implement this, you could update the schema with the following: ```graphql -type Post @model { +type Post @model @auth(rules: [{allow: public, provider: apiKey}]){ id: ID! title: String! content: String comments: [Comment] @hasMany } -type Comment @model { +type Comment @model @auth(rules: [{allow: public, provider: apiKey}]){ id: ID! content: String postCommentsId: ID! From 28ea17bdac8d49857e1542e4f3997196ce80850c Mon Sep 17 00:00:00 2001 From: equartey Date: Tue, 14 Mar 2023 12:38:55 -0500 Subject: [PATCH 5/5] fix: spacing and auth mode --- .../guides/api-graphql/common/subscriptions-by-id.mdx | 8 ++++---- .../guides/api-graphql/flutter/subscriptions-by-id.mdx | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx index d89caa089be..7651bbe4998 100644 --- a/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/common/subscriptions-by-id.mdx @@ -1,14 +1,14 @@ Take for example the following GraphQL schema: ```graphql -type Post @model @auth(rules: [{allow: public, provider: apiKey}]){ +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}]){ +type Comment @model @auth(rules: [{ allow: public, provider: apiKey }]){ id: ID! content: String } @@ -35,14 +35,14 @@ Because the schema has a one to many relationship enabled between posts and comm To implement this, you could update the schema with the following: ```graphql -type Post @model @auth(rules: [{allow: public, provider: apiKey}]){ +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}]){ +type Comment @model @auth(rules: [{ allow: public, provider: apiKey }]){ id: ID! content: String postCommentsId: ID! diff --git a/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx b/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx index 3480f92ffc7..351b8c24407 100644 --- a/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx +++ b/src/fragments/guides/api-graphql/flutter/subscriptions-by-id.mdx @@ -42,14 +42,14 @@ Future subscribeByPostId(String postId) async { Take for example the following GraphQL schema: ```graphql -type Post @model @auth(rules: [{allow: public, provider: apiKey}]){ +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}]){ +type Comment @model @auth(rules: [{ allow: public, provider: apiKey }]){ id: ID! content: String post: Post @belongsTo(fields: ["postCommentsId"]) @@ -64,6 +64,7 @@ Future subscribeByPostId(String postId) async { final subscriptionRequest = ModelSubscriptions.onCreate( Comment.classType, where: Comment.POST.eq(postId), + authorizationMode: APIAuthorizationType.apiKey, ); final operation = Amplify.API.subscribe(