Skip to content

Commit

Permalink
feat(sdk): change rest queries interface (#444)
Browse files Browse the repository at this point in the history
  • Loading branch information
afmika committed Oct 10, 2023
1 parent 0f8f272 commit bf5df69
Show file tree
Hide file tree
Showing 34 changed files with 168 additions and 140 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dev/lock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ dev:
WASMEDGE_VERSION: 0.12.1
TYPEGRAPH_VERSION: 0.0.2
PRISMA_VERSION: 5.4.0-dev.31
METATYPE_VERSION: 0.2.1
METATYPE_VERSION: 0.2.2
WASM_OPT_VERSION: 0.114.1
TAGLINE: >-
Declarative API development platform. Build serverless backends with
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/deno/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ typegraph("test-multiple-runtimes", (g) => {
const python = new PythonRuntime();

g.expose({
add: t.func(
add: python.fromLambda(
t.struct({ "first": t.float(), "second": t.float() }),
t.float(),
python.fromLambda("lambda x: x['first'] + x['second']"),
{ code: "lambda x: x['first'] + x['second']" },
).withPolicy(pub),
multiply: deno.func(
t.struct({ "first": t.float(), "second": t.float() }),
Expand Down
2 changes: 1 addition & 1 deletion examples/templates/python/compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
typegate:
image: ghcr.io/metatypedev/typegate:v0.2.1
image: ghcr.io/metatypedev/typegate:v0.2.2
restart: always
ports:
- "7890:7890"
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[tool.poetry]
name = "example"
version = "0.2.1"
version = "0.2.2"
description = ""
authors = []
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.8,<4.0"
typegraph = "0.2.1"
typegraph = "0.2.2"

[build-system]
requires = ["poetry-core"]
Expand Down
2 changes: 1 addition & 1 deletion libs/common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "common"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion libs/macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "macros"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion libs/typescript/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typescript"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion libs/xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xtask"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion meta-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "meta-cli"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

description = "Declarative API development platform. Build serverless backends with zero-trust and less code, no matter where and how your (legacy) systems are."
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "metatype"
version = "0.2.1"
version = "0.2.2"
description = ""
authors = []

Expand Down
2 changes: 1 addition & 1 deletion typegate/native/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "native"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[lib]
Expand Down
18 changes: 18 additions & 0 deletions typegate/tests/rest/custom/custom_loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
// SPDX-License-Identifier: Elastic-2.0

import { dirname } from "std/path/dirname.ts";
import { resolve } from "std/path/resolve.ts";
import { join } from "std/path/join.ts";
import { fromFileUrl } from "std/path/from_file_url.ts";

const customDir = resolve(dirname(fromFileUrl(import.meta.url)));

export const endpoints = {
mutation: Deno.readTextFileSync(
join(customDir, "m.gql"),
),
query: Deno.readTextFileSync(
join(customDir, "q.graphql"),
),
};
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions typegate/tests/rest/custom_dir/test.gql

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { Policy, t, typegraph } from "@typegraph/deno/src/mod.ts";
import { DenoRuntime } from "@typegraph/deno/src/runtimes/deno.ts";
import { endpoints } from "./custom/custom_loader.ts";

const user = t.struct({
id: t.integer(),
Expand Down Expand Up @@ -49,6 +50,9 @@ typegraph("rest", (g) => {
},
).withPolicy(pub);

g.rest(endpoints.mutation);
g.rest(endpoints.query);

g.expose({
postFromUser,
readPost,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from typegraph import t, typegraph, Graph, Policy
from typegraph import Graph, Policy, t, typegraph
from typegraph.runtimes.deno import DenoRuntime


@typegraph(dynamic=False, folder="custom_dir")
@typegraph(dynamic=False)
def custom(g: Graph):
deno = DenoRuntime()
pub = Policy.public()

g.rest(
"""
query ping {
ping
}
"""
)

ping = deno.func(
t.struct({}),
t.integer(),
Expand Down
4 changes: 2 additions & 2 deletions typegate/tests/rest/rest_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { gql, Meta, rest } from "../utils/mod.ts";
import { RestSchemaGenerator } from "../../src/transports/rest/rest_schema_generator.ts";

Meta.test("Rest queries in Python", async (t) => {
const e = await t.engine("rest/custom.py");
const e = await t.engine("rest/rest_simple.py");

await t.should("work with simple rest requests", async () => {
await rest.get("ping")
Expand All @@ -29,7 +29,7 @@ Meta.test("Rest queries in Python", async (t) => {
});

Meta.test("Rest queries in Deno", async (t) => {
const e = await t.engine("rest/rest.ts");
const e = await t.engine("rest/rest_custom_loader.ts");

await t.should("work with simple rest requests", async () => {
await rest.get("get_post_id?id=1")
Expand Down
2 changes: 1 addition & 1 deletion typegate/tests/runtimes/wasmedge/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[lib]
Expand Down
2 changes: 1 addition & 1 deletion typegraph/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typegraph_core"
version = "0.2.1"
version = "0.2.2"
edition = "2021"

[lib]
Expand Down
25 changes: 25 additions & 0 deletions typegraph/core/src/global_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::runtimes::{DenoMaterializer, Materializer, MaterializerDenoModule, Ru
use crate::types::{Struct, Type, TypeFun, TypeId, WrapperTypeData};
use crate::wit::core::{Policy as CorePolicy, PolicyId, RuntimeId};
use crate::wit::runtimes::{Effect, MaterializerDenoPredefined, MaterializerId};
use graphql_parser::parse_query;
use indexmap::IndexMap;
use std::rc::Rc;
use std::{cell::RefCell, collections::HashMap};
Expand Down Expand Up @@ -46,6 +47,7 @@ pub struct Store {
prisma_migration_runtime: RuntimeId,
typegate_runtime: RuntimeId,
typegraph_runtime: RuntimeId,
graphql_endpoints: Vec<String>,
}

impl Store {
Expand Down Expand Up @@ -281,6 +283,29 @@ impl Store {
mat
}
}

pub fn add_graphql_endpoint(graphql: String) -> Result<()> {
with_store_mut(|s| {
let ast = parse_query::<&str>(&graphql).map_err(|e| e.to_string())?;
let endpoints = ast
.definitions
.into_iter()
.map(|op| {
format!("{}", op)
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
})
.collect::<Vec<_>>();

s.graphql_endpoints.extend(endpoints);
Ok(())
})
}

pub fn get_graphql_endpoints() -> Vec<String> {
with_store(|s| s.graphql_endpoints.clone())
}
}

impl TypeId {
Expand Down
20 changes: 0 additions & 20 deletions typegraph/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,6 @@ pub mod host {
pub use metatype::typegraph::abi;
}

// native stubs to make the test compilation work
#[cfg(not(feature = "wasm"))]
pub mod host {
pub mod abi {
pub fn log(message: &str) {
println!("{}", message);
}
pub fn glob(_pattern: &str, _exts: &[String]) -> Result<Vec<String>, String> {
Ok(vec![])
}
pub fn read_file(path: &str) -> Result<String, String> {
Ok(path.to_string())
}
pub fn write_file(_path: &str, _data: &str) -> Result<(), String> {
Ok(())
}
}
}

pub struct Lib {}

impl TypeBase {
Expand Down Expand Up @@ -366,7 +347,6 @@ mod tests {
Self {
name: "".to_string(),
dynamic: None,
folder: None,
path: ".".to_string(),
prefix: None,
cors: Cors {
Expand Down

0 comments on commit bf5df69

Please sign in to comment.