Skip to content

Commit

Permalink
Fixed functions that are imported then re-exported causing a crash (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-chambers authored Apr 10, 2024
1 parent 050d99a commit b752aab
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This changelog documents the changes between release versions.
Changes to be included in the next upcoming release

- Fixed watch mode not reloading after files with compiler errors are changed [#27](https://github.com/hasura/ndc-nodejs-lambda/pull/27)
- Fixed functions that are imported then re-exported causing a crash [#28](https://github.com/hasura/ndc-nodejs-lambda/pull/28)

## [1.2.0] - 2024-03-18
- Improved error messages when unsupported enum types or unions of literal types are found, and allow these types to be used in relaxed types mode ([#17](https://github.com/hasura/ndc-nodejs-lambda/pull/17))
Expand Down
6 changes: 5 additions & 1 deletion ndc-lambda-sdk/src/inference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,14 @@ function deriveSchemaFromFunctions(sourceFile: ts.SourceFile, projectRootDir: st
const declaration = exportedSymbol.getDeclarations()?.[0] ?? throwError("exported symbol does not have a declaration");

// If exported via 'export { name } from "./imported"'
// or 'import { name } from "./imported"; export { name }'
if (ts.isExportSpecifier(declaration)) {
const identifier = declaration.name ?? throwError("export declaration didn't have an identifier");
const exportTarget = typeChecker.getExportSpecifierLocalTargetSymbol(declaration) ?? throwError("export specifier does not have a local target symbol");
const exportTargetDeclaration = exportTarget.valueDeclaration ?? throwError("export target symbol does not have a value declaration");
const exportTargetDeclaration =
exportTarget.valueDeclaration // 'export { name } from "./imported"'
?? typeChecker.getAliasedSymbol(exportTarget).valueDeclaration // 'import { name } from "./imported"; export { name }'
?? throwError("export target symbol does not have a value declaration");
if (ts.isFunctionDeclaration(exportTargetDeclaration)) {
return [[identifier.text, exportTargetDeclaration]];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { fileAChildFunction as renamedFileAChildFunction } from "./file-a"
import { fileBChildFunction1, fileBChildFunction2 } from "./file-b"

export {
renamedFileAChildFunction,
fileBChildFunction1,
fileBChildFunction2 as renamedFileBChildFunction2
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it } from "mocha";
import { assert } from "chai";
import { deriveSchema } from "../../../src/inference";
import { BuiltInScalarTypeName, FunctionNdcKind, NullOrUndefinability } from "../../../src/schema";
import { FunctionNdcKind } from "../../../src/schema";

describe("re-exported functions", function() {
it("supports functions re-exported from other files", function() {
Expand Down Expand Up @@ -206,4 +206,65 @@ describe("re-exported functions", function() {
}
})
});

it("supports re-exports of functions from other files that are first imported then exported", function() {
const schema = deriveSchema(require.resolve("./functions-import-then-export.ts"));

assert.deepStrictEqual(schema, {
compilerDiagnostics: [],
functionIssues: {},
functionsSchema: {
functions: {
"renamedFileAChildFunction": {
ndcKind: FunctionNdcKind.Procedure,
description: null,
parallelDegree: null,
arguments: [],
resultType: {
name: "String",
kind: "scalar",
type: "named",
}
},
"fileBChildFunction1": {
ndcKind: FunctionNdcKind.Procedure,
description: null,
parallelDegree: null,
arguments: [],
resultType: {
name: "Boolean",
kind: "scalar",
type: "named",
}
},
"renamedFileBChildFunction2": {
ndcKind: FunctionNdcKind.Function,
description: null,
parallelDegree: null,
arguments: [
{
argumentName: "input",
description: null,
type: {
name: "String",
kind: "scalar",
type: "named",
}
}
],
resultType: {
name: "String",
kind: "scalar",
type: "named",
}
},
},
scalarTypes: {
Boolean: { type: "built-in" },
String: { type: "built-in" },
},
objectTypes: {},
}
})
});
});

0 comments on commit b752aab

Please sign in to comment.