Skip to main content

Programmatic deployment

The SDKs are complete enough to enable deploying typegraphs without using meta cli, the later being built as a convenience tool for everyday use.

Common use cases:

  • Testing
  • Manage everything programmatically

Deploy typegraphs

This can be done using the tgRemove/tg_remove function.
You are required to provide the configurations and also handle migrations by yourself (if any).

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

import { wit_utils } from "@typegraph/sdk/wit.js";
import { BasicAuth, tgDeploy } from "@typegraph/sdk/tg_deploy.js";

// Your typegraph
const tg = await typegraph("example", (g) => {
const deno = new DenoRuntime();
const pub = Policy.public();

g.expose({
sayHello: deno.import(
t.struct({ name: t.string() }),
t.string(),
{ module: "path/to/say_hello.ts", name: "sayHello" },
),
}, pub);
});

// Configure your deployment
const artifactsConfig = {
prismaMigration: {
globalAction: {
create: true,
reset: true, // allow destructive migrations
},
migrationDir: path.join("prisma-migrations", tg.name),
},
// dir: "." // artifacts are resolved relative to this path
};

const config = {
baseUrl: "<TYPEGATE_URL>",
auth: new BasicAuth("<USERNAME>", "<PASSWORD>");,
secrets: { POSTGRES: "<DB_URL>" },
artifactsConfig,
};

// Deploy to typegate
tgDeploy(tg, config).then(({ typegate }) => {
const selection = typegate?.data?.addTypegraph;
if (selection) {
const { migrations, messages } = selection;
migrations.map(({ runtime, migrations }) => {
// Convention, however if migrationDir is absolute then you might want to use that instead
// cwd + tg_name
const baseDir = artifactsConfig.prismaMigration.migrationDir;
// cwd + tg_name + runtime_name
const fullPath = path.join(baseDir, runtime);
wit_utils.unpackTarb64(migrations, fullPath);
console.log(`Unpacked migrations at ${fullPath}`);
});
} else {
throw new Error(JSON.stringify(typegate));
}
})
.catch(console.error);

Undeploy typegraphs

Similarly to the above, you can undeploy typegraphs using the tgRemove/tg_remove function.

// ..
import { BasicAuth, tgDeploy } from "@typegraph/sdk/tg_deploy.js";

// Your typegraph
const tg = await typegraph("example", (g) => {
// ..
});

const { typegate } = await tgRemove(tg, {
baseUrl: "<TYPEGATE_URL>",
auth: new BasicAuth("<USERNAME>", "<PASSWORD>"),
});

// Response from typegate
console.log(typegate);