Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Parameters:
Client:
Description: Client website for authentication redirects and cors (must start with https://)
Type: String
Default: https://myapp.com
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: This sample domain was redirecting to another site, so I updated the URL for safety.

Default: https://aws.amazon.com

Resources:
# Creates a nested stack with the required Cognito requirements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"author": "",
"license": "ISC",
"dependencies": {
"aws-appsync": "^4.1.10",
"@aws-sdk/signature-v4": "^3.0.0",
"@smithy/protocol-http": "^4.0.0",
"@aws-sdk/credential-provider-node": "^3.0.0",
"@aws-crypto/sha256-js": "^5.0.0",
"axios": "^1.8.2",
"graphql": "^16.10.0",
"graphql-tag": "^2.12.6",
"isomorphic-fetch": "^3.0.0"
"graphql-tag": "^2.12.6"
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
require('isomorphic-fetch');
const AUTH_TYPE = require('aws-appsync').AUTH_TYPE;
const AWSAppSyncClient = require('aws-appsync').default;
const { SignatureV4 } = require('@aws-sdk/signature-v4');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Updated code for AWS SDK v3 using Claude Sonnet 4.5 coding agent.

const { HttpRequest } = require('@smithy/protocol-http');
const { defaultProvider } = require('@aws-sdk/credential-provider-node');
const { Sha256 } = require('@aws-crypto/sha256-js');
const gql = require('graphql-tag');

const config = {
url: process.env.APPSYNC_ENDPOINT,
region: process.env.AWS_REGION,
auth: {
type: AUTH_TYPE.AWS_IAM,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
sessionToken: process.env.AWS_SESSION_TOKEN
},
},
disableOffline: true
};
const graphql = require('graphql');
const { print } = graphql;

const createTodo = gql`
mutation MyMutation(
Expand All @@ -38,21 +27,48 @@ const createTodo = gql`
}
`;

const client = new AWSAppSyncClient(config);
const signer = new SignatureV4({
credentials: defaultProvider(),
region: process.env.AWS_REGION,
service: 'appsync',
sha256: Sha256
});

exports.handler = async function (event) {
console.log("event ", event);

try {
const result = await client.mutate({
mutation: createTodo,
variables: {
orderId: "123",
prevStatus: "PENDING",
status: "IN_PROGRESS",
updatedAt: "2021-10-07T20:38:18.683Z"
}
const url = new URL(process.env.APPSYNC_ENDPOINT);
const query = print(createTodo);
const variables = {
orderId: "123",
prevStatus: "PENDING",
status: "IN_PROGRESS",
updatedAt: "2021-10-07T20:38:18.683Z"
};

const requestBody = JSON.stringify({ query, variables });

const request = new HttpRequest({
method: 'POST',
headers: {
'Content-Type': 'application/json',
host: url.host
},
hostname: url.hostname,
path: url.pathname,
body: requestBody
});

const signedRequest = await signer.sign(request);

const response = await fetch(process.env.APPSYNC_ENDPOINT, {
method: signedRequest.method,
headers: signedRequest.headers,
body: requestBody
});

const result = await response.json();
console.log("result ", result);
} catch (error) {
console.log("error ", error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Description: Notify subscribers of database updates
Globals:
Function:
CodeUri: src/
Runtime: nodejs20.x
Runtime: nodejs22.x
Timeout: 10
MemorySize: 128
Layers:
Expand Down Expand Up @@ -39,7 +39,7 @@ Resources:
# delete old version of layer
RetentionPolicy: Delete
Metadata:
BuildMethod: nodejs16.x
BuildMethod: nodejs22.x

AppSyncLambdaUseIAM:
Type: 'AWS::Serverless::Function'
Expand Down
9 changes: 9 additions & 0 deletions appsync-notify-subscribers-of-database-updates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ Important: this application uses various AWS services and there are costs associ
```
sam deploy -g --capabilities CAPABILITY_IAM
```
13. During the prompts:
* Enter a stack name
* Select the desired AWS Region
* Enter a GraphQLApiEndpoint
* Enter an AppSyncApiKey
* Enter a GraphQLApiId
* Enter an OrdersEventBusName
* Enter an OrdersEventBusArn
* Allow SAM to create roles with the required permissions.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: Added missing step😀


## Testing Part 1 - notify via HTTP Request

Expand Down