Skip to content

Commit

Permalink
fix(docs): Fix programmatic deployment guides (#762)
Browse files Browse the repository at this point in the history
<!--
Pull requests are squashed and merged using:
- their title as the commit message
- their description as the commit body

Having a good title and description is important for the users to get
readable changelog.
-->

- [x] Fix typo
- [x] fix/test tg deploy
- [x] fix/test tg remove

<!-- 1. Explain WHAT the change is about -->


[MET-587](https://linear.app/metatypedev/issue/MET-587/docs-fix-programmatic-deployment-guides)

<!-- 2. Explain WHY the change cannot be made simpler -->

-

<!-- 3. Explain HOW users should update their code -->

#### Migration notes

_No Migration Needed_

...

- [ ] The change comes with new or modified tests
- [ ] Hard-to-understand functions have explanatory comments
- [ ] End-user documentation is updated to reflect the change
  • Loading branch information
destifo authored Jun 21, 2024
1 parent fc6a7bc commit f67ea81
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 82 deletions.
146 changes: 66 additions & 80 deletions website/docs/guides/programmatic-deployment/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ Common use cases:


## Deploy typegraphs
This can be done using the `tgRemove`/`tg_remove` function.\
This can be done using the `tgDeploy`/`tg_deploy` function.\
You are required to provide the configurations and also handle migrations by yourself (if any).

<SDKTabs>
<TabItem value="python">
```python
import os
from os import path
from typegraph import Graph, Policy, t, typegraph

from typegraph.gen.exports.core import MigrationAction
from typegraph.graph.shared_types import BasicAuth
from typegraph.graph.tg_deploy import (
TypegraphDeployParams,
tg_deploy,
TypegateConnectionOptions,
)
from typegraph.runtimes.deno import DenoRuntime

from typegraph.wit import ArtifactResolutionConfig, MigrationAction, MigrationConfig
from typegraph.utils import unpack_tarb64
from typegraph.graph.tg_deploy import BasicAuth, TypegraphDeployParams, tg_deploy
from typegraph import Graph, Policy, t, typegraph

# Your typegraph
@typegraph()
Expand All @@ -42,41 +48,31 @@ def example(g: Graph):
)

# Configure your deployment
tg = example()

config_params = MigrationConfig(
migration_dir=path.join("prisma-migrations", tg.name),
global_action=MigrationAction(
create=True,
reset=True # allow destructive migrations
),
runtime_actions=None,
)
artifacts_config = ArtifactResolutionConfig(
prisma_migration=config_params,
prefix=None,
dir=None, # artifacts are resolved relative to this path
disable_artifact_resolution=None,
)
config = TypegraphDeployParams(
base_url="<TYPEGATE_URL>",
auth=BasicAuth(username="<USERNAME>", password="<PASSWORD>"),
artifacts_config=artifacts_config,
secrets={"POSTGRES": "<DB_URL>"},
)

# Deploy to typegate
res = tg_deploy(tg, config)
def deploy():
cwd = os.getcwd()

config: TypegraphDeployParams = TypegraphDeployParams(
typegate=TypegateConnectionOptions(
url="<TYPEGATE_URL>",
auth=BasicAuth("<USERNAME>", "<PASSWORD>")
),
typegraph_path=os.path.join(cwd, "path/to/typegraph"),
prefix="<prefix>",
secrets={},
migrations_dir=path.join("prisma-migrations", example.name),
migration_actions=None,
default_migration_action=MigrationAction(
apply=True,
reset=True, # allow destructive migrations
create=True
),
),

migrations = res.typegate["data"]["addTypegraph"]["migrations"] or []
for item in migrations:
base_dir = artifacts_config.prisma_migration.migration_dir
# Convention, however if migration_dir is absolute then you might want to use that instead
# cwd + tg_name + runtime_name
full_path = path.join(base_dir, item["runtime"])
unpack_tarb64(item["migrations"], full_path)
print(f"Unpacked migrations at {full_path}")
# Deploy to typegate
result = tg_deploy(example, config) # pass your typegraph function name
return result

res = deploy()
```
</TabItem>
<TabItem value="typescript">
Expand All @@ -103,44 +99,21 @@ const tg = await typegraph("example", (g) => {
});

// 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,
};
typegate: { url: "<TYPEGATE_URL>", auth: new BasicAuth("<USERNAME>", "<PASSWORD>"), },
typegraphPath: "<absolute/path/to/typegraph>",
prefix: "<prefx>",
secrets: {},
migrationsDir: path.join("prisma-migrations", tg.name),
defaultMigrationAction: {
create: true,
reset: true, // allow destructive migrations
},
}

// 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);

const deployResult = await tgDeploy(tg, config);
```
</TabItem>
</SDKTabs>
Expand All @@ -153,35 +126,48 @@ Similarly to the above, you can undeploy typegraphs using the `tgRemove`/`tg_rem
<TabItem value="python">
```python
# ..
from typegraph.graph.tg_deploy import BasicAuth, TypegraphRemoveParams, tg_remove
from typegraph.graph.tg_deploy import (
TypegateConnectionOptions,
TypegraphDeployParams,
TypegraphRemoveParams,
tg_remove,
)

# Your typegraph
@typegraph()
def example(g: Graph):
# ..

tg = example()
res = tg_remove(tg, TypegraphRemoveParams(
base_url="<TYPEGATE_URL>",
auth=BasicAuth(username="<USERNAME>", password="<PASSWORD>"),
))
def remove():
result = tg_remove(
example,
params=TypegraphRemoveParams(
typegate=TypegateConnectionOptions(
url = "<TYPEGATE_URL>",
auth = BasicAuth("<USERNAME>", "<PASSWORD>")
)
)
)

return result

res = remove()
# Response from typegate
print(res.typegate)
```
</TabItem>
<TabItem value="typescript">
```typescript
// ..
import { BasicAuth, tgDeploy } from "@typegraph/sdk/tg_deploy.js";
import { BasicAuth, tgRemove } from "@typegraph/sdk/tg_deploy.js";

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

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

Expand Down
5 changes: 4 additions & 1 deletion website/shared/projects/tab-first-project-python.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ With these simple steps, you were able to build a basic backend with database ca
{require("../../../examples/typegraphs/quick-start-project.py").content}
</CodeBlock>

You are almost there to test your first `Metatype` application. You now need to spin a [Tyepgate](/docs/reference/typegate) and deploy your typegraph to the instance. You can leverage the embedded typegate that comes with the _Meta_CLI_. To run the embedded typegate, you can follow this [reference](/docs/reference/meta-cli/embedded-typegate).
You are almost there to test your first `Metatype` application. You now need to spin a [Tyepgate](/docs/reference/typegate) and deploy your typegraph to the instance. You can leverage the embedded typegate that comes with the _Meta_CLI_. To run the [embedded typegate](/docs/reference/meta-cli/embedded-typegate), execute the following command from your terminal.
```shell
meta typegate
```

Once you started your typegate instance using one of the available choice, if you open [localhost:7890](http://localhost:7890) in your browser, you will get a webpage similar to this one.

Expand Down
4 changes: 3 additions & 1 deletion website/shared/projects/tab-first-project-ts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ With these three simple steps, you were able to build a basic backend with datab
}
</CodeBlock>

You are almost there to test your first `Metatype` application. You now need to spin a [Tyepgate](/docs/reference/typegate) and deploy your typegraph to the instance. You can leverage the embedded typegate that comes with the _Meta_CLI_. To run the embedded typegate, you can follow this [reference](/docs/reference/meta-cli/embedded-typegate).
You are almost there to test your first `Metatype` application. You now need to spin a [Tyepgate](/docs/reference/typegate) and deploy your typegraph to the instance. You can leverage the embedded typegate that comes with the _Meta_CLI_. To run the [embedded typegate](/docs/reference/meta-cli/embedded-typegate), execute the following command from your terminal.
```shell
meta typegate

Once you started your typegate instance using one of the available choice, if you open [localhost:7890](http://localhost:7890) in your browser, you will get a webpage similar to this one.

Expand Down

0 comments on commit f67ea81

Please sign in to comment.