Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed functions that return null causing crashes #31

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});