diff --git a/query-engine/core-tests/tests/query_validation_tests/postgres_basic/unknown_nested_selection_field.query.json b/query-engine/core-tests/tests/query_validation_tests/postgres_basic/unknown_nested_selection_field.query.json new file mode 100644 index 000000000000..c2d578abe45e --- /dev/null +++ b/query-engine/core-tests/tests/query_validation_tests/postgres_basic/unknown_nested_selection_field.query.json @@ -0,0 +1,13 @@ +{ + "action": "findMany", + "modelName": "User", + "query": { + "selection": { + "notAField": { + "selection": { + "$scalars": true + } + } + } + } +} diff --git a/query-engine/core-tests/tests/query_validation_tests/unknown_nested_selection_field.expected.json b/query-engine/core-tests/tests/query_validation_tests/unknown_nested_selection_field.expected.json new file mode 100644 index 000000000000..ce64b25461dd --- /dev/null +++ b/query-engine/core-tests/tests/query_validation_tests/unknown_nested_selection_field.expected.json @@ -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" +} \ No newline at end of file diff --git a/query-engine/request-handlers/src/protocols/json/protocol_adapter.rs b/query-engine/request-handlers/src/protocols/json/protocol_adapter.rs index 4bf97d574bd5..650f4e1a8bb9 100644 --- a/query-engine/request-handlers/src/protocols/json/protocol_adapter.rs +++ b/query-engine/request-handlers/src/protocols/json/protocol_adapter.rs @@ -101,26 +101,23 @@ impl<'a> JsonProtocolAdapter<'a> { // : { 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)); + } } } }