Skip to content

Commit

Permalink
fix: json protocol should raise rich error on unknown nested field (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky authored Jun 14, 2024
1 parent 9959b17 commit e974ca8
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"action": "findMany",
"modelName": "User",
"query": {
"selection": {
"notAField": {
"selection": {
"$scalars": true
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"is_panic": false,
"message": "Field 'notAField' not found in enclosing type 'User'",
"meta": {
"kind": "UnknownSelectionField",
"outputType": {
"name": "User",
"fields": [
{
"name": "id",
"typeName": "Int",
"isRelation": false
},
{
"name": "email",
"typeName": "String",
"isRelation": false
},
{
"name": "dob",
"typeName": "DateTime",
"isRelation": false
},
{
"name": "posts",
"typeName": "Post[]",
"isRelation": true
},
{
"name": "_count",
"typeName": "UserCountOutputType",
"isRelation": false
}
]
},
"selectionPath": [
"findManyUser",
"notAField"
]
},
"error_code": "P2009"
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,23 @@ impl<'a> JsonProtocolAdapter<'a> {
// <field_name>: { selection: { ... }, arguments: { ... } }
crate::SelectionSetValue::Nested(nested_query) => {
if field.field_type().as_object_type().is_some() {
let schema_field = field
if let Some(schema_field) = field
.field_type()
.as_object_type()
.and_then(|t| t.find_field(&selection_name))
.ok_or_else(|| {
HandlerError::query_conversion(format!(
"Unknown nested field '{}' for operation {} does not match any query.",
selection_name,
field.name()
))
})?;

let field = container.and_then(|container| container.find_field(schema_field.name()));
let nested_container = field.map(|f| f.related_container());

selection.push_nested_selection(self.convert_selection(
schema_field,
nested_container.as_ref(),
nested_query,
)?);
{
let field = container.and_then(|container| container.find_field(schema_field.name()));
let nested_container = field.map(|f| f.related_container());

selection.push_nested_selection(self.convert_selection(
schema_field,
nested_container.as_ref(),
nested_query,
)?);
} else {
// Unknown nested field that we keep around so that parser can fail with a rich error.
selection.push_nested_selection(Selection::with_name(selection_name));
}
}
}
}
Expand Down

0 comments on commit e974ca8

Please sign in to comment.