The documentation states that I can import types to write lambda scripts in Typescript. I have done this successfully but noticed Lambda only accepts UMD modules.
Typescript
import type * as dgraph from '@slash-graphql/lambda-types';
async function newEvent({ args, dql }: dgraph.GraphQLEventWithParent) {
// lets give every new author a reputation of 3 by default
const res = await dql.mutate(`{
set {
_:newAuth <Author.name> "${args.name}" .
_:newAuth <Author.reputation> "3.0" .
_:newAuth <dgraph.type> "Author" .
}
}`);
return res.data?.uids.newAuth;
}
// eslint-disable-next-line no-restricted-globals
(self as any).addGraphQLResolvers({
'Mutation.newAuthor': newEvent,
});
Transpiled Javascript (es2021) with CommonJS:
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
async function newEvent({ args, dql }) {
// lets give every new author a reputation of 3 by default
const res = await dql.mutate(`{
set {
_:newAuth <Author.name> "${args.name}" .
_:newAuth <Author.reputation> "3.0" .
_:newAuth <dgraph.type> "Author" .
}
}`);
return res.data?.uids.newAuth;
}
// eslint-disable-next-line no-restricted-globals
self.addGraphQLResolvers({
'Mutation.newAuthor': newEvent,
});
CommonJS Full error:
You have triggered an unhandledRejection, you may have forgotten to catch a Promise rejection:
evalmachine.<anonymous>:2
Object.defineProperty(exports, "__esModule", { value: true });
^
ReferenceError: exports is not defined
at evalmachine.<anonymous>:2:23
at Script.runInContext (vm.js:144:12)
at Object.evaluateScript (/app/src/evaluate-script.ts:90:10)
at Object.scriptToExpress (/app/src/script-to-express.ts:17:18)
at startServer (/app/src/index.ts:24:15)
Errors by Module Resolution Method
Reference: https://www.typescriptlang.org/tsconfig#module
Module Resolution Method | Error |
---|---|
CommonJS | ReferenceError: exports is not defined |
UMD | No error - works |
AMD | ReferenceError: define is not defined |
System | ReferenceError: System is not defined |
esnext/es2020/es2015/es6 | SyntaxError: Unexpected token 'export' |
I think it would be useful to support CommonJS
and es6/es2015
(and newer) module resolution methods in the lambda server, and at minimum document that only UMD is supported.