Skip to content

Commit 32e0403

Browse files
authored
docs(data): add async function handlers section to custom queries and mutations page (#7964)
* add async function handlers section to custom queries and mutations page * update use case wording
1 parent e53ca20 commit 32e0403

File tree

1 file changed

+32
-1
lines changed
  • src/pages/[platform]/build-a-backend/data/custom-business-logic

1 file changed

+32
-1
lines changed

src/pages/[platform]/build-a-backend/data/custom-business-logic/index.mdx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ const { data, errors } = await client.mutations.likePost({
372372
```swift
373373
struct EchoResponse: Codable {
374374
public let echo: Echo
375-
375+
376376
struct Echo: Codable {
377377
public let content: String
378378
public let executionDuration: Float
@@ -508,3 +508,34 @@ safePrint(echoResponse.echo.content);
508508
```
509509

510510
</InlineFilter>
511+
512+
## Async function handlers
513+
514+
Async function handlers allow you to execute long-running operations asynchronously, improving the responsiveness of your API. This is particularly useful for tasks that don't require an immediate response, such as batch processing, putting messages in a queue, and initiating a generative AI model inference.
515+
516+
### Usage
517+
518+
To define an async function handler, use the `.async()` method when defining your handler:
519+
520+
```ts title="amplify/data/resource.ts"
521+
const signUpForNewsletter = defineFunction({
522+
entry: './sign-up-for-newsletter/handler.ts'
523+
});
524+
525+
const schema = a.schema({
526+
someAsyncOperation: a.mutation()
527+
.arguments({
528+
email: a.email().required()
529+
})
530+
.handler(a.handler.function(signUpForNewsletter).async())
531+
.authorization((allow) => allow.guest()),
532+
})
533+
```
534+
535+
### Key Characteristics
536+
537+
1. **Single Return Type**: Async handlers return a static type `EventInvocationResponse` and don't support specifying a return type. The `.returns()` method is not available for operations using async handlers.
538+
539+
2. **Fire and Forget**: The client is informed whether the invocation was successfully queued, but doesn't receive data from the Lambda function execution.
540+
541+
3. **Pipeline Support**: Async handlers can be used in function pipelines. If the final handler is an async function, the return type of the query or mutation is `EventInvocationResponse`.

0 commit comments

Comments
 (0)