Skip to content

Commit

Permalink
Fixed functions that return null causing crashes (#31)
Browse files Browse the repository at this point in the history
---------
Co-authored-by: Daniel Chambers <daniel@hasura.io>
  • Loading branch information
danieljharvey authored Apr 16, 2024
1 parent 089e447 commit e2e59f5
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Changes to be included in the next upcoming release
- Support for NDC Spec v0.1.2 via the NDC TypeScript SDK v4.4.0 ([#29](https://github.com/hasura/ndc-nodejs-lambda/pull/29)).
- Built-in scalar types that support equality now define it in the NDC schema.
- Built-in scalar types now have an explicit type representation defined in the NDC schema.
- Fixed functions that return null causing crashes ([#31](https://github.com/hasura/ndc-nodejs-lambda/pull/31))

## [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: 3 additions & 3 deletions ndc-lambda-sdk/src/execution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ function coerceArgumentValue(value: unknown, type: schema.TypeReference, valuePa
async function invokeFunction(func: Function, preparedArgs: unknown[], functionName: string): Promise<unknown> {
try {
return await withActiveSpan(tracer, `Function: ${functionName}`, async () => {
const result = func.apply(undefined, preparedArgs);
const result: unknown = func.apply(undefined, preparedArgs);
// Await the result if it is a promise
if (typeof result === "object" && 'then' in result && typeof result.then === "function") {
return await result;
if (result !== null && typeof result === "object" && "then" in result && typeof result.then === "function") {
return await (result as PromiseLike<unknown>);
}
return result;
}, { [FUNCTION_NAME_SPAN_ATTR_NAME]: functionName });
Expand Down
84 changes: 80 additions & 4 deletions ndc-lambda-sdk/test/execution/execute-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it } from "mocha";
import { assert, expect } from "chai";
import * as sdk from "@hasura/ndc-sdk-typescript"
import { executeQuery } from "../../src/execution";
import { FunctionNdcKind, FunctionsSchema } from "../../src/schema";
import { FunctionNdcKind, FunctionsSchema, NullOrUndefinability } from "../../src/schema";
import { sleep } from "../../src/util";

describe("execute query", function() {
Expand Down Expand Up @@ -389,9 +389,13 @@ describe("execute query", function() {
parallelDegree: null,
arguments: [],
resultType: {
type: "named",
kind: "scalar",
name: "String"
type: "nullable",
nullOrUndefinability: NullOrUndefinability.AcceptsNullOnly,
underlyingType: {
type: "named",
kind: "scalar",
name: "String"
}
}
}
},
Expand Down Expand Up @@ -493,5 +497,77 @@ describe("execute query", function() {
]);
assert.equal(functionCallCount, 1);
});

it("function can return null", async function() {
let functionCallCount = 0;
const runtimeFunctions = {
"theFunction": () => {
functionCallCount++;
return null;
}
};
const queryRequest: sdk.QueryRequest = {
collection: "theFunction",
query: {
fields: {
"__value": {
type: "column",
column: "__value"
}
}
},
arguments: {},
collection_relationships: {}
};

const result = await executeQuery(queryRequest, functionSchema, runtimeFunctions);
assert.deepStrictEqual(result, [
{
aggregates: null,
rows: [
{
"__value": null,
}
]
}
]);
assert.equal(functionCallCount, 1);
});

it("async function can return null", async function() {
let functionCallCount = 0;
const runtimeFunctions = {
"theFunction": async () => {
functionCallCount++;
return null;
}
};
const queryRequest: sdk.QueryRequest = {
collection: "theFunction",
query: {
fields: {
"__value": {
type: "column",
column: "__value"
}
}
},
arguments: {},
collection_relationships: {}
};

const result = await executeQuery(queryRequest, functionSchema, runtimeFunctions);
assert.deepStrictEqual(result, [
{
aggregates: null,
rows: [
{
"__value": null,
}
]
}
]);
assert.equal(functionCallCount, 1);
});
});
});

0 comments on commit e2e59f5

Please sign in to comment.