I'm testing out AWS AppSync JS Resolvers and i'm currently building them in TS. To be able to use them I transpile each TS file to a JS file to be able to use them as JS Resolver in AWS AppSync and that works. But i also want to transform each local import to to be included in the output JS file.
getUser.ts
import { util, Context } from '@aws-appsync/utils';
import { dynamoDBGetItemRequest } from './utils';
export function request(ctx: Context) {
if (!ctx.identity || !('sub' in ctx.identity)) {
return util.unauthorized();
}
return dynamoDBGetItemRequest({ id: ctx.identity.sub });
}
export function response(ctx: Context) {
return ctx.result;
}
Currently transpiles into getUser.js
import { util } from '@aws-appsync/utils';
import { dynamoDBGetItemRequest } from './utils';
export function request(ctx) {
if (!ctx.identity || !('sub' in ctx.identity)) {
return util.unauthorized();
}
return dynamoDBGetItemRequest({ id: ctx.identity.sub });
}
export function response(ctx) {
return ctx.result;
}
The issue here is that I need to bundle the dynamoDBGetItemRequest
in the js file.
Desired outcome: getUser.js
import { util } from '@aws-appsync/utils';
const dynamoDBGetItemRequest = (key) => {
return {
operation: 'GetItem',
key: util.dynamodb.toMapValues(key),
};
};
export function request(ctx) {
if (!ctx.identity || !('sub' in ctx.identity)) {
return util.unauthorized();
}
return dynamoDBGetItemRequest({ id: ctx.identity.sub });
}
export function response(ctx) {
return ctx.result;
}
How can i achieve this bundling using tsc
?
Additional notes / Context:
- There will be multiple files like this, one for each resolver.
- They must export one
request
function and oneresponse
function. - Using NX
- The js file are to be used in AWS CDK like this
appsync.Code.fromAsset('dist/appsync-resolver/some-folder/getUser.js')),
in a AppSync Function