Skip to content

Commit

Permalink
Removed any type usage and added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-chambers committed Apr 16, 2024
1 parent 8873625 commit 522a9f5
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 7 deletions.
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 (result && 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 522a9f5

Please sign in to comment.