From f1f59e97dfe536b21e6d713aa99301202c57a077 Mon Sep 17 00:00:00 2001 From: Varshitha Pamisetty Date: Mon, 26 Oct 2026 02:03:38 +0100 Subject: [PATCH 1/2] fix(data): add native client examples to batch DynamoDB operations page --- .../batch-ddb-operations/index.mdx | 175 ++++++++++++++++-- 1 file changed, 162 insertions(+), 13 deletions(-) diff --git a/src/pages/[platform]/build-a-backend/data/custom-business-logic/batch-ddb-operations/index.mdx b/src/pages/[platform]/build-a-backend/data/custom-business-logic/batch-ddb-operations/index.mdx index 551b4ab6d5e..af42b939797 100644 --- a/src/pages/[platform]/build-a-backend/data/custom-business-logic/batch-ddb-operations/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/custom-business-logic/batch-ddb-operations/index.mdx @@ -37,22 +37,18 @@ Batch DynamoDB operations allow you to add multiple items in single mutation. import { type ClientSchema, a, defineData } from '@aws-amplify/backend'; const schema = a.schema({ - // 1. Define your return type as a custom type or model Post: a.model({ id: a.id(), content: a.string(), likes: a.integer() - }), + }).authorization(allow => [allow.authenticated()]), - // 2. Define your mutation with the return type and, optionally, arguments - BatchCreatePost: a + batchCreatePost: a .mutation() - // arguments that this query accepts .arguments({ - content: a.string().array() + contents: a.string().array() }) .returns(a.ref('Post').array()) - // only allow signed-in users to call this API .authorization(allow => [allow.authenticated()]) }); @@ -79,20 +75,19 @@ const schema = a.schema({ id: a.id(), content: a.string(), likes: a.integer() - }), + }).authorization(allow => [allow.authenticated()]), - BatchCreatePost: a + batchCreatePost: a .mutation() .arguments({ contents: a.string().array() }) .returns(a.ref('Post').array()) .authorization(allow => [allow.authenticated()]) - // 1. Add the custom handler .handler( a.handler.custom({ dataSource: a.ref('Post'), - entry: './BatchCreatePostHandler.js', + entry: './batchCreatePostHandler.js', }) ) }); @@ -113,7 +108,7 @@ Amplify will store some values in the resolver context stash that can be accesse The Amplify generated DynamoDB table names can be constructed from the variables in the context stash. The table name is in the format `--NONE`. For example, the table name for the `Post` model would be `Post-abc123def456-NONE` where `abc123def456` is the AppSync API ID. -```ts title="amplify/data/BatchCreatePostHandler.js" +```ts title="amplify/data/batchCreatePostHandler.js" import { util } from '@aws-appsync/utils'; export function request(ctx) { @@ -144,10 +139,164 @@ export function response(ctx) { ## Step 3 - Invoke the custom query or mutation + + From your generated Data client, you can find all your custom queries and mutations under the `client.queries.` and `client.mutations.` APIs respectively. ```ts -const { data, errors } = await client.mutations.BatchCreatePost({ +const { data, errors } = await client.mutations.batchCreatePost({ contents: ['Post 1', 'Post 2', 'Post 3'] }); ``` + + + + + +```swift +struct BatchCreatePostResponse: Codable { + public let batchCreatePost: [Post] + + struct Post: Codable { + public let id: String + public let content: String? + public let likes: Int? + } +} + +let document = """ + mutation BatchCreatePost($contents: [String]) { + batchCreatePost(contents: $contents) { + id + content + likes + } + } + """ + +let result = try await Amplify.API.mutate(request: GraphQLRequest( + document: document, + variables: [ + "contents": ["Post 1", "Post 2", "Post 3"] + ], + responseType: BatchCreatePostResponse.self +)) +switch result { +case .success(let response): + print(response.batchCreatePost) +case .failure(let error): + print(error) +} +``` + + + + + +```kotlin +data class PostDetails( + val id: String, + val content: String?, + val likes: Int? +) + +data class BatchCreatePostResponse( + val batchCreatePost: List +) + +val document = """ + mutation BatchCreatePost(${'$'}contents: [String]) { + batchCreatePost(contents: ${'$'}contents) { + id + content + likes + } + } +""".trimIndent() + +val request = SimpleGraphQLRequest( + document, + mapOf("contents" to listOf("Post 1", "Post 2", "Post 3")), + String::class.java, + GsonVariablesSerializer()) + +Amplify.API.mutate( + request, + { + var gson = Gson() + val response = gson.fromJson(it.data, BatchCreatePostResponse::class.java) + Log.i("MyAmplifyApp", "${response.batchCreatePost}") + }, + { Log.e("MyAmplifyApp", "Mutation failed", it) } +) +``` + + + + + +First define a class that matches your response shape: + +```dart +class BatchCreatePostResponse { + final List posts; + + BatchCreatePostResponse({required this.posts}); + + factory BatchCreatePostResponse.fromJson(Map json) { + return BatchCreatePostResponse( + posts: (json['batchCreatePost'] as List) + .map((post) => Post.fromJson(post)) + .toList(), + ); + } +} + +class Post { + final String id; + final String? content; + final int? likes; + + Post({required this.id, this.content, this.likes}); + + factory Post.fromJson(Map json) { + return Post( + id: json['id'], + content: json['content'], + likes: json['likes'], + ); + } +} +``` + +Next, make the request and map the response to the classes defined above: + +```dart +import 'dart:convert'; + +const graphQLDocument = ''' + mutation BatchCreatePost(\$contents: [String]) { + batchCreatePost(contents: \$contents) { + id + content + likes + } + } +'''; + +final request = GraphQLRequest( + document: graphQLDocument, + variables: { + "contents": ["Post 1", "Post 2", "Post 3"] + }, +); + +final response = await Amplify.API.mutate(request: request).response; + +Map jsonMap = json.decode(response.data!); +BatchCreatePostResponse batchCreatePostResponse = + BatchCreatePostResponse.fromJson(jsonMap); +safePrint(batchCreatePostResponse.posts); +``` + + From 82af2646cfa1c3aea896049eab1944a0f57599c9 Mon Sep 17 00:00:00 2001 From: Varshitha Pamisetty Date: Mon, 26 Oct 2026 11:45:17 +0100 Subject: [PATCH 2/2] fix(data): remove recoverable comments from custom business logic page --- .../data/custom-business-logic/index.mdx | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/src/pages/[platform]/build-a-backend/data/custom-business-logic/index.mdx b/src/pages/[platform]/build-a-backend/data/custom-business-logic/index.mdx index d8ed43875fd..94bcca84061 100644 --- a/src/pages/[platform]/build-a-backend/data/custom-business-logic/index.mdx +++ b/src/pages/[platform]/build-a-backend/data/custom-business-logic/index.mdx @@ -54,22 +54,17 @@ For every custom query or mutation, you need to set a return type and, optionall import { type ClientSchema, a, defineData } from '@aws-amplify/backend'; const schema = a.schema({ - // 1. Define your return type as a custom type EchoResponse: a.customType({ content: a.string(), executionDuration: a.float() }), - // 2. Define your query with the return type and, optionally, arguments echo: a .query() - // arguments that this query accepts .arguments({ content: a.string() }) - // return type of the query .returns(a.ref('EchoResponse')) - // only allow signed-in users to call this API .authorization(allow => [allow.authenticated()]) }); @@ -88,23 +83,18 @@ export const data = defineData({ import { type ClientSchema, a, defineData } from '@aws-amplify/backend'; const schema = a.schema({ - // 1. Define your return type as a custom type or model Post: a.model({ id: a.id(), content: a.string(), likes: a.integer() }), - // 2. Define your mutation with the return type and, optionally, arguments likePost: a .mutation() - // arguments that this query accepts .arguments({ postId: a.string() }) - // return type of the query .returns(a.ref('Post')) - // only allow signed-in users to call this API .authorization(allow => [allow.authenticated()]) }); @@ -148,10 +138,9 @@ import { type ClientSchema, a, defineData, - defineFunction // 1.Import "defineFunction" to create new functions + defineFunction } from '@aws-amplify/backend'; -// 2. define a function const echoHandler = defineFunction({ entry: './echo-handler/handler.ts' }) @@ -167,7 +156,6 @@ const schema = a.schema({ .arguments({ content: a.string() }) .returns(a.ref('EchoResponse')) .authorization(allow => [allow.publicApiKey()]) - // 3. set the function has the handler .handler(a.handler.function(echoHandler)) }); @@ -529,12 +517,9 @@ const schema = a.schema({ getPost: a .query() .arguments({ - // scalar field content: a.string(), - // inline custom type config: a.customType({ filter: a.string(), - // reference to enum status: a.ref('Status') }), })