Skip to content

Commit

Permalink
MET-143: support multi-runtime in all runtimes (#258)
Browse files Browse the repository at this point in the history
* fix output generator

* mix with random runtime
  • Loading branch information
michael-0acf4 authored Apr 5, 2023
1 parent 2b86c1e commit fe0055f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 58 deletions.
25 changes: 17 additions & 8 deletions typegate/tests/prisma/mixed_runtime.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
from typegraph import TypeGraph, effects, policies, t
from typegraph.providers.prisma.runtimes.prisma import PrismaRuntime
from typegraph.runtimes.graphql import GraphQLRuntime
from typegraph.runtimes.random import RandomMat, RandomRuntime

with TypeGraph("prisma") as g:
db = PrismaRuntime("prisma", "POSTGRES")
gql = GraphQLRuntime("https://graphqlzero.almansi.me/api")
gql_1 = GraphQLRuntime("https://graphqlzero.almansi.me/api")
rand = RandomRuntime(seed=1)

public = policies.public()
country = t.struct(
post = t.struct(
{
"code": t.string(),
"name": t.integer(),
"id": t.string(),
"title": t.string(),
},
).named("Country")
).named("Post")
user = t.struct(
{
"name": t.string().config(gen="name"),
"age": t.integer().config(gen="age", type="adult"),
}
).named("Album")
record = t.struct(
{
"id": t.integer().config("id", "auto"),
"description": t.string(),
"country": gql.query(
t.struct({"code": t.string()}),
t.optional(country),
"post": gql_1.query(
t.struct({"id": t.string()}),
t.optional(post),
),
"user": t.gen(user, RandomMat(runtime=rand)),
},
).named("Record")

Expand Down
128 changes: 79 additions & 49 deletions typegate/tests/prisma/mixed_runtime_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ test("prisma mixed runtime", async (t) => {
"postgresql://postgres:password@localhost:5432/db?schema=test",
},
});

await t.should("drop schema and recreate", async () => {
await gql`
mutation a {
dropSchema
}
`
mutation a {
dropSchema
}
`
.expectData({
dropSchema: 0,
})
Expand All @@ -26,19 +27,19 @@ test("prisma mixed runtime", async (t) => {
"insert a record without considering fields owned by other runtimes",
async () => {
await gql`
mutation {
createOneRecord (
data: {
id: 1,
description: "Some description"
}
)
{
id
description
}
mutation {
createOneRecord (
data: {
id: 1,
description: "Some description"
}
)
{
id
description
}
`.expectData({
}
`.expectData({
createOneRecord: {
id: 1,
description: "Some description",
Expand All @@ -48,38 +49,67 @@ test("prisma mixed runtime", async (t) => {
},
);

// At this point, mixed runtime should work fine
await t.should("work with different runtimes", async () => {
await gql`
query {
findUniqueRecord(where: {
id: 1
}) {
id
description
post(id: "1") {
id
title
}
}
}
`.expectData({
findUniqueRecord: {
id: 1,
description: "Some description",
post: {
id: "1",
title:
"sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
},
},
})
.on(e);
});

// graphql runtime bug ?
// generated sub-query:
// query Q {
// country {
// code
// name
// }
// }
// request err: Error: From remote graphql: Cannot query field "country" on type "Query".
// await t.should("find unique and work with graphql runtime", async () => {
// await gql`
// query {
// findUniqueRecord(where: {
// id: 1
// }) {
// id
// description
// country(code: "MC") {
// code
// name
// }
// }
// }
// `.expectData({
// findUniqueRecord: {
// id: 1,
// description: "Some description",
// country: { code: "MC", name: "Monaco" },
// },
// })
// .on(e);
// });
await t.should(
"work with more than two runtimes",
async () => {
await gql`
query {
findUniqueRecord(where: {
id: 1
}) {
id
description
post(id: "1") {
id
title
}
user {
name
age
}
}
}
`.expectData({
findUniqueRecord: {
id: 1,
description: "Some description",
post: {
id: "1",
title:
"sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
},
user: { name: "Landon Glover", age: 62 },
},
})
.on(e);
},
);
});
2 changes: 1 addition & 1 deletion typegraph/typegraph/providers/prisma/type_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def get_out_type(
return proxy

if isinstance(tpe, t.func):
return self.get_out_type(tpe.out)
return tpe.out

if not isinstance(tpe, t.struct):
return tpe
Expand Down

0 comments on commit fe0055f

Please sign in to comment.