Skip to content

Commit a4a0350

Browse files
committed
feat(gate): update prisma migrations and read wasm file in engine
1 parent e634bf5 commit a4a0350

File tree

3 files changed

+69
-62
lines changed

3 files changed

+69
-62
lines changed

typegate/engine/src/runtimes/wasmedge.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use wasmedge_sdk::{
1818
Module,
1919
};
2020

21+
use std::env;
22+
2123
#[derive(Deserialize)]
2224
#[serde(crate = "serde")]
2325
pub struct WasiInput {
@@ -54,8 +56,14 @@ fn param_cast(out: &str, res: &mut Vec<Box<dyn Any + Send + Sync>>) -> Result<St
5456
pub fn op_wasmedge_wasi(#[serde] input: WasiInput) -> Result<String> {
5557
// https://github.com/second-state/wasmedge-rustsdk-examples
5658

57-
let wasm_path = PathBuf::from(input.wasm);
58-
let wasm_binary = common::archive::encode_to_base_64(&wasm_path).unwrap();
59+
let wasm_relative_path = PathBuf::from(input.wasm);
60+
61+
let wasm_absolute_path = match env::current_dir() {
62+
Ok(cwd) => cwd.join(wasm_relative_path),
63+
Err(e) => return Err(anyhow::anyhow!(e)),
64+
};
65+
66+
let wasm_binary = common::archive::encode_to_base_64(&wasm_absolute_path).unwrap();
5967

6068
let bytes = general_purpose::STANDARD.decode(wasm_binary).unwrap();
6169
let module = Module::from_bytes(None, bytes).unwrap();

typegate/src/typegraphs/prisma_migration.json

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,13 @@
1515
"deploy": 14,
1616
"reset": 18
1717
},
18-
"required": [
19-
"diff",
20-
"apply",
21-
"create",
22-
"deploy",
23-
"reset"
24-
]
18+
"required": ["diff", "apply", "create", "deploy", "reset"]
2519
},
2620
{
2721
"type": "function",
2822
"title": "func_10",
2923
"runtime": 1,
30-
"policies": [
31-
0
32-
],
24+
"policies": [0],
3325
"config": {},
3426
"as_id": false,
3527
"input": 2,
@@ -95,9 +87,7 @@
9587
"type": "function",
9688
"title": "func_23",
9789
"runtime": 1,
98-
"policies": [
99-
0
100-
],
90+
"policies": [0],
10191
"config": {},
10292
"as_id": false,
10393
"input": 8,
@@ -147,9 +137,7 @@
147137
"type": "function",
148138
"title": "func_40",
149139
"runtime": 1,
150-
"policies": [
151-
0
152-
],
140+
"policies": [0],
153141
"config": {},
154142
"as_id": false,
155143
"input": 12,
@@ -193,9 +181,7 @@
193181
"type": "function",
194182
"title": "func_51",
195183
"runtime": 1,
196-
"policies": [
197-
0
198-
],
184+
"policies": [0],
199185
"config": {},
200186
"as_id": false,
201187
"input": 15,
@@ -243,9 +229,7 @@
243229
"type": "function",
244230
"title": "func_58",
245231
"runtime": 1,
246-
"policies": [
247-
0
248-
],
232+
"policies": [0],
249233
"config": {},
250234
"as_id": false,
251235
"input": 19,
@@ -366,9 +350,7 @@
366350
"name": "basic",
367351
"protocol": "basic",
368352
"auth_data": {
369-
"users": [
370-
"admin"
371-
]
353+
"users": ["admin"]
372354
}
373355
}
374356
],
@@ -380,6 +362,7 @@
380362
"local_excess": 5
381363
},
382364
"version": "0.0.3",
383-
"random_seed": null
365+
"random_seed": null,
366+
"ref_files": {}
384367
}
385-
}
368+
}

typegraph/python/typegraph/graph/tg_deploy.py

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22
# SPDX-License-Identifier: MPL-2.0
33

44
import json
5-
from base64 import b64encode
5+
import os
6+
import sys
67
from dataclasses import dataclass
78
from typing import Dict, Optional, Union
89
from urllib import request
9-
from typegraph.graph.shared_types import BasicAuth
10-
from typegraph.wit import ArtifactResolutionConfig
10+
11+
from typegraph.gen.exports.utils import QueryDeployParams
1112
from typegraph.gen.types import Err
13+
from typegraph.graph.shared_types import BasicAuth
1214
from typegraph.graph.typegraph import TypegraphOutput
13-
from typegraph.gen.exports.utils import QueryDeployParams
14-
from typegraph.wit import store, wit_utils
15+
from typegraph.wit import ArtifactResolutionConfig, store, wit_utils
1516

1617

1718
@dataclass
@@ -55,23 +56,58 @@ def tg_deploy(tg: TypegraphOutput, params: TypegraphDeployParams) -> DeployResul
5556
headers["Authorization"] = params.auth.as_header_value()
5657
serialized = tg.serialize(params.artifacts_config)
5758

59+
res = wit_utils.gql_deploy_query(
60+
store,
61+
params=QueryDeployParams(
62+
tg=serialized,
63+
secrets=[(k, v) for k, v in (params.secrets or {}).items()],
64+
),
65+
)
66+
67+
if isinstance(res, Err):
68+
raise Exception(res.value)
69+
70+
req = request.Request(
71+
url=url,
72+
method="POST",
73+
headers=headers,
74+
data=res.value.encode(),
75+
)
76+
77+
result = DeployResult(
78+
serialized=serialized,
79+
typegate=handle_response(request.urlopen(req).read().decode()),
80+
)
81+
5882
# upload the referred files
59-
ref_files = core.get_ref_files(store)
60-
if isinstance(ref_files, Err):
61-
raise Exception(ref_files.value)
83+
# print("***************A")
84+
ref_files = {
85+
"662307922d7b879a17ce206d57db63b27630a7b4a8de2455a5730fb4bfe07a5c": "file:rust.wasm"
86+
}
87+
# if isinstance(ref_files, Err):
88+
# raise Exception(ref_files.value)
6289

6390
# TODO: fetch all the upload by one request
64-
get_upload_url = params.base_url + sep + "get-upload-url"
65-
for file_hash, file_path in ref_files.value:
91+
get_upload_url = params.base_url + sep + tg.name + "/get-upload-url"
92+
for file_hash, file_path in ref_files.items():
93+
prefix = "file:"
94+
if not file_path.startswith(prefix):
95+
raise Exception(f"file path {file_path} should start with {prefix}")
96+
97+
curr_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
98+
file_path = os.path.join(curr_dir, file_path[len(prefix) :])
99+
66100
with open(file_path, "rb") as file:
67101
file_content = file.read()
68102
artifact = UploadArtifactMeta(
69-
name=file.name,
103+
name=os.path.basename(file_path),
70104
file_hash=file_hash,
71105
file_size_in_bytes=len(file_content),
72106
)
107+
108+
artifact_json = json.dumps(artifact.__dict__).encode()
73109
req = request.Request(
74-
url=get_upload_url, method="GET", headers=headers, data=artifact
110+
url=get_upload_url, method="PUT", headers=headers, data=artifact_json
75111
)
76112

77113
response = handle_response(request.urlopen(req).read().decode())
@@ -84,27 +120,7 @@ def tg_deploy(tg: TypegraphOutput, params: TypegraphDeployParams) -> DeployResul
84120
)
85121
response = request.urlopen(upload_req)
86122

87-
res = wit_utils.gql_deploy_query(
88-
store,
89-
params=QueryDeployParams(
90-
tg=serialized,
91-
secrets=[(k, v) for k, v in (params.secrets or {}).items()],
92-
),
93-
)
94-
95-
if isinstance(res, Err):
96-
raise Exception(res.value)
97-
98-
req = request.Request(
99-
url=url,
100-
method="POST",
101-
headers=headers,
102-
data=res.value.encode(),
103-
)
104-
return DeployResult(
105-
serialized=serialized,
106-
typegate=handle_response(request.urlopen(req).read().decode()),
107-
)
123+
return result
108124

109125

110126
def tg_remove(tg: TypegraphOutput, params: TypegraphRemoveParams):

0 commit comments

Comments
 (0)