Skip to content

Instantly share code, notes, and snippets.

@sebastianrothbucher
Created November 12, 2023 11:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastianrothbucher/8a59febc5f864f64de5d046246b58c81 to your computer and use it in GitHub Desktop.
Save sebastianrothbucher/8a59febc5f864f64de5d046246b58c81 to your computer and use it in GitHub Desktop.
Appsync dynamodb JS
type ItemsResponse {
listid: ID!
items: [ListItem]
}
type ListItem {
listid: ID!
ts_uuid: ID!
title: String!
done: Boolean!
}
type Mutation {
putItem(listid: ID!, title: String!): ListItem
}
type Query {
items(listid: ID!): ItemsResponse
}
schema {
query: Query
mutation: Mutation
}
import { util } from "@aws-appsync/utils";
/**
* Queries a DynamoDB table for items based on the `id` and that contain the `tag`
* @param {import('@aws-appsync/utils').Context<{listid: string}>} ctx the context
* @returns {import('@aws-appsync/utils').DynamoDBQueryRequest} the request
*/
export function request(ctx) {
const query = JSON.parse(
util.transform.toDynamoDBConditionExpression({
listid: { eq: ctx.args.listid },
}),
);
return { operation: "Query", query };
}
/**
* Returns the query items
* @param {import('@aws-appsync/utils').Context} ctx the context
* @returns {{listid, items: {listid, ts_uuid, title, done}}} a flat list of result items
*/
export function response(ctx) {
if (ctx.error) {
util.error(ctx.error.message, ctx.error.type);
}
return {
listid: ctx.args.listid,
items: ctx.result.items.map(item => ({
listid: item['listid'],
ts_uuid: item['ts-uuid'],
title: item['title'],
done: item['done'],
}))
};
}
import { util } from "@aws-appsync/utils";
/**
* Puts an item into the DynamoDB table using an auto-generated ID.
* @param {import('@aws-appsync/utils').Context<{listid, title}>} ctx the context
* @returns {import('@aws-appsync/utils').DynamoDBPutItemRequest} the request
*/
export function request(ctx) {
return {
operation: "PutItem",
key: util.dynamodb.toMapValues({ listid: ctx.args.listid, 'ts-uuid': '' + util.time.nowEpochMilliSeconds() + '-' + util.autoId() }), // have a predictable yet unique range key
attributeValues: util.dynamodb.toMapValues({
title: ctx.args.title,
done: false,
}),
};
}
/**
* Returns the item or throws an error if the operation failed
* @param {import('@aws-appsync/utils').Context} ctx the context
* @returns {{listid, ts_uuid, title, done}} the inserted item
*/
export function response(ctx) {
if (ctx.error) {
util.error(ctx.error.message, ctx.error.type);
}
return {
listid: ctx.result['listid'],
ts_uuid: ctx.result['ts-uuid'],
title: ctx.result['title'],
done: ctx.result['done'],
};
}
query {
items(listid:"abc-123") {
listid,
items {
ts_uuid,
title,
done
}
}
}
mutation {
putItem(listid:"abc-123", title:"Create a gist") {
listid,
ts_uuid,
title,
done
}
}
listid ts-uuid done title
abc-123 1699783397000-def-456 false Hack some AppSync
abc-123 1699784061000-geh-789 true Create a template lib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment