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

feat(deno/sdk): native function embedding in typescript #598

Merged
merged 4 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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 as string).toLowerCase()}`;
}

typegraph("test-deno-tyepscript", (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),
});
});
32 changes: 28 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;
Natoandro marked this conversation as resolved.
Show resolved Hide resolved
secrets?: Array<string>;
effect?: Effect;
}
Expand All @@ -38,6 +38,26 @@ export interface DenoImport {
effect?: Effect;
}

function stringifyFn(code: string | Function) {
if (typeof code == "function") {
const source = code.toString();
const namedFnMatch = source.match(/function\s*(\*?\s*[a-zA-Z0-9_]+)/);
if (namedFnMatch) {
const [, name] = namedFnMatch;
if (name.replace(/s/g, "").startsWith("*")) {
michael-0acf4 marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Generator function "${name}" not supported`);
}
if (/function\s[a-zA-Z0-9_]+\(\) { \[native code\] }/.test(source)) {
throw new Error(
`"${name}" is not supported as it is a native function`,
);
}
}
return source;
}
return code;
}

export class DenoRuntime extends Runtime {
constructor() {
super(runtimes.getDenoRuntime());
Expand All @@ -51,10 +71,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 +144,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
Loading