Skip to content

Commit

Permalink
Provide single-character locations for selection parse errors (#6241)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-apollo authored Nov 7, 2024
1 parent 6cf67ed commit 6ff695e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
18 changes: 8 additions & 10 deletions apollo-federation/src/sources/connect/validation/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::ops::Range;
use apollo_compiler::ast::FieldDefinition;
use apollo_compiler::collections::IndexSet;
use apollo_compiler::parser::LineColumn;
use apollo_compiler::parser::SourceMap;
use apollo_compiler::schema::Component;
use apollo_compiler::schema::Directive;
use apollo_compiler::schema::ExtendedType;
Expand Down Expand Up @@ -36,7 +35,7 @@ pub(super) fn validate_selection(
schema: &SchemaInfo,
seen_fields: &mut IndexSet<(Name, Name)>,
) -> Result<(), Message> {
let (selection_arg, json_selection) = get_json_selection(coordinate, &schema.sources)?;
let (selection_arg, json_selection) = get_json_selection(coordinate, schema)?;
let field = coordinate.field_coordinate.field;

let Some(return_type) = schema.get_object(field.ty.inner_named_type()) else {
Expand Down Expand Up @@ -102,7 +101,7 @@ pub(super) fn validate_body_selection(

fn get_json_selection<'a>(
connect_directive: ConnectDirectiveCoordinate<'a>,
source_map: &'a SourceMap,
schema: &'a SchemaInfo<'a>,
) -> Result<(SelectionArg<'a>, JSONSelection), Message> {
let coordinate = SelectionCoordinate::from(connect_directive);
let selection_arg = connect_directive
Expand All @@ -115,26 +114,25 @@ fn get_json_selection<'a>(
message: format!("{coordinate} is required."),
locations: connect_directive
.directive
.line_column_range(source_map)
.line_column_range(&schema.sources)
.into_iter()
.collect(),
})?;
let selection_str =
GraphQLString::new(&selection_arg.value, source_map).map_err(|_| Message {
GraphQLString::new(&selection_arg.value, &schema.sources).map_err(|_| Message {
code: Code::GraphQLError,
message: format!("{coordinate} must be a string."),
locations: selection_arg
.line_column_range(source_map)
.line_column_range(&schema.sources)
.into_iter()
.collect(),
})?;

let selection = JSONSelection::parse(selection_str.as_str()).map_err(|err| Message {
code: Code::InvalidJsonSelection,
message: format!("{coordinate} is not a valid JSONSelection: {err}",),
locations: selection_arg
.value
.line_column_range(source_map)
locations: selection_str
.line_col_for_subslice(err.offset..err.offset + 1, schema)
.into_iter()
.collect(),
})?;
Expand All @@ -145,7 +143,7 @@ fn get_json_selection<'a>(
message: format!("{coordinate} is empty",),
locations: selection_arg
.value
.line_column_range(source_map)
.line_column_range(&schema.sources)
.into_iter()
.collect(),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ input_file: apollo-federation/src/sources/connect/validation/test_data/invalid_s
code: InvalidJsonSelection,
message: "`@connect(selection:)` on `Query.something` is not a valid JSONSelection: nom::error::ErrorKind::Eof at offset 0: &how",
locations: [
8:86..8:92,
8:87..8:88,
],
},
]

0 comments on commit 6ff695e

Please sign in to comment.