Skip to content

Commit 73871a7

Browse files
authored
Merge branch 'main' into upload/s3
2 parents 5a7584a + 18128b6 commit 73871a7

File tree

24 files changed

+1746
-1277
lines changed

24 files changed

+1746
-1277
lines changed

examples/typegraphs/http-runtime.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# skip:start
2+
from typegraph import typegraph, Policy, t, Graph
3+
from typegraph.graph.params import Cors
4+
from typegraph.runtimes import HttpRuntime
5+
6+
7+
# skip:end
8+
@typegraph(
9+
# skip:next-line
10+
cors=Cors(allow_origin=["https://metatype.dev", "http://localhost:3000"]),
11+
)
12+
def http_example(g: Graph):
13+
pub = Policy.public()
14+
15+
facts = HttpRuntime("https://uselessfacts.jsph.pl/api/v2/facts")
16+
17+
g.expose(
18+
pub,
19+
facts=facts.get(
20+
"/random",
21+
t.struct({"language": t.enum(["en", "de"])}),
22+
t.struct(
23+
{
24+
"id": t.string(),
25+
"text": t.string(),
26+
"source": t.string(),
27+
"source_url": t.string(),
28+
"language": t.string(),
29+
"permalink": t.string(),
30+
}
31+
),
32+
),
33+
facts_as_text=facts.get(
34+
"/random",
35+
t.struct(
36+
{
37+
"header_accept": t.string().set("text/plain"),
38+
"language": t.enum(["en", "de"]),
39+
}
40+
),
41+
t.string(),
42+
header_prefix="header_",
43+
),
44+
)

examples/typegraphs/random-field.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typegraph import typegraph, Policy, t, Graph
2+
from typegraph.runtimes.deno import DenoRuntime
3+
4+
# skip:start
5+
from typegraph.graph.params import Cors
6+
7+
# skip:end
8+
import time
9+
10+
11+
@typegraph(
12+
# skip:start
13+
cors=Cors(allow_origin=["https://metatype.dev", "http://localhost:3000"]),
14+
name="random-field",
15+
# skip:end
16+
)
17+
def roadmap(g: Graph):
18+
deno = DenoRuntime()
19+
pub = Policy.public()
20+
21+
bonus = t.list(t.enum(["+1 gold", "+1 metal"]))
22+
daily_bonus = t.struct(
23+
{
24+
"performance": t.integer(),
25+
"bonus": bonus.from_random(), # this field is now generated randomly
26+
}
27+
)
28+
29+
# set a custom seed
30+
custom = int(round(time.time() * 1000)) % 1000
31+
g.configure_random_injection(seed=custom)
32+
33+
g.expose(
34+
pub,
35+
get_bonus=deno.func(
36+
daily_bonus,
37+
t.string(),
38+
code="""
39+
({ performance, bonus }) => `Daily bonus: ${
40+
(performance > 100 ? bonus : ['none']).join(', ')
41+
}`;
42+
""",
43+
),
44+
)

examples/typegraphs/roadmap-random.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from typegraph import typegraph, Policy, t, Graph
22
from typegraph.runtimes.random import RandomRuntime
3+
4+
# skip:start
35
from typegraph.graph.params import Cors
6+
# skip:end
47

58

69
@typegraph(
@@ -10,21 +13,13 @@
1013
# skip:end
1114
)
1215
def roadmap(g: Graph):
16+
# skip:start
1317
_bucket = t.struct(
1418
{
1519
"id": t.integer(as_id=True),
1620
"name": t.string(),
1721
}
1822
)
19-
idea = t.struct(
20-
{
21-
"id": t.uuid(
22-
as_id=True
23-
), # email is just a shorthand alias for `t.string({format: "uuid"})`
24-
"name": t.string(),
25-
"authorEmail": t.email(), # another string shorthand
26-
}
27-
)
2823
_vote = t.struct(
2924
{
3025
"id": t.uuid(),
@@ -36,6 +31,16 @@ def roadmap(g: Graph):
3631
"desc": t.string().optional(), # makes it optional
3732
}
3833
)
39-
random = RandomRuntime(reset=None)
34+
# skip:end
35+
idea = t.struct(
36+
{
37+
"id": t.uuid(
38+
as_id=True
39+
), # uuid is just a shorthand alias for `t.string({format: "uuid"})`
40+
"name": t.string(),
41+
"authorEmail": t.email(), # another string shorthand
42+
}
43+
)
44+
random = RandomRuntime(reset=None, seed=1)
4045
pub = Policy.public()
4146
g.expose(pub, get_idea=random.gen(idea))

examples/typegraphs/roadmap-random.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,13 @@ await typegraph({
99
// skip:next-line
1010
cors: { allowOrigin: ["https://metatype.dev", "http://localhost:3000"] },
1111
}, (g) => {
12+
// skip:start
1213
const _bucket = t.struct(
1314
{
1415
"id": t.integer({}, { asId: true }),
1516
"name": t.string(),
1617
},
1718
);
18-
19-
const idea = t.struct(
20-
{
21-
"id": t.uuid({ asId: true }), // email is just a shorthand alias for `t.string({}, {{format: "uuid"}: undefined})`
22-
"name": t.string(),
23-
"authorEmail": t.email(), // another string shorthand
24-
},
25-
);
26-
2719
const _vote = t.struct(
2820
{
2921
"id": t.uuid(),
@@ -35,8 +27,17 @@ await typegraph({
3527
"desc": t.string().optional(), // makes it optional
3628
},
3729
);
30+
// skip:end
31+
32+
const idea = t.struct(
33+
{
34+
"id": t.uuid({ asId: true }), // uuid is just a shorthand alias for `t.string({}, {{format: "uuid"}: undefined})`
35+
"name": t.string(),
36+
"authorEmail": t.email(), // another string shorthand
37+
},
38+
);
3839

39-
const random = new RandomRuntime({});
40+
const random = new RandomRuntime({ seed: 1 });
4041
const pub = Policy.public();
4142
g.expose({ get_idea: random.gen(idea) }, pub);
4243
});

website/docs/concepts/features-overview/index.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ Working with object files in Metatype is easy using the [S3Runtime](/docs/refere
5151

5252
## Function runner
5353

54-
When the expressive powers of the typegate primitives are not up for the task, different runtimes are available for running the exact, turing complete, code you need.
55-
Metatype supports [Typescript](/docs/reference/runtimes/deno), [Python](/docs/reference/runtimes/python) and [Wasm](/docs/reference/runtimes/wasmedge) functions today.
54+
When the expressive powers of the typegate primitives are not up for the task, different runtimes are available for running the exact, turing complete, code you need.
55+
Metatype supports [Typescript](/docs/reference/runtimes/deno), [Python](/docs/reference/runtimes/python) and [Wasm](/docs/reference/runtimes/wasm) functions today.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Available commands
2+
3+
## Managing typegraphs
4+
5+
| Command | Example of use | Description |
6+
| -------- | --------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------- |
7+
| deploy | <pre><p>meta deploy --target dev --file path/to/typegraph.py</p><p>meta --dir folder/to/typegraphs dev --run-destructive-migrations</p></pre> | Deploy typegraph to a typegate instance |
8+
| undeploy | <pre><p>meta undeploy --target dev --typegraph logs accounting services</p></pre> | Undeploy typegraphs by name |
9+
| gen | <pre><p>meta gen mod --file path/to/typegraph.py</p><p></p>meta gen mdk</pre> | Generate script or files that are used in your typegraph |
10+
| typegate | <pre><p>meta typegate --quiet</p></pre> | Access a minimal deno CLI |
11+
12+
## Troubleshooting and others
13+
14+
| Command | Example of use | Description |
15+
| ---------- | -------------------------------------------------------------------- | ------------------------------------------------------------------------ |
16+
| doctor | <pre><p>meta doctor</p></pre> | Help troubleshoot all your installations |
17+
| completion | <pre><p>meta completion</p><p>meta completion --shell bash</p></pre> | Generate shell completion (supports bash, elvish, fish, powershell, zsh) |
18+
| serialize | <pre><p>meta serialize --file path/to/typegraph.py</p></pre> | Inspect serialized version of your typegraph |
19+
| upgrade | <pre><p>meta upgrade</p><p>meta upgrade --version 0.3.4</p></pre> | Upgrade current cli |
20+
21+
22+
Feel free to use the `--help` flag if you want more informations on each commands/subcommands.
23+
```bash
24+
meta -h
25+
meta deploy -h
26+
meta gen mod -h
27+
```

website/docs/reference/meta-cli/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ It deploy your typegraph to the Metatype Cloud or your self-hosted typegate inst
2222

2323
## Development typegate
2424

25-
For development purposes, the cli bundles the typegate itself and this can be accessed throug the `meta typegate` subcommand.
25+
For development purposes, the cli bundles the typegate itself and this can be accessed through the `meta typegate` subcommand.
2626

2727
:::info
2828

website/docs/reference/runtimes/graphql/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import TGExample from "@site/src/components/TGExample";
44

55
## GraphQL runtime
66

7-
You currently have a single model to describe messages sent in the chat-based app. A reasonable next step is to add a user model and make a link between the two. While you can store users in the same database, it's wiser to avoid data duplication and re-use your service for user management available at [GraphQLZero](https://graphqlzero.almansi.me) endpoint. Let's introduce the GraphQL runtime that allows remote GraphQL queries.
7+
You currently have a single model to describe messages sent in the chat-based app. A reasonable next step is to add a user model and make a link between the two. While you can store users in the same database, it's wiser to avoid data duplication and re-use your service for user management available at [GraphQLZero](https://graphqlzero.almansi.me) endpoint. Let's introduce the [GraphQL](https://spec.graphql.org/October2021/) runtime that allows remote GraphQL queries.
88

99
Update `typegraph.py` with the highlighted lines below:
1010

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
query {
2+
facts(language: "en") {
3+
id
4+
text
5+
# source_url
6+
# permalink
7+
}
8+
facts_as_text(language: "en")
9+
}
Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,102 @@
1+
import TGExample from "@site/src/components/TGExample";
2+
import SDKTabs from "@site/src/components/SDKTabs";
3+
import TabItem from "@theme/TabItem";
4+
15
# HTTP/REST
26

3-
The HTTPRuntime allows our typegraphs to access external REST APIs.
7+
## HTTP Runtime
8+
The HTTPRuntime allows your typegraphs to access external REST APIs.
9+
10+
Common use cases (but not limited to):
11+
- Enable consuming one or more REST APIs through the same interface
12+
- Programmatically generate typegraphs from an existing [openapi specs](https://swagger.io/specification/) or similar
413

514
Example:
15+
<TGExample
16+
typegraph="http-runtime"
17+
python={require("../../../../../examples/typegraphs/http-runtime.py")}
18+
query={require("./http.graphql")}
19+
/>
620

7-
```python
8-
from typegraph.runtime.http import HTTPRuntime
921

10-
# ..
22+
## Verbs
23+
This runtime supports `GET`, `POST`, `PUT`, `DELETE` http verbs.
24+
25+
In most cases, queries are not limited to a simple query parameter or use the default `application/json` content type.
26+
You can assign what parts of your request description each field in the input struct belongs to.
27+
28+
In the example bellow, this endpoint corresponds to `POST <API_URL>/submit_user?form_type=..` with a body requiring the fields:
29+
`pseudo`, `age` and with header `accept` set as `application/json`.
1130

12-
remote = HTTPRuntime('https://dev.to/api')
13-
remote.get(
14-
'/test',
15-
t.struct({}),
16-
t.list(t.struct({'a': t.integer()})),
17-
)
31+
32+
<SDKTabs>
33+
<TabItem value="python">
34+
```python
35+
# ..
36+
remote = HTTPRuntime("<API_URL>")
37+
g.expose(
38+
pub,
39+
add_user=remote.post(
40+
"/submit_user",
41+
# define your input/output
42+
t.struct(
43+
{
44+
"id": t.uuid(),
45+
"username": t.float(),
46+
"years_lived": t.integer(),
47+
"form_type": t.integer(),
48+
"config_accept": t.string().set("application/json")
49+
},
50+
),
51+
t.struct({ "message": t.string() }),
52+
# specify where each field in your input should be associated with
53+
body_fields=("username", "years_lived"),
54+
query_fields=("form_type"),
55+
# you may want to rename a few fields
56+
# if you are using your own naming conventions or reusing types
57+
rename_fields={
58+
"username": "pseudo",
59+
"years_lived": "age",
60+
},
61+
content_type="multipart/form-data",
62+
# set a custom header prefix
63+
header_prefix="config_"
64+
)
65+
)
66+
# ..
67+
```
68+
</TabItem>
69+
<TabItem value="typescript">
70+
```typescript
71+
// ..
72+
const remote = new HttpRuntime("<API_URL>");
73+
g.expose({
74+
add_user: remote.post(
75+
// define your input/output
76+
t.struct(
77+
{
78+
id: t.uuid(),
79+
username: t.float(),
80+
years_lived: t.integer(),
81+
form_type: t.integer()
82+
},
83+
),
84+
t.struct({ message: t.string() }),
85+
{
86+
path: "/submit_user",
87+
// specify where each field in your input should be associated with
88+
bodyFields: ["username", "years_lived"],
89+
queryFields: ["form_type"],
90+
// you may want to rename a few fields
91+
// if you are using your own naming conventions or reusing types
92+
renameFields: [
93+
["username", "pseudo"],
94+
["years_lived", "age"],
95+
],
96+
contentType: "multipart/form-data",
97+
}
98+
)}, pub);
99+
// ..
18100
```
101+
</TabItem>
102+
</SDKTabs>

0 commit comments

Comments
 (0)