Skip to content

Commit

Permalink
feat(deno/sdk): native function/lambda support for typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-0acf4 committed Feb 23, 2024
1 parent bbf0b95 commit 7f25865
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 30 deletions.
21 changes: 0 additions & 21 deletions typegate/tests/runtimes/deno/deno_static.ts

This file was deleted.

22 changes: 17 additions & 5 deletions typegate/tests/runtimes/deno/deno_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,34 @@ Meta.test("Deno runtime: use local imports", async (t) => {
});

Meta.test("Deno runtime with typescript", async (t) => {
const e = await t.engine("runtimes/deno/deno_static.ts");
const e = await t.engine("runtimes/deno/deno_typescript.ts");
await t.should("work with static values", async () => {
await gql`
query {
simpleStatic
structStatic {
static {
a
}
}
`.expectData({
simpleStatic: "One!",
structStatic: {
static: {
a: "Hello World",
},
}).on(e);
});

await t.should("work with native tyepscript code", async () => {
await gql`
query {
hello(name: "World")
helloFn(name: "World!")
}
`
.expectData({
hello: "Hello World",
helloFn: "Hello World!",
})
.on(e);
});
});

Meta.test("Deno runtime: file name reloading", async (t) => {
Expand Down
31 changes: 31 additions & 0 deletions typegate/tests/runtimes/deno/deno_typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
// SPDX-License-Identifier: Elastic-2.0

import { Policy, t, typegraph } from "@typegraph/sdk/index.js";
import { DenoRuntime } from "@typegraph/sdk/runtimes/deno.js";

const hello = ({ name }: any) => `Hello ${name}`;
function helloFn({ name }: any) {
return `Hello ${name}`;
}

typegraph("test-deno-static", (g: any) => {
const deno = new DenoRuntime();
const pub = Policy.public();

g.expose({
static: deno.static(t.struct({ a: t.string() }), {
a: "Hello World",
}).withPolicy(pub),
hello: deno.func(
t.struct({ name: t.string() }),
t.string(),
{ code: hello },
).withPolicy(pub),
helloFn: deno.func(
t.struct({ name: t.string() }),
t.string(),
{ code: helloFn },
).withPolicy(pub),
});
});
16 changes: 12 additions & 4 deletions typegraph/node/sdk/src/runtimes/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface PredefinedFuncMat extends Materializer {
}

export interface DenoFunc {
code: string;
code: string | Function;
secrets?: Array<string>;
effect?: Effect;
}
Expand All @@ -38,6 +38,10 @@ export interface DenoImport {
effect?: Effect;
}

function stringifyFn(code: string | Function) {
return typeof code == "function" ? code.toString() : code;
}

export class DenoRuntime extends Runtime {
constructor() {
super(runtimes.getDenoRuntime());
Expand All @@ -51,10 +55,11 @@ export class DenoRuntime extends Runtime {
out: O,
{ code, secrets = [], effect = fx.read() }: DenoFunc,
): t.Func<I, O, FunMat> {
const matId = runtimes.registerDenoFunc({ code, secrets }, effect);
const source = stringifyFn(code);
const matId = runtimes.registerDenoFunc({ code: source, secrets }, effect);
const mat: FunMat = {
_id: matId,
code,
code: source,
secrets,
effect,
};
Expand Down Expand Up @@ -123,7 +128,10 @@ export class DenoRuntime extends Runtime {

return Policy.create(
name,
runtimes.registerDenoFunc(params, fx.read()),
runtimes.registerDenoFunc(
{ ...params, code: stringifyFn(params.code) },
fx.read(),
),
);
}

Expand Down
26 changes: 26 additions & 0 deletions website/static/specs/0.0.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -1241,6 +1241,24 @@
"$ref": "#/definitions/InjectionData_for_String"
}
}
},
{
"type": "object",
"required": [
"data",
"source"
],
"properties": {
"source": {
"type": "string",
"enum": [
"random"
]
},
"data": {
"$ref": "#/definitions/InjectionData_for_String"
}
}
}
]
},
Expand Down Expand Up @@ -2363,6 +2381,14 @@
},
"version": {
"type": "string"
},
"random_seed": {
"type": [
"integer",
"null"
],
"format": "uint32",
"minimum": 0.0
}
}
},
Expand Down

0 comments on commit 7f25865

Please sign in to comment.