Skip to content

Commit

Permalink
feat(sdk): Add the prisma runtime to the new SDK (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
Natoandro authored Sep 20, 2023
1 parent 057b6fe commit 04428d1
Show file tree
Hide file tree
Showing 136 changed files with 11,234 additions and 1,642 deletions.
990 changes: 507 additions & 483 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dev/tree-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

/**
* Usage:
* deno run -A dev/tree-view.ts --import-map typegate/deno-import.json [<options>] <file.py>
* deno run -A dev/tree-view.ts --confg typegate/deno.json [<options>] <file.py>
*
* Options:
* --depth <N> The depth of the tree
Expand Down
2 changes: 1 addition & 1 deletion libs/common/src/typegraph/runtimes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub enum KnownRuntime {
}

#[cfg_attr(feature = "codegen", derive(JsonSchema))]
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct UnknownRuntime {
pub name: String,
pub data: IndexMap<String, serde_json::Value>,
Expand Down
6 changes: 3 additions & 3 deletions libs/common/src/typegraph/runtimes/prisma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ pub struct RelationshipModel {
#[cfg_attr(feature = "codegen", derive(JsonSchema))]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Relationship {
name: String,
left: RelationshipModel,
right: RelationshipModel,
pub name: String,
pub left: RelationshipModel,
pub right: RelationshipModel,
}

#[cfg_attr(feature = "codegen", derive(JsonSchema))]
Expand Down
2 changes: 1 addition & 1 deletion meta-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ self_update = { version = "0.37.0", features = [
"compression-zip-deflate",
"compression-zip-bzip2",
] }
prisma-models = { git = "https://github.com/prisma/prisma-engines", tag = "5.0.0" }
prisma-models = { git = "https://github.com/prisma/prisma-engines", tag = "5.1.1" }
lazy_static = "1.4.0"
tokio = { version = "1.32.0", features = ["full"] }
futures = "0.3.28"
Expand Down
124 changes: 123 additions & 1 deletion typegate/deno.lock

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions typegate/native/src/runtimes/prisma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,28 @@ enum PrismaRegisterEngineOut {

#[deno]
fn prisma_register_engine(input: PrismaRegisterEngineInp) -> PrismaRegisterEngineOut {
match RT.block_on(engine::register_engine(input.datamodel, input.engine_name)) {
let datamodel = prisma_models::psl::reformat(&input.datamodel, 4);

let datamodel = match datamodel {
Some(dm) => dm,
None => {
log::error!("Error formatting datamodel:\n{}", input.datamodel);
return PrismaRegisterEngineOut::Err {
message: "Error formatting datamodel".to_string(),
};
}
};

log::info!("Reformatted datamodel:\n{}", datamodel);

match RT.block_on(engine::register_engine(datamodel, input.engine_name)) {
Ok(()) => PrismaRegisterEngineOut::Ok,
Err(e) => PrismaRegisterEngineOut::Err {
message: e.to_string(),
},
Err(e) => {
log::error!("Error registering engine: {:?}", e);
PrismaRegisterEngineOut::Err {
message: e.to_string(),
}
}
}
}

Expand Down
60 changes: 51 additions & 9 deletions typegate/src/planner/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ class ArgumentCollector {
): ComputeArg {
const { value: valueNode } = astNode;
const variantTypesIndexes: number[] = getVariantTypesIndexes(typeNode);
const errors: Error[] = [];

// throw type mismatch error only if the argument node of the query
// does not match any of the subschemes (variant nodes).
Expand All @@ -416,10 +417,14 @@ class ArgumentCollector {
typeIdx: variantTypeIndex,
});
} catch (error) {
console.error("variant error:", error);
if (
error instanceof TypeMismatchError ||
error instanceof MandatoryArgumentError
error instanceof UnionTypeMismatchError ||
error instanceof MandatoryArgumentError ||
error instanceof UnexpectedPropertiesError
) {
errors.push(error);
continue;
}

Expand All @@ -434,9 +439,11 @@ class ArgumentCollector {
.map((variantType) => variantType.type)
.forEach((typeName) => expectedVariants.add(typeName.toUpperCase()));

throw new TypeMismatchError(
throw new UnionTypeMismatchError(
valueNode.kind,
[...expectedVariants],
typeNode.type,
errors,
this.currentNode.path.length,
this.currentNodeDetails,
);
}
Expand Down Expand Up @@ -510,11 +517,11 @@ class ArgumentCollector {

const unexpectedProps = Object.keys(fieldByKeys);
if (unexpectedProps.length > 0) {
const details = [
unexpectedProps.map((name) => `'${name}'`).join(", "),
`for argument ${this.currentNodeDetails}`,
].join(" ");
throw new Error(`Unexpected props ${details}`);
throw new UnexpectedPropertiesError(
unexpectedProps,
this.currentNodeDetails,
Object.keys(props),
);
}

return (...params: Parameters<ComputeArg>) =>
Expand Down Expand Up @@ -742,9 +749,44 @@ class TypeMismatchError extends Error {
.map((t) => `'${t}'`)
.join(" or ");
const errorMessage = [
`Type mismatch: got '${actual}' but expected ${exp}`,
`Type mismatch: got '${actual}' but expected '${exp}'`,
`for argument ${argDetails}`,
].join(" ");
super(errorMessage);
}
}

class UnionTypeMismatchError extends Error {
constructor(
actual: string,
expected: string,
nestedErrors: Error[],
depth: number,
argDetails: string,
) {
const indent = " ".repeat(depth);
const causes = nestedErrors.map((e) => `${indent}- ${e.message}\n`);
const errorMessage = [
`Type mismatch: got '${actual}' but expected '${expected}'`,
`for argument ${argDetails}`,
`caused by:\n${causes.join("")}`,
].join(" ");
super(errorMessage);
}
}

class UnexpectedPropertiesError extends Error {
constructor(
props: string[],
nodeDetails: string,
validProps: string[],
) {
const name = props.length === 1 ? "property" : "properties";
const errorMessage = [
`Unexpected ${name} '${props.join(", ")}'`,
`for argument ${nodeDetails};`,
`valid properties are: ${validProps.join(", ")}`,
].join(" ");
super(errorMessage);
}
}
12 changes: 6 additions & 6 deletions typegate/src/runtimes/prisma/hooks/generate_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,12 @@ class FieldBuilder {
}
const [rel, side] = found;
switch (side) {
case "left": {
case "left":
return new ModelField(name, typeNode.title + quant, [
`@relation(name: ${toPrismaString(rel.name)})`,
]);

case "right": {
const [tag, fkeys] = this.#getRelationTagAndFkeys(
name,
typeNode,
Expand All @@ -217,11 +222,6 @@ class FieldBuilder {
return modelField;
}

case "right":
return new ModelField(name, typeNode.title + quant, [
`@relation(name: ${toPrismaString(rel.name)})`,
]);

default:
throw new Error(`invalid side: ${side}`);
}
Expand Down
1 change: 1 addition & 0 deletions typegate/src/services/graphql_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export async function handleGraphQL(

return jsonOk(res, headers);
} catch (e) {
// throw e;
if (e instanceof ResolverError) {
logger.error(`field err: ${e.message}`);
return jsonError(e.message, headers, 502);
Expand Down
30 changes: 15 additions & 15 deletions typegate/tests/e2e/typegraph/__snapshots__/typegraph_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ snapshot[`typegraphs creation 1`] = `
},
{
"type": "object",
"title": "object_18",
"title": "ComplexType",
"runtime": 0,
"policies": [],
"config": {},
Expand Down Expand Up @@ -179,7 +179,7 @@ snapshot[`typegraphs creation 1`] = `
},
{
"type": "object",
"title": "object_4",
"title": "SomeType",
"runtime": 0,
"policies": [],
"config": {},
Expand All @@ -192,7 +192,7 @@ snapshot[`typegraphs creation 1`] = `
},
{
"type": "array",
"title": "array_1",
"title": "Two",
"runtime": 0,
"policies": [],
"config": {},
Expand All @@ -210,7 +210,7 @@ snapshot[`typegraphs creation 1`] = `
},
{
"type": "optional",
"title": "optional_3",
"title": "_3_SomeType?",
"runtime": 0,
"policies": [],
"config": {},
Expand Down Expand Up @@ -570,7 +570,7 @@ snapshot[`typegraphs creation 3`] = `
},
{
"type": "function",
"title": "func_9",
"title": "func_10",
"runtime": 0,
"policies": [
0
Expand Down Expand Up @@ -625,7 +625,7 @@ snapshot[`typegraphs creation 3`] = `
},
{
"type": "function",
"title": "func_11",
"title": "func_12",
"runtime": 0,
"policies": [
1
Expand All @@ -640,7 +640,7 @@ snapshot[`typegraphs creation 3`] = `
},
{
"type": "object",
"title": "object_6",
"title": "User",
"runtime": 0,
"policies": [],
"config": {},
Expand All @@ -661,7 +661,7 @@ snapshot[`typegraphs creation 3`] = `
},
{
"type": "object",
"title": "object_8",
"title": "Post",
"runtime": 0,
"policies": [],
"config": {},
Expand All @@ -682,7 +682,7 @@ snapshot[`typegraphs creation 3`] = `
},
{
"type": "function",
"title": "func_13",
"title": "func_14",
"runtime": 0,
"policies": [
2
Expand Down Expand Up @@ -859,7 +859,7 @@ snapshot[`typegraphs creation 4`] = `
},
{
"type": "object",
"title": "object_18",
"title": "ComplexType",
"runtime": 0,
"policies": [],
"config": {},
Expand Down Expand Up @@ -1002,7 +1002,7 @@ snapshot[`typegraphs creation 4`] = `
},
{
"type": "object",
"title": "object_4",
"title": "SomeType",
"runtime": 0,
"policies": [],
"config": {},
Expand All @@ -1015,7 +1015,7 @@ snapshot[`typegraphs creation 4`] = `
},
{
"type": "array",
"title": "array_1",
"title": "Two",
"runtime": 0,
"policies": [],
"config": {},
Expand All @@ -1033,7 +1033,7 @@ snapshot[`typegraphs creation 4`] = `
},
{
"type": "optional",
"title": "optional_3",
"title": "_3_SomeType?",
"runtime": 0,
"policies": [],
"config": {},
Expand Down Expand Up @@ -1463,7 +1463,7 @@ snapshot[`typegraphs creation 6`] = `
},
{
"type": "object",
"title": "object_6",
"title": "User",
"runtime": 0,
"policies": [],
"config": {},
Expand All @@ -1484,7 +1484,7 @@ snapshot[`typegraphs creation 6`] = `
},
{
"type": "object",
"title": "object_8",
"title": "Post",
"runtime": 0,
"policies": [],
"config": {},
Expand Down
File renamed without changes.
Loading

0 comments on commit 04428d1

Please sign in to comment.