Skip to content

Commit 4db7dc7

Browse files
docs: improve /guides/external-functions (#677)
- Improvements to the `/guides/external-functions` page. - Adds a configuration file for git-town #### Motivation and context _N/A_ #### Migration notes _N/A_ ### Checklist - [ ] The change come with new or modified tests - [ ] Hard-to-understand functions have explanatory comments - [ ] End-user documentation is updated to reflect the change --------- Co-authored-by: Michaël <[email protected]>
1 parent 074310d commit 4db7dc7

40 files changed

+852
-167
lines changed

.git-branches.toml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
push-hook = true
2+
push-new-branches = false
3+
ship-delete-tracking-branch = false
4+
sync-before-ship = true
5+
sync-upstream = true
6+
7+
[branches]
8+
main = "main"
9+
perennials = []
10+
perennial-regex = ""
11+
12+
[hosting]
13+
# platform = ""
14+
# origin-hostname = ""
15+
16+
[sync-strategy]
17+
18+
feature-branches = "rebase"
19+
perennial-branches = "rebase"

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ typegraph/node/sdk/src/gen
4545
typegraph/python/typegraph/gen
4646

4747
*.egg-info/
48+
49+
examples/typegraphs/migrations

examples/metatype.yaml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ typegates:
1212
GITHUB_CLIENT_ID: fake
1313
GITHUB_CLIENT_SECRET: fake
1414
authentication:
15-
BASIC_ADMIN: "password"
15+
BASIC_andim: "password"
1616
docs:
1717
SENDINBLUE_KEY: "fake"
1818
homepage:
@@ -26,8 +26,8 @@ typegates:
2626
database:
2727
POSTGRES_CONN: "postgresql://postgres:password@localhost:5432/db?schema=database"
2828
policies:
29-
BASIC_ADMIN: "admin_pass"
30-
BASIC_USER: "user_pass"
29+
BASIC_admin: "admin_pass"
30+
BASIC_user: "user_pass"
3131
auth:
3232
CUSTOM_JWT: "mysupersecretkey"
3333
graphql:
@@ -41,7 +41,7 @@ typegates:
4141
iam-provider:
4242
GITHUB_CLIENT_ID: "fake"
4343
GITHUB_CLIENT_SECRET: "fake"
44-
retrend:
44+
files-upload:
4545
S3_HOST: http://localhost:9000
4646
S3_REGION: local
4747
S3_ACCESS_KEY: minio
@@ -66,6 +66,8 @@ typegates:
6666
BASIC_ADMIN: hunter2
6767
roadmap:
6868
BASIC_ADMIN: hunter2
69+
func-gql:
70+
POSTGRES: "postgresql://postgres:password@localhost:5432/db?schema=func_ctx"
6971

7072
prd:
7173
url: https://demo.metatype.dev
@@ -80,7 +82,7 @@ typegates:
8082
GITHUB_CLIENT_ID: infisical://app.infisical.com/643057c5bb17b13ef6e73d99/prod/GITHUB_CLIENT_ID
8183
GITHUB_CLIENT_SECRET: infisical://app.infisical.com/643057c5bb17b13ef6e73d99/prod/GITHUB_CLIENT_SECRET
8284
authentication:
83-
BASIC_ADMIN: "password"
85+
BASIC_andim: "password"
8486
docs:
8587
SENDINBLUE_KEY: infisical://app.infisical.com/643057c5bb17b13ef6e73d99/prod/TG_DOCS_SENDINBLUE_KEY
8688
homepage:
@@ -94,8 +96,8 @@ typegates:
9496
database:
9597
POSTGRES_CONN: infisical://app.infisical.com/643057c5bb17b13ef6e73d99/prod/TG_DATABASE_POSTGRES_CONN
9698
policies:
97-
BASIC_ADMIN: "admin_pass"
98-
BASIC_USER: "user_pass"
99+
BASIC_admin: "admin_pass"
100+
BASIC_user: "user_pass"
99101
auth:
100102
CUSTOM_JWT: "mysupersecretkey"
101103
graphql:
@@ -109,7 +111,7 @@ typegates:
109111
iam-provider:
110112
GITHUB_CLIENT_ID: infisical://app.infisical.com/643057c5bb17b13ef6e73d99/prod/GITHUB_CLIENT_ID
111113
GITHUB_CLIENT_SECRET: infisical://app.infisical.com/643057c5bb17b13ef6e73d99/prod/GITHUB_CLIENT_SECRET
112-
retrend:
114+
files-upload:
113115
S3_HOST: http://localhost:9000
114116
S3_REGION: local
115117
S3_ACCESS_KEY: minio
@@ -139,3 +141,7 @@ typegraphs:
139141
materializers:
140142
prisma:
141143
migrations_path: "migrations"
144+
145+
metagen:
146+
targets:
147+
main:

examples/typegraphs/authentication.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,32 @@
33
from typegraph.graph.params import Auth, Cors
44
from typegraph.runtimes.deno import DenoRuntime
55

6-
# skip:end
7-
86

97
@typegraph(
10-
# skip:start
118
cors=Cors(allow_origin=["https://metatype.dev", "http://localhost:3000"]),
12-
# skip:end
13-
# ..
149
)
1510
def authentication(g: Graph):
11+
# skip:end
1612
deno = DenoRuntime()
17-
public = Policy.public()
18-
1913
ctx = t.struct({"username": t.string().optional()})
2014

2115
# highlight-start
2216
# expects a secret in metatype.yml
2317
# `BASIC_[username]`
2418
# highlight-next-line
25-
g.auth(Auth.basic(["admin"]))
19+
g.auth(Auth.basic(["andim"]))
2620
# highlight-end
2721

2822
g.expose(
23+
Policy.public(),
2924
get_context=deno.identity(ctx).apply(
3025
{
3126
"username": g.from_context("username"),
3227
}
3328
),
34-
default_policy=[public],
29+
get_full_context=deno.func(
30+
t.struct({}),
31+
t.string(),
32+
code="(_: any, ctx: any) => Deno.inspect(ctx.context)",
33+
),
3534
)

examples/typegraphs/authentication.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@ import { Policy, t, typegraph } from "@typegraph/sdk/index.js";
33
import { Auth } from "@typegraph/sdk/params.js";
44
import { DenoRuntime } from "@typegraph/sdk/runtimes/deno.js";
55

6-
// skip:end
7-
86
await typegraph({
97
name: "authentication",
10-
// skip:next-line
118
cors: { allowOrigin: ["https://metatype.dev", "http://localhost:3000"] },
129
}, (g) => {
10+
// skip:end
1311
const deno = new DenoRuntime();
14-
const pub = Policy.public();
1512

1613
const ctx = t.struct({
1714
"username": t.string().optional(),
@@ -21,12 +18,21 @@ await typegraph({
2118
// expects a secret in metatype.yml
2219
// `BASIC_[username]`
2320
// highlight-next-line
24-
g.auth(Auth.basic(["admin"]));
21+
g.auth(Auth.basic(["andim"]));
2522
// highlight-end
2623

2724
g.expose({
2825
get_context: deno.identity(ctx).apply({
2926
username: g.fromContext("username"),
30-
}).withPolicy(pub),
31-
});
27+
}),
28+
get_full_context: deno.func(
29+
t.struct({}),
30+
t.string(),
31+
{
32+
code: "(_: any, ctx: any) => Deno.inspect(ctx.context)",
33+
},
34+
),
35+
}, Policy.public());
36+
// skip:start
3237
});
38+
// skip:end

examples/typegraphs/cors.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,20 @@
66

77
# skip:end
88
@typegraph(
9-
# highlight-next-line
9+
# highlight-start
1010
cors=Cors(
11-
# highlight-next-line
1211
allow_origin=["https://not-this.domain"],
13-
# highlight-next-line
1412
allow_headers=["x-custom-header"],
15-
# highlight-next-line
1613
expose_headers=["header-1"],
17-
# highlight-next-line
1814
allow_credentials=True,
19-
# highlight-next-line
2015
max_age_sec=60,
21-
# highlight-next-line
2216
),
17+
# highlight-end
2318
)
24-
def auth(g: Graph):
19+
def cors(g: Graph):
2520
random = RandomRuntime(seed=0, reset=None)
26-
public = Policy.public()
2721

2822
g.expose(
29-
public,
23+
Policy.public(),
3024
catch_me_if_you_can=random.gen(t.string()),
3125
)

examples/typegraphs/cors.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,20 @@ import { RandomRuntime } from "@typegraph/sdk/runtimes/random.js";
55
// skip:end
66

77
await typegraph({
8-
name: "auth",
9-
// highlight-next-line
8+
name: "cors",
9+
// highlight-start
1010
cors: {
11-
// highlight-next-line
1211
allowOrigin: ["https://not-this.domain"],
13-
// highlight-next-line
1412
allowHeaders: ["x-custom-header"],
15-
// highlight-next-line
1613
exposeHeaders: ["header-1"],
17-
// highlight-next-line
1814
allowCredentials: true,
19-
// highlight-next-line
2015
maxAgeSec: 60,
2116
},
17+
// highlight-end
2218
}, (g) => {
2319
const random = new RandomRuntime({ seed: 0 });
24-
const pub = Policy.public();
2520

2621
g.expose({
27-
catch_me_if_you_can: random.gen(t.string()).withPolicy(pub),
28-
});
22+
catch_me_if_you_can: random.gen(t.string()),
23+
}, Policy.public());
2924
});

examples/typegraphs/example_rest.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# skip:start
12
from typegraph import Graph, Policy, t, typegraph
23
from typegraph.runtimes.deno import DenoRuntime
34

@@ -16,9 +17,18 @@ def example_rest(g: Graph):
1617
},
1718
name="Post",
1819
)
20+
# skip:end
21+
22+
g.expose(
23+
postFromUser=deno.func(
24+
user,
25+
post,
26+
code="(_) => ({ id: 12, author: {id: 1} })",
27+
).with_policy(pub),
28+
)
1929

20-
# API docs {typegate_url}/example-rest/rest
2130
# In this example, the query below maps to {typegate_url}/example-rest/rest/get_post?id=..
31+
# highlight-start
2232
g.rest(
2333
"""
2434
query get_post($id: Integer) {
@@ -31,11 +41,4 @@ def example_rest(g: Graph):
3141
}
3242
"""
3343
)
34-
35-
g.expose(
36-
postFromUser=deno.func(
37-
user,
38-
post,
39-
code="(_) => ({ id: 12, author: {id: 1} })",
40-
).with_policy(pub),
41-
)
44+
# highlight-end

examples/typegraphs/example_rest.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// skip:start
12
import { Policy, t, typegraph } from "@typegraph/sdk/index.js";
23
import { DenoRuntime } from "@typegraph/sdk/runtimes/deno.js";
34

@@ -17,9 +18,18 @@ await typegraph({
1718
},
1819
{ name: "Post" },
1920
);
21+
// skip:end
22+
23+
g.expose({
24+
postFromUser: deno.func(
25+
user,
26+
post,
27+
{ code: "(_) => ({ id: 12, author: {id: 1} })" },
28+
).withPolicy(pub),
29+
});
2030

21-
// API docs {typegate_url}/example-rest/rest
2231
// In this example, the query below maps to {typegate_url}/example-rest/rest/get_post?id=..
32+
// highlight-start
2333
g.rest(
2434
`
2535
query get_post($id: Integer) {
@@ -32,12 +42,6 @@ await typegraph({
3242
}
3343
`,
3444
);
35-
36-
g.expose({
37-
postFromUser: deno.func(
38-
user,
39-
post,
40-
{ code: "(_) => ({ id: 12, author: {id: 1} })" },
41-
).withPolicy(pub),
42-
});
45+
// highlight-end
46+
// skip:start
4347
});

examples/typegraphs/files-upload.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@
66

77

88
@typegraph(
9+
name="files-upload",
910
# skip-next-line
1011
cors=Cors(allow_origin=["https://metatype.dev", "http://localhost:3000"]),
1112
)
12-
def retrend(g: Graph):
13-
public = Policy.public()
14-
13+
def files_upload(g: Graph):
1514
s3 = S3Runtime(
1615
# we provide the name of the env vars
1716
# the typegate will read from
@@ -23,11 +22,11 @@ def retrend(g: Graph):
2322
)
2423

2524
g.expose(
25+
Policy.public(),
2626
# we can then generate helpers for interacting with our runtime
2727
listObjects=s3.list("bucket"),
2828
getDownloadUrl=s3.presign_get("bucket"),
2929
signUploadUrl=s3.presign_put("bucket"),
3030
upload=s3.upload("bucket", t.file(allow=["image/png", "image/jpeg"])),
3131
uploadMany=s3.upload_all("bucket"),
32-
default_policy=[public],
3332
)

examples/typegraphs/files-upload.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import { Policy, t, typegraph } from "@typegraph/sdk/index.js";
22
import { S3Runtime } from "@typegraph/sdk/providers/aws.js";
33

44
await typegraph({
5-
name: "retrend",
5+
name: "files-upload",
66
// skip:next-line
77
cors: { allowOrigin: ["https://metatype.dev", "http://localhost:3000"] },
88
}, (g) => {
9-
const pub = Policy.public();
10-
119
const s3 = new S3Runtime({
1210
hostSecret: "S3_HOST",
1311
regionSecret: "S3_REGION",
@@ -22,5 +20,5 @@ await typegraph({
2220
signUploadUrl: s3.presignPut({ bucket: "bucket" }),
2321
upload: s3.upload("bucket", t.file({ allow: ["image/png", "image/jpeg"] })),
2422
uploadMany: s3.uploadAll("bucket"),
25-
}, pub);
23+
}, Policy.public());
2624
});

0 commit comments

Comments
 (0)