Skip to content

Commit 3c74918

Browse files
fix: catch missing fragment definitions in standalone executable validation (#1003)
Given an operation with undefined fragment definitions using `document.validate_standalone_executable()`, report `UndefinedFragment` error.
1 parent 6840509 commit 3c74918

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

crates/apollo-compiler/src/validation/field.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,18 @@ pub(crate) fn validate_field(
3333

3434
super::argument::validate_arguments(diagnostics, &field.arguments);
3535

36-
// Return early if we don't know the type--this can happen if we are nested deeply
37-
// inside a selection set that has a wrong field, or if we are validating a standalone
38-
// operation without a schema.
36+
// If we don't know the type (no schema, or invalid parent), we cannot perform
37+
// type-aware checks for this field. However, for standalone executable validation
38+
// we still want to traverse into the nested selection set so that validations
39+
// that do not require a schema (like missing fragment detection) can run.
3940
let Some((schema, against_type)) = against_type else {
41+
super::selection::validate_selection_set(
42+
diagnostics,
43+
document,
44+
None,
45+
&field.selection_set,
46+
context,
47+
);
4048
return;
4149
};
4250

crates/apollo-compiler/tests/validation/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,33 @@ type TestObject {
352352
}"#]];
353353
expected.assert_eq(&actual);
354354
}
355+
356+
#[test]
357+
fn missing_fragment_in_standalone_validation() {
358+
let input = r#"
359+
query {
360+
company {
361+
user {
362+
...UserFragment
363+
}
364+
365+
...CompanyFragment
366+
}
367+
}
368+
369+
fragment UserFragment on User {
370+
id
371+
name
372+
}
373+
"#;
374+
375+
let doc = ast::Document::parse(input, "query.graphql").unwrap();
376+
let diagnostics = doc
377+
.validate_standalone_executable()
378+
.expect_err("should report missing fragment error");
379+
let errors = diagnostics.to_string();
380+
assert!(
381+
errors.contains("cannot find fragment `CompanyFragment` in this document"),
382+
"{errors}"
383+
);
384+
}

0 commit comments

Comments
 (0)