Skip to content

Commit

Permalink
Exit during startup if functions fail to compile (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-chambers authored Nov 22, 2024
1 parent b8e329e commit 2711113
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 37 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This changelog documents the changes between release versions.
## [Unreleased]
Changes to be included in the next upcoming release

## [1.10.0] - 2024-11-21
- The connector now exits during startup if there are compiler errors in the functions code. The compiler errors are printed to stderr. Previously the connector would print the errors and start "successfully", but with an empty schema. The new behaviour ensures that when the connector is used with `ddn connector introspect`, `ddn` is aware that a problem has occurred (because the connector fails to start) and will prompt the user to print the logs to see the compiler errors.

## [1.9.0] - 2024-10-24

### Added
Expand Down
10 changes: 5 additions & 5 deletions ndc-lambda-sdk/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ndc-lambda-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hasura/ndc-lambda-sdk",
"version": "1.9.0",
"version": "1.10.0",
"description": "SDK that can automatically expose TypeScript functions as Hasura NDC functions/procedures",
"author": "Hasura",
"license": "Apache-2.0",
Expand Down
42 changes: 11 additions & 31 deletions ndc-lambda-sdk/src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,21 @@ export function createConnector(options: ConnectorOptions): sdk.Connector<Config
parseConfiguration: async function (configurationDir: string): Promise<Configuration> {
// We need to try imporing the functions code via require before doing schema inference because
// during watch mode we need it to be registered in the watching system so when the files are
// changed we reload. If the files fail to compile, ts-node will print the diagnostic errors on the
// terminal for us
let runtimeFunctions: RuntimeFunctions | undefined = undefined;
try {
runtimeFunctions = require(functionsFilePath);
} catch (e) {
console.error(`${e}`); // Print the compiler errors produced by ts-node
runtimeFunctions = undefined;
}
// changed we reload. If the files fail to compile, require with throw and the exception will
// result in the diagnostic errors being printed on the terminal for us
let runtimeFunctions: RuntimeFunctions = require(functionsFilePath);;

// If the functions successfully loaded (ie. compiled), let's derive the schema.
// Unfortunately this means we've typechecked everything twice, but that seems unavoidable without
// implementing our own hot-reloading system instead of using ts-node-dev.
if (runtimeFunctions !== undefined) {
const schemaResults = deriveSchema(functionsFilePath);
printCompilerDiagnostics(schemaResults.compilerDiagnostics); // Should never have any of these, since we've already tried compiling the code above
printFunctionIssues(schemaResults.functionIssues);
printRelaxedTypesWarning(schemaResults.functionsSchema);

return {
functionsSchema: schemaResults.functionsSchema,
runtimeFunctions,
}
}
// If the functions did not compile, just have an empty schema, the user will need to correct
// their code before we can derive a schema
else {
return {
functionsSchema: {
functions: {},
objectTypes: {},
scalarTypes: {},
},
runtimeFunctions: {}
}
const schemaResults = deriveSchema(functionsFilePath);
printCompilerDiagnostics(schemaResults.compilerDiagnostics); // Should never have any of these, since we've already tried compiling the code above
printFunctionIssues(schemaResults.functionIssues);
printRelaxedTypesWarning(schemaResults.functionsSchema);

return {
functionsSchema: schemaResults.functionsSchema,
runtimeFunctions,
}
},

Expand Down

0 comments on commit 2711113

Please sign in to comment.