Skip to content

Commit efe08d6

Browse files
bnjjjabernix
andauthored
fix error message when querying non existent field (#1817)
close #1816 Signed-off-by: Benjamin Coenen <[email protected]> Co-authored-by: Jesse Rosenberger <[email protected]>
1 parent 3449da9 commit efe08d6

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

NEXT_CHANGELOG.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,24 @@ By [@Geal](https://github.com/Geal) in https://github.com/apollographql/router/p
104104

105105
## 🛠 Maintenance
106106

107-
### Update apollo-router-scaffold to use the published router crate [PR #1782](https://github.com/apollographql/router/pull/1782)
107+
### Improve error message when querying non existent field [Issue #1816](https://github.com/apollographql/router/issues/1816)
108+
109+
When querying a non existent field you will get a better error.
110+
111+
```patch
112+
{
113+
"errors": [
114+
{
115+
- "message": "invalid type error, expected another type than 'Named type Computer'"
116+
+ "message": "Cannot query field \"xxx\" on type \"Computer\""
117+
}
118+
]
119+
}
120+
```
121+
122+
By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/1817
123+
124+
### Update `apollo-router-scaffold` to use the published `apollo-router` crate [PR #1782](https://github.com/apollographql/router/pull/1782)
108125

109126
Now that apollo-router version "1.0.0-rc.0" is released on [crates.io](https://crates.io/crates/apollo-router), we can update scaffold to it relies on the published crate instead of the git tag.
110127

apollo-router/src/spec/field_type.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use apollo_parser::ast;
2-
use displaydoc::Display;
32

43
use crate::json_ext::Value;
54
use crate::json_ext::ValueExt;
@@ -9,7 +8,7 @@ use crate::*;
98
pub(crate) struct InvalidValue;
109

1110
// Primitives are taken from scalars: https://spec.graphql.org/draft/#sec-Scalars
12-
#[derive(Debug, Display, Clone, PartialEq, Eq, Hash)]
11+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1312
pub(crate) enum FieldType {
1413
/// Only used for introspection queries when types are prefixed by __
1514
Introspection(String),
@@ -31,6 +30,21 @@ pub(crate) enum FieldType {
3130
Boolean,
3231
}
3332

33+
impl std::fmt::Display for FieldType {
34+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35+
match self {
36+
FieldType::Introspection(ty) | FieldType::Named(ty) => write!(f, "{ty}"),
37+
FieldType::List(ty) => write!(f, "[{ty}]"),
38+
FieldType::NonNull(ty) => write!(f, "{ty}!"),
39+
FieldType::String => write!(f, "String"),
40+
FieldType::Int => write!(f, "Int"),
41+
FieldType::Float => write!(f, "Float"),
42+
FieldType::Id => write!(f, "ID"),
43+
FieldType::Boolean => write!(f, "Boolean"),
44+
}
45+
}
46+
}
47+
3448
impl FieldType {
3549
// This function validates input values according to the graphql specification.
3650
// Each of the values are validated against the "input coercion" rules.

apollo-router/src/spec/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ pub(crate) enum SpecError {
2121
RecursionLimitExceeded,
2222
/// invalid type error, expected another type than '{0}'
2323
InvalidType(String),
24+
/// cannot query field '{0}' on type '{1}'
25+
InvalidField(String, String),
2426
/// parsing error: {0}
2527
ParsingError(String),
2628
/// subscription operation is not supported

apollo-router/src/spec/query/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ fn filter_nested_object_errors() {
16491649
}})
16501650
.expected_errors(json! {[
16511651
{
1652-
"message": "Cannot return null for non-nullable field Named type Review.text2",
1652+
"message": "Cannot return null for non-nullable field Review.text2",
16531653
"path": ["me", "reviews1", 0]
16541654
}
16551655
]})

apollo-router/src/spec/selection.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,23 +97,25 @@ impl Selection {
9797
} else if field_name == "__type" {
9898
FieldType::Introspection("__Type".to_string())
9999
} else {
100-
current_type
100+
let name = current_type
101101
.inner_type_name()
102-
.and_then(|name| {
103-
//looking into object types
102+
.ok_or_else(|| SpecError::InvalidType(current_type.to_string()))?;
103+
104+
//looking into object types
105+
schema
106+
.object_types
107+
.get(name)
108+
.and_then(|ty| ty.field(&field_name))
109+
// otherwise, it might be an interface
110+
.or_else(|| {
104111
schema
105-
.object_types
112+
.interfaces
106113
.get(name)
107114
.and_then(|ty| ty.field(&field_name))
108-
// otherwise, it might be an interface
109-
.or_else(|| {
110-
schema
111-
.interfaces
112-
.get(name)
113-
.and_then(|ty| ty.field(&field_name))
114-
})
115115
})
116-
.ok_or_else(|| SpecError::InvalidType(current_type.to_string()))?
116+
.ok_or_else(|| {
117+
SpecError::InvalidField(field_name.clone(), current_type.to_string())
118+
})?
117119
.clone()
118120
};
119121

0 commit comments

Comments
 (0)