Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add GraphQL alias support for prisma runtime #887

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ghjk/lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,7 @@
]
}
},
"defaultEnv": "ci",
"defaultEnv": "dev",
"envsNamed": {
"main": "bciqiaaqxx3azvrkvlj2eknjj6awsczu4gdcohccrvjpwk3vmkrnyfpi",
"_wasm": "bciqdpiup27tflfkq26oirbutkz53wrp34entiq423upf47za7zoq4za",
Expand Down
3 changes: 2 additions & 1 deletion src/typegate/src/engine/query_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ export class ComputeStage {
}\n--`;
}

withResolver(resolver: Resolver): ComputeStage {
withResolver(resolver: Resolver, deps: string[] = []): ComputeStage {
return new ComputeStage({
...this.props,
dependencies: [...this.props.dependencies, ...deps],
resolver,
});
}
Expand Down
5 changes: 4 additions & 1 deletion src/typegate/src/runtimes/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ export abstract class Runtime {
materializedStages.push(...materializeRoot(s));
} else {
materializedStages.push(
s.withResolver(Runtime.resolveFromParent(s.props.node)),
s.withResolver(
Runtime.resolveFromParent(s.props.node),
[s.props.parent!.id()],
),
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/typegate/src/runtimes/prisma/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class PrismaRuntime extends Runtime {
this.execute(
query,
stage.props.materializer?.data.path as string[] ??
[node],
[stage.props.node],
renames,
),
);
Expand Down
23 changes: 22 additions & 1 deletion tests/typecheck/type_alias.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
from typegraph import typegraph, Policy, t, Graph
from typegraph.runtimes.random import RandomRuntime
from typegraph.runtimes import RandomRuntime
from typegraph.providers import PrismaRuntime


@typegraph()
def type_alias(g: Graph):
random = RandomRuntime(seed=1)
prisma = PrismaRuntime("prisma", "POSTGRES")
public = Policy.public()

infos = t.struct(
Expand All @@ -23,7 +25,26 @@ def type_alias(g: Graph):
}
)

user = t.struct(
{
"id": t.integer().id(),
"name": t.string(),
"posts": t.list(g.ref("post")),
},
name="user",
)
_post = t.struct(
{
"id": t.integer().id(),
"title": t.string(),
"content": t.string(),
"author": user,
},
name="post",
)

g.expose(
public,
get_message=random.gen(message),
create_user=prisma.create(user),
)
74 changes: 72 additions & 2 deletions tests/typecheck/type_alias_test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
// Copyright Metatype OÜ, licensed under the Elastic License 2.0.
// SPDX-License-Identifier: Elastic-2.0

import { gql, Meta } from "../utils/mod.ts";
import { gql, Meta } from "test-utils/mod.ts";
import { dropSchemas, recreateMigrations } from "test-utils/migrations.ts";
import { randomPGConnStr } from "test-utils/database.ts";

Meta.test("Random", async (t) => {
const e = await t.engine("typecheck/type_alias.py");
const { connStr } = randomPGConnStr();
const e = await t.engine("typecheck/type_alias.py", {
secrets: {
POSTGRES: connStr,
},
});
await dropSchemas(e);
await recreateMigrations(e);

await t.should("validate and work with a basic alias", async () => {
await gql`
Expand Down Expand Up @@ -102,4 +111,65 @@ Meta.test("Random", async (t) => {
})
.on(e);
});

await t.should("validate and work with prisma runtime", async () => {
await gql`
mutation {
user1: create_user(
data: { id: 123, name: "john", }
) {
user_id: id
name
posts {
title
content
}
}
}
`
.expectData({
user1: {
name: "john",
user_id: 123,
posts: [],
},
})
.on(e);

await gql`
mutation {
user1: create_user(
data: {
id: 124,
name: "john",
posts: {
create: {
id: 321,
title: "hello",
content: "Hello World!",
}
}
}
) {
id
name
user_posts: posts {
post_title: title
content
}
}
}
`
.expectData({
user1: {
id: 124,
name: "john",
user_posts: [{
content: "Hello World!",
post_title: "hello",
}],
},
})
.on(e);
});
});
Loading