Skip to content

feat(parser): add schemas for AppSync Events #3907

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

Merged
merged 4 commits into from
May 12, 2025
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
30 changes: 17 additions & 13 deletions docs/features/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,28 @@ Both are also able to parse either an object or JSON string as an input.
| **APIGatewayRequestAuthorizerEventSchema** | Lambda Event Source payload for Amazon API Gateway Request Authorizer |
| **APIGatewayTokenAuthorizerEventSchema** | Lambda Event Source payload for Amazon API Gateway Token Authorizer |
| **APIGatewayProxyEventV2Schema** | Lambda Event Source payload for Amazon API Gateway v2 payload |
| **APIGatewayProxyWebsocketEventSchema** | Lambda Event Source payload for Amazon API Gateway WebSocket events |
| **APIGatewayProxyWebsocketEventSchema** | Lambda Event Source payload for Amazon API Gateway WebSocket events |
| **APIGatewayRequestAuthorizerEventV2Schema** | Lambda Event Source payload for Amazon API Gateway v2 Authorizer |
| **AppSyncResolverSchema** | Lambda Event Source payload for AWS AppSync GraphQL API resolver |
| **AppSyncBatchResolverSchema** | Lambda Event Source payload for AWS AppSync GraphQL API batch resolver |
| **AppSyncEventsPublishSchema** | Lambda Event Source payload for AWS AppSync Events API `PUBLISH` operation |
| **AppSyncEventsSubscribeSchema** | Lambda Event Source payload for AWS AppSync Events API `SUBSCRIBE` operation |
| **CloudFormationCustomResourceCreateSchema** | Lambda Event Source payload for AWS CloudFormation `CREATE` operation |
| **CloudFormationCustomResourceUpdateSchema** | Lambda Event Source payload for AWS CloudFormation `UPDATE` operation |
| **CloudFormationCustomResourceDeleteSchema** | Lambda Event Source payload for AWS CloudFormation `DELETE` operation |
| **CloudwatchLogsSchema** | Lambda Event Source payload for Amazon CloudWatch Logs |
| **PreSignupTriggerSchema** | Lambda Event Source payload for Amazon Cognito Pre Sign-up trigger |
| **PostConfirmationTriggerSchema** | Lambda Event Source payload for Amazon Cognito Post Confirmation trigger |
| **PreTokenGenerationTriggerSchema** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger |
| **CustomMessageTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom Message trigger |
| **MigrateUserTriggerSchema** | Lambda Event Source payload for Amazon Cognito User Migration trigger |
| **CustomSMSTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom SMS trigger |
| **CustomEmailTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom Email trigger |
| **DefineAuthChallengeTriggerSchema** | Lambda Event Source payload for Amazon Cognito Define Auth Challenge trigger |
| **CreateAuthChallengeTriggerSchema** | Lambda Event Source payload for Amazon Cognito Create Auth Challenge trigger |
| **VerifyAuthChallengeResponseTriggerSchema** | Lambda Event Source payload for Amazon Cognito Verify Auth Challenge Response trigger |
| **PreTokenGenerationTriggerSchemaV1** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger v1 |
| **PreTokenGenerationTriggerSchemaV2AndV3** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger v2 and v3 |
| **PreSignupTriggerSchema** | Lambda Event Source payload for Amazon Cognito Pre Sign-up trigger |
| **PostConfirmationTriggerSchema** | Lambda Event Source payload for Amazon Cognito Post Confirmation trigger |
| **PreTokenGenerationTriggerSchema** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger |
| **CustomMessageTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom Message trigger |
| **MigrateUserTriggerSchema** | Lambda Event Source payload for Amazon Cognito User Migration trigger |
| **CustomSMSTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom SMS trigger |
| **CustomEmailTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom Email trigger |
| **DefineAuthChallengeTriggerSchema** | Lambda Event Source payload for Amazon Cognito Define Auth Challenge trigger |
| **CreateAuthChallengeTriggerSchema** | Lambda Event Source payload for Amazon Cognito Create Auth Challenge trigger |
| **VerifyAuthChallengeResponseTriggerSchema** | Lambda Event Source payload for Amazon Cognito Verify Auth Challenge Response trigger |
| **PreTokenGenerationTriggerSchemaV1** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger v1 |
| **PreTokenGenerationTriggerSchemaV2AndV3** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger v2 and v3 |
| **DynamoDBStreamSchema** | Lambda Event Source payload for Amazon DynamoDB Streams |
| **EventBridgeSchema** | Lambda Event Source payload for Amazon EventBridge |
| **KafkaMskEventSchema** | Lambda Event Source payload for AWS MSK payload |
Expand Down
172 changes: 172 additions & 0 deletions packages/parser/src/schemas/appsync-events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import { z } from 'zod';
import {
AppSyncCognitoIdentity,
AppSyncIamIdentity,
AppSyncOidcIdentity,
} from './appsync-shared.js';

/**
* A zod schema for the AppSync Events `identity` object when using an AWS Lambda Authorizer.
*/
const AppSyncLambdaAuthIdentity = z.object({
handlerContext: z.record(z.string(), z.unknown()),
});

/**
* A zod schema for AppSync Events request object.
*
* This schema is used when extending subscribe and publish events.
*/
const AppSyncEventsRequestSchema = z.object({
headers: z.record(z.string(), z.string()).optional(),
domainName: z.string().nullable(),
});

/**
* A zod schema for AppSync Events info object.
*
* This schema is used when extending subscribe and publish events.
*/
const AppSyncEventsInfoSchema = z.object({
channel: z.object({
path: z.string(),
segments: z.array(z.string()),
}),
channelNamespace: z.object({
name: z.string(),
}),
operation: z.union([z.literal('PUBLISH'), z.literal('SUBSCRIBE')]),
});

/**
* A zod schema for AppSync Events base events.
*
* This schema is used as a base for both publish and subscribe events.
*/
const AppSyncEventsBaseSchema = z.object({
identity: z.union([
z.null(),
AppSyncCognitoIdentity,
AppSyncIamIdentity,
AppSyncLambdaAuthIdentity,
AppSyncOidcIdentity,
]),
result: z.null(),
request: AppSyncEventsRequestSchema,
info: AppSyncEventsInfoSchema,
error: z.null(),
prev: z.null(),
stash: z.object({}),
outErrors: z.array(z.unknown()),
events: z.null(),
});

/**
* A zod schema for AppSync Events publish events.
*
* @example
* ```json
* {
* "identity": null,
* "result": null,
* "request": {
* "headers": {
* "header1": "value1",
* },
* "domainName": "example.com"
* },
* "info": {
* "channel": {
* "path": "/default/foo",
* "segments": ["default", "foo"]
* },
* "channelNamespace": {
* "name": "default"
* },
* "operation": "PUBLISH"
* },
* "error": null,
* "prev": null,
* "stash": {},
* "outErrors": [],
* "events": [
* {
* "payload": {
* "key": "value"
* },
* "id": "12345"
* },
* {
* "payload": {
* "key2": "value2"
* },
* "id": "67890"
* }
* ]
* }
* ```
*/
const AppSyncEventsPublishSchema = AppSyncEventsBaseSchema.extend({
info: AppSyncEventsInfoSchema.extend({
operation: z.literal('PUBLISH'),
}),
events: z
.array(
z.object({
payload: z.record(z.string(), z.unknown()),
id: z.string(),
})
)
.min(1),
});

/**
* A zod schema for AppSync Events subscribe events.
*
* @example
* ```json
* {
* "identity": null,
* "result": null,
* "request": {
* "headers": {
* "header1": "value1",
* },
* "domainName": "example.com"
* },
* "info": {
* "channel": {
* "path": "/default/foo",
* "segments": ["default", "foo"]
* },
* "channelNamespace": {
* "name": "default"
* },
* "operation": "SUBSCRIBE"
* },
* "error": null,
* "prev": null,
* "stash": {},
* "outErrors": [],
* "events": null,
* }
* ```
*/
const AppSyncEventsSubscribeSchema = AppSyncEventsBaseSchema.extend({
info: AppSyncEventsInfoSchema.extend({
operation: z.literal('SUBSCRIBE'),
}),
events: z.null(),
});

export {
AppSyncEventsBaseSchema,
AppSyncCognitoIdentity,
AppSyncIamIdentity,
AppSyncLambdaAuthIdentity,
AppSyncOidcIdentity,
AppSyncEventsRequestSchema,
AppSyncEventsInfoSchema,
AppSyncEventsPublishSchema,
AppSyncEventsSubscribeSchema,
};
30 changes: 30 additions & 0 deletions packages/parser/src/schemas/appsync-shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { z } from 'zod';

const AppSyncIamIdentity = z.object({
accountId: z.string(),
cognitoIdentityPoolId: z.string().nullable(),
cognitoIdentityId: z.string().nullable(),
sourceIp: z.array(z.string()),
username: z.string(),
userArn: z.string(),
cognitoIdentityAuthType: z.string().nullable(),
cognitoIdentityAuthProvider: z.string().nullable(),
});

const AppSyncCognitoIdentity = z.object({
sub: z.string(),
issuer: z.string(),
username: z.string(),
claims: z.record(z.string(), z.unknown()),
sourceIp: z.array(z.string().ip()),
defaultAuthStrategy: z.string().nullable(),
groups: z.array(z.string()).nullable(),
});

const AppSyncOidcIdentity = z.object({
claims: z.any(),
issuer: z.string(),
sub: z.string(),
});

export { AppSyncCognitoIdentity, AppSyncIamIdentity, AppSyncOidcIdentity };
41 changes: 13 additions & 28 deletions packages/parser/src/schemas/appsync.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,9 @@
import { z } from 'zod';

const AppSyncIamIdentity = z.object({
accountId: z.string(),
cognitoIdentityPoolId: z.string().nullable(),
cognitoIdentityId: z.string().nullable(),
sourceIp: z.array(z.string()),
username: z.string(),
userArn: z.string(),
cognitoIdentityAuthType: z.string().nullable(),
cognitoIdentityAuthProvider: z.string().nullable(),
});

const AppSyncCognitoIdentity = z.object({
sub: z.string(),
issuer: z.string(),
username: z.string(),
claims: z.any(),
sourceIp: z.array(z.string()),
defaultAuthStrategy: z.string(),
groups: z.array(z.string()).nullable(),
});

const AppSyncOidcIdentity = z.object({
claims: z.any(),
issuer: z.string(),
sub: z.string(),
});
import {
AppSyncCognitoIdentity,
AppSyncIamIdentity,
AppSyncOidcIdentity,
} from './appsync-shared.js';

const AppSyncLambdaIdentity = z.object({
resolverContext: z.any(),
Expand Down Expand Up @@ -251,4 +229,11 @@ const AppSyncResolverSchema = z.object({

const AppSyncBatchResolverSchema = z.array(AppSyncResolverSchema);

export { AppSyncResolverSchema, AppSyncBatchResolverSchema };
export {
AppSyncResolverSchema,
AppSyncBatchResolverSchema,
AppSyncCognitoIdentity,
AppSyncIamIdentity,
AppSyncOidcIdentity,
AppSyncLambdaIdentity,
};
9 changes: 8 additions & 1 deletion packages/parser/src/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ export {
APIGatewayTokenAuthorizerEventSchema,
APIGatewayEventRequestContextSchema,
} from './api-gateway.js';
export { APIGatewayProxyWebsocketEventSchema } from './api-gateway-websocket.js'
export { APIGatewayProxyWebsocketEventSchema } from './api-gateway-websocket.js';
export {
AppSyncResolverSchema,
AppSyncBatchResolverSchema,
} from './appsync.js';
export {
AppSyncEventsBaseSchema,
AppSyncEventsRequestSchema,
AppSyncEventsInfoSchema,
AppSyncEventsPublishSchema,
AppSyncEventsSubscribeSchema,
} from './appsync-events.js';
export {
APIGatewayProxyEventV2Schema,
APIGatewayRequestAuthorizerEventV2Schema,
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export type {
APIGatewayTokenAuthorizerEvent,
AppSyncBatchResolverEvent,
AppSyncResolverEvent,
AppSyncEventsPublishEvent,
AppSyncEventsSubscribeEvent,
CloudFormationCustomResourceCreateEvent,
CloudFormationCustomResourceDeleteEvent,
CloudFormationCustomResourceUpdateEvent,
Expand Down
12 changes: 11 additions & 1 deletion packages/parser/src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import type {
AlbMultiValueHeadersSchema,
AlbSchema,
AppSyncBatchResolverSchema,
AppSyncEventsPublishSchema,
AppSyncEventsSubscribeSchema,
AppSyncResolverSchema,
CloudFormationCustomResourceCreateSchema,
CloudFormationCustomResourceDeleteSchema,
Expand Down Expand Up @@ -69,7 +71,9 @@ type APIGatewayEventRequestContext = z.infer<

type APIGatewayProxyEventV2 = z.infer<typeof APIGatewayProxyEventV2Schema>;

type APIGatewayProxyWebsocketEvent = z.infer<typeof APIGatewayProxyWebsocketEventSchema>;
type APIGatewayProxyWebsocketEvent = z.infer<
typeof APIGatewayProxyWebsocketEventSchema
>;

type APIGatewayRequestAuthorizerV2 = z.infer<
typeof APIGatewayRequestAuthorizerV2Schema
Expand All @@ -83,6 +87,10 @@ type AppSyncResolverEvent = z.infer<typeof AppSyncResolverSchema>;

type AppSyncBatchResolverEvent = z.infer<typeof AppSyncBatchResolverSchema>;

type AppSyncEventsPublishEvent = z.infer<typeof AppSyncEventsPublishSchema>;

type AppSyncEventsSubscribeEvent = z.infer<typeof AppSyncEventsSubscribeSchema>;

type CloudWatchLogEvent = z.infer<typeof CloudWatchLogEventSchema>;

type CloudWatchLogsDecode = z.infer<typeof CloudWatchLogsDecodeSchema>;
Expand Down Expand Up @@ -176,6 +184,8 @@ export type {
APIGatewayTokenAuthorizerEvent,
AppSyncBatchResolverEvent,
AppSyncResolverEvent,
AppSyncEventsPublishEvent,
AppSyncEventsSubscribeEvent,
CloudFormationCustomResourceCreateEvent,
CloudFormationCustomResourceDeleteEvent,
CloudFormationCustomResourceUpdateEvent,
Expand Down
28 changes: 28 additions & 0 deletions packages/parser/tests/events/appsync-events/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"identity": null,
"result": null,
"request": {
"headers": {
"key": "value"
},
"domainName": null
},
"info": {
"channel": {
"path": "/request/channel",
"segments": [
"request",
"channel"
]
},
"channelNamespace": {
"name": "request"
},
"operation": "PUBLISH"
},
"error": null,
"prev": null,
"stash": {},
"outErrors": [],
"events": null
}
Loading
Loading