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

fix(template/node): update sdk version to 0.3.2 + add deno.static #558

Merged
merged 5 commits into from
Jan 18, 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
117 changes: 58 additions & 59 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dev/lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ dev:
TYPEGRAPH_VERSION: 0.0.3
PRISMA_VERSION: 5.5.2
METATYPE_VERSION: 0.3.2
PUBLISHED_VERSION: 0.2.4
PUBLISHED_VERSION: 0.3.2
WASM_OPT_VERSION: 0.116.0
MOLD_VERSION: v2.4.0
CMAKE_VERSION: 3.28.0-rc6
Expand Down
6 changes: 3 additions & 3 deletions examples/templates/deno/api/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Policy, t, typegraph } from "npm:@typegraph/sdk@0.2.4";
import { PythonRuntime } from "npm:@typegraph/sdk@0.2.4/runtimes/python";
import { DenoRuntime } from "npm:@typegraph/sdk@0.2.4/runtimes/deno";
import { Policy, t, typegraph } from "npm:@typegraph/sdk@0.3.2/index.js";
import { PythonRuntime } from "npm:@typegraph/sdk@0.3.2/runtimes/python.js";
import { DenoRuntime } from "npm:@typegraph/sdk@0.3.2/runtimes/deno.js";

typegraph("example", (g) => {
const pub = Policy.public();
Expand Down
6 changes: 3 additions & 3 deletions examples/templates/node/api/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Policy, t, typegraph } from "@typegraph/sdk";
import { DenoRuntime } from "@typegraph/sdk/runtimes/deno";
import { PythonRuntime } from "@typegraph/sdk/runtimes/python";
import { Policy, t, typegraph } from "@typegraph/sdk/index.js";
import { DenoRuntime } from "@typegraph/sdk/runtimes/deno.js";
import { PythonRuntime } from "@typegraph/sdk/runtimes/python.js";

typegraph("example", (g) => {
const pub = Policy.public();
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dev": "MCLI_LOADER_CMD='npm run load-tgraph --silent' meta dev"
},
"dependencies": {
"@typegraph/sdk": "^0.2.4"
"@typegraph/sdk": "^0.3.2"
},
"devDependencies": {
"tsx": "^3.13.0",
Expand Down
1 change: 1 addition & 0 deletions examples/templates/node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"strict": true,
"moduleResolution": "node16",
"module": "NodeNext",
"esModuleInterop": true
}
}
11 changes: 8 additions & 3 deletions typegate/tests/e2e/templates/templates_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@ const modifiers: Record<string, (dir: string) => Promise<void> | void> = {
const version = await get_version();
console.log(version);
for await (const f of expandGlob("**/*.ts", { root: dir })) {
const data = await Deno.readTextFile(f.path);
const level = f.path.replace(projectDir, "").split("/").length - 1;
// FIXME: deno is unable to map the types from .d.ts files when used locally
const data = (await Deno.readTextFile(f.path)).replace(
/\(\s*g\s*\)/,
"(g: any)",
);
const level = f.path.replace(projectDir, "").split("/").length - 2;
const newData = data.replaceAll(
`npm:@typegraph/sdk@${version}`,
`${Array(level).fill("..").join("/")}/typegraph/deno/src`,
`${Array(level).fill("..").join("/")}/typegraph/node/sdk/dist`,
);

await Deno.writeTextFile(f.path, newData);
}
},
Expand Down
21 changes: 21 additions & 0 deletions typegate/tests/runtimes/deno/deno_static.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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";

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

g.expose({
simpleStatic: deno.static(
t.either([t.string(), t.integer()]),
"One!",
).withPolicy(pub),
structStatic: deno.static(t.struct({ "a": t.string() }), {
a: "Hello World",
})
.withPolicy(pub),
});
});
19 changes: 19 additions & 0 deletions typegate/tests/runtimes/deno/deno_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,25 @@ 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");
await t.should("work with static values", async () => {
await gql`
query {
simpleStatic
structStatic {
a
}
}
`.expectData({
simpleStatic: "One!",
structStatic: {
a: "Hello World",
},
}).on(e);
});
});

Meta.test("Deno runtime: file name reloading", async (t) => {
const load = async (value: number) => {
Deno.env.set("DYNAMIC", join("dynamic", `${value}.ts`));
Expand Down
15 changes: 15 additions & 0 deletions typegraph/node/sdk/src/runtimes/deno.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,21 @@ export class DenoRuntime extends Runtime {
);
}

static<
P extends t.Typedef,
>(out: P, value: any) {
const mat = {
_id: runtimes.registerDenoStatic({
value: JSON.stringify(value),
}, out._id),
};
return t.func(
t.struct({}),
out,
mat,
);
}

policy(name: string, _code: string): Policy;
policy(name: string, data: Omit<DenoFunc, "effect">): Policy;
policy(name: string, data: string | Omit<DenoFunc, "effect">): Policy {
Expand Down
11 changes: 11 additions & 0 deletions typegraph/node/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,17 @@ export class Func<
);
}

rate(
inp: { calls: boolean; weight?: number },
): Func<P, I, O, M> {
return func(
this.inp,
this.out,
this.mat,
{ rateCalls: inp.calls ?? false, rateWeight: inp.weight },
);
}

static fromTypeFunc(data: FuncParams) {
return func(
new Typedef(data.inp, {}) as Struct<{ [key: string]: Typedef }>,
Expand Down
Loading