Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better locations for URL strings #6031

Merged
merged 7 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ dependencies = [
"insta",
"itertools 0.13.0",
"lazy_static",
"line-col",
"multimap 0.10.0",
"nom",
"nom_locate",
Expand Down Expand Up @@ -3984,6 +3985,12 @@ dependencies = [
"vcpkg",
]

[[package]]
name = "line-col"
pubmodmatt marked this conversation as resolved.
Show resolved Hide resolved
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e69cdf6b85b5c8dce514f694089a2cf8b1a702f6cd28607bcb3cf296c9778db"

[[package]]
name = "linked-hash-map"
version = "0.5.6"
Expand Down
1 change: 1 addition & 0 deletions apollo-federation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ http.workspace = true
indexmap = { version = "2.2.6", features = ["serde"] }
itertools = "0.13.0"
lazy_static = "1.4.0"
line-col = "0.2.1"
multimap = "0.10.0"
nom = "7.1.3"
petgraph = { version = "0.6.4", features = ["serde-1"] }
Expand Down
42 changes: 18 additions & 24 deletions apollo-federation/src/sources/connect/validation/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ use apollo_compiler::ast::InputValueDefinition;
use apollo_compiler::ast::Value;
use apollo_compiler::executable::FieldSet;
use apollo_compiler::executable::Selection;
use apollo_compiler::parser::SourceMap;
use apollo_compiler::schema::Component;
use apollo_compiler::schema::Directive;
use apollo_compiler::schema::ExtendedType;
use apollo_compiler::schema::InputObjectType;
use apollo_compiler::schema::ObjectType;
use apollo_compiler::Name;
use apollo_compiler::Node;
use apollo_compiler::Schema;

use super::coordinates::connect_directive_entity_argument_coordinate;
use super::coordinates::field_with_connect_directive_entity_true_coordinate;
Expand All @@ -23,13 +21,13 @@ use super::Message;
use crate::sources::connect::expand::visitors::FieldVisitor;
use crate::sources::connect::expand::visitors::GroupVisitor;
use crate::sources::connect::spec::schema::CONNECT_ENTITY_ARGUMENT_NAME;
use crate::sources::connect::validation::graphql::SchemaInfo;

pub(super) fn validate_entity_arg(
field: &Component<FieldDefinition>,
connect_directive: &Node<Directive>,
object: &Node<ObjectType>,
schema: &Schema,
source_map: &SourceMap,
schema: &SchemaInfo,
category: ObjectCategory,
) -> Vec<Message> {
let mut messages = vec![];
Expand All @@ -52,7 +50,7 @@ pub(super) fn validate_entity_arg(
"{coordinate} is invalid. Entity resolvers can only be declared on root `Query` fields.",
coordinate = connect_directive_entity_argument_coordinate(connect_directive_name, entity_arg_value.as_ref(), object, &field.name)
),
locations: entity_arg.line_column_range(source_map)
locations: entity_arg.line_column_range(&schema.sources)
.into_iter()
.collect(),
})
Expand All @@ -73,7 +71,7 @@ pub(super) fn validate_entity_arg(
)
),
locations: entity_arg
.line_column_range(source_map)
.line_column_range(&schema.sources)
.into_iter()
.collect(),
})
Expand All @@ -95,7 +93,7 @@ pub(super) fn validate_entity_arg(
),
),
locations: entity_arg
.line_column_range(source_map)
.line_column_range(&schema.sources)
.into_iter()
.collect(),
});
Expand All @@ -104,11 +102,9 @@ pub(super) fn validate_entity_arg(

if let Some(message) = (ArgumentVisitor {
schema,
connect_directive_name,
entity_arg,
entity_arg_value,
object,
source_map,
field: &field.name,
key_fields,
})
Expand Down Expand Up @@ -160,12 +156,10 @@ struct Field<'schema> {
/// Since input types may contain fields with subtypes, and the fields of those subtypes can be
/// part of composite keys, this potentially requires visiting a tree.
struct ArgumentVisitor<'schema> {
schema: &'schema Schema,
connect_directive_name: &'schema Name,
schema: &'schema SchemaInfo<'schema>,
entity_arg: &'schema Node<Argument>,
entity_arg_value: &'schema Node<Value>,
object: &'schema Node<ObjectType>,
source_map: &'schema SourceMap,
field: &'schema Name,
key_fields: Vec<FieldSet>,
}
Expand Down Expand Up @@ -229,7 +223,7 @@ impl<'schema> FieldVisitor<Field<'schema>> for ArgumentVisitor<'schema> {
message: format!(
"{coordinate} has invalid arguments. Mismatched type on field `{field_name}` - expected `{entity_type}` but found `{input_type}`.",
coordinate = field_with_connect_directive_entity_true_coordinate(
self.connect_directive_name,
self.schema.connect_directive_name,
self.entity_arg_value.as_ref(),
self.object,
self.field,
Expand All @@ -239,9 +233,9 @@ impl<'schema> FieldVisitor<Field<'schema>> for ArgumentVisitor<'schema> {
entity_type = field.entity_type.name(),
),
locations: field.node
.line_column_range(self.source_map)
.line_column_range(&self.schema.sources)
.into_iter()
.chain(self.entity_arg.line_column_range(self.source_map))
.chain(self.entity_arg.line_column_range(&self.schema.sources))
.collect(),
})
}
Expand Down Expand Up @@ -292,7 +286,7 @@ impl<'schema> ArgumentVisitor<'schema> {
message: format!(
"{coordinate} has invalid arguments. Argument `{arg_name}` does not have a matching field `{arg_name}` on type `{entity_type}`.",
coordinate = field_with_connect_directive_entity_true_coordinate(
self.connect_directive_name,
self.schema.connect_directive_name,
self.entity_arg_value.as_ref(),
self.object,
&field.name
Expand All @@ -301,9 +295,9 @@ impl<'schema> ArgumentVisitor<'schema> {
entity_type = entity_type.name,
),
locations: arg
.line_column_range(self.source_map)
.line_column_range(&self.schema.sources)
.into_iter()
.chain(self.entity_arg.line_column_range(self.source_map))
.chain(self.entity_arg.line_column_range(&self.schema.sources))
.collect(),
}))
}
Expand Down Expand Up @@ -364,7 +358,7 @@ impl<'schema> ArgumentVisitor<'schema> {
message: format!(
"{coordinate} has invalid arguments. Field `{name}` on `{input_type}` does not have a matching field `{name}` on `{entity_type}`.",
coordinate = field_with_connect_directive_entity_true_coordinate(
self.connect_directive_name,
self.schema.connect_directive_name,
self.entity_arg_value.as_ref(),
self.object,
self.field,
Expand All @@ -373,9 +367,9 @@ impl<'schema> ArgumentVisitor<'schema> {
entity_type = entity_object_type.name,
),
locations: input_field
.line_column_range(self.source_map)
.line_column_range(&self.schema.sources)
.into_iter()
.chain(self.entity_arg.line_column_range(self.source_map))
.chain(self.entity_arg.line_column_range(&self.schema.sources))
.collect(),
}))
}
Expand Down Expand Up @@ -419,16 +413,16 @@ impl<'schema> ArgumentVisitor<'schema> {
None => format!("Argument `{name}`"),
},
coordinate = field_with_connect_directive_entity_true_coordinate(
self.connect_directive_name,
self.schema.connect_directive_name,
self.entity_arg_value.as_ref(),
self.object,
self.field,
),
),
locations: node
.line_column_range(self.source_map)
.line_column_range(&self.schema.sources)
.into_iter()
.chain(self.entity_arg.line_column_range(self.source_map))
.chain(self.entity_arg.line_column_range(&self.schema.sources))
.collect(),
})
} else {
Expand Down
59 changes: 19 additions & 40 deletions apollo-federation/src/sources/connect/validation/extended_type.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
use std::sync::Arc;

use apollo_compiler::ast::FieldDefinition;
use apollo_compiler::collections::IndexMap;
use apollo_compiler::collections::IndexSet;
use apollo_compiler::executable::Selection;
use apollo_compiler::parser::FileId;
use apollo_compiler::parser::SourceFile;
use apollo_compiler::parser::SourceMap;
use apollo_compiler::parser::SourceSpan;
use apollo_compiler::schema::Component;
use apollo_compiler::schema::ExtendedType;
use apollo_compiler::schema::ObjectType;
use apollo_compiler::Name;
use apollo_compiler::Node;
use apollo_compiler::Schema;
use itertools::Itertools;

use super::coordinates::ConnectDirectiveCoordinate;
Expand All @@ -33,33 +27,26 @@ use super::Message;
use crate::sources::connect::spec::schema::CONNECT_BODY_ARGUMENT_NAME;
use crate::sources::connect::spec::schema::CONNECT_SOURCE_ARGUMENT_NAME;
use crate::sources::connect::spec::schema::HTTP_ARGUMENT_NAME;
use crate::sources::connect::validation::graphql::SchemaInfo;

pub(super) fn validate_extended_type(
extended_type: &ExtendedType,
schema: &Schema,
connect_directive_name: &Name,
source_directive_name: &Name,
schema: &SchemaInfo,
all_source_names: &[SourceName],
source_map: &Arc<IndexMap<FileId, Arc<SourceFile>>>,
seen_fields: &mut IndexSet<(Name, Name)>,
) -> Vec<Message> {
match extended_type {
ExtendedType::Object(object) => validate_object_fields(
object,
schema,
connect_directive_name,
source_directive_name,
all_source_names,
seen_fields,
),
ExtendedType::Object(object) => {
validate_object_fields(object, schema, all_source_names, seen_fields)
}
ExtendedType::Union(union_type) => vec![validate_abstract_type(
SourceSpan::recompose(union_type.location(), union_type.name.location()),
source_map,
&schema.sources,
"union",
)],
ExtendedType::Interface(interface) => vec![validate_abstract_type(
SourceSpan::recompose(interface.location(), interface.name.location()),
source_map,
&schema.sources,
"interface",
)],
_ => Vec::new(),
Expand All @@ -77,9 +64,7 @@ pub enum ObjectCategory {
/// are resolvable by some combination of `@connect` directives.
fn validate_object_fields(
object: &Node<ObjectType>,
schema: &Schema,
connect_directive_name: &Name,
source_directive_name: &Name,
schema: &SchemaInfo,
source_names: &[SourceName],
seen_fields: &mut IndexSet<(Name, Name)>,
) -> Vec<Message> {
Expand Down Expand Up @@ -122,7 +107,8 @@ fn validate_object_fields(
return vec![Message {
code: Code::SubscriptionInConnectors,
message: format!(
"A subscription root type is not supported when using `@{connect_directive_name}`."
"A subscription root type is not supported when using `@{connect_directive_name}`.",
connect_directive_name = schema.connect_directive_name,
),
locations: object.line_column_range(source_map).into_iter().collect(),
}];
Expand Down Expand Up @@ -154,37 +140,32 @@ fn validate_object_fields(
object_category,
source_names,
object,
connect_directive_name,
source_directive_name,
schema,
seen_fields,
)
})
.collect()
}

#[allow(clippy::too_many_arguments)]
fn validate_field(
field: &Component<FieldDefinition>,
category: ObjectCategory,
source_names: &[SourceName],
object: &Node<ObjectType>,
connect_directive_name: &Name,
source_directive_name: &Name,
schema: &Schema,
schema: &SchemaInfo,
seen_fields: &mut IndexSet<(Name, Name)>,
) -> Vec<Message> {
let field_coordinate = FieldCoordinate { object, field };
let connect_coordinate = ConnectDirectiveCoordinate {
connect_directive_name,
connect_directive_name: schema.connect_directive_name,
field_coordinate,
};
let source_map = &schema.sources;
let mut errors = Vec::new();
let connect_directives = field
.directives
.iter()
.filter(|directive| directive.name == *connect_directive_name)
.filter(|directive| directive.name == *schema.connect_directive_name)
.collect_vec();

if connect_directives.is_empty() {
Expand All @@ -194,14 +175,14 @@ fn validate_field(
field,
object,
source_map,
connect_directive_name,
schema.connect_directive_name,
)),
ObjectCategory::Mutation => errors.push(get_missing_connect_directive_message(
Code::MutationFieldMissingConnect,
field,
object,
source_map,
connect_directive_name,
schema.connect_directive_name,
)),
_ => (),
}
Expand Down Expand Up @@ -240,7 +221,6 @@ fn validate_field(
connect_directive,
object,
schema,
source_map,
category,
));

Expand Down Expand Up @@ -294,10 +274,8 @@ fn validate_field(
&field.name,
&object.name,
source_name,
source_map,
source_names,
source_directive_name,
&connect_directive.name,
schema,
));

if let Some((template, coordinate)) = url_template {
Expand All @@ -320,7 +298,8 @@ fn validate_field(
code: Code::RelativeConnectUrlWithoutSource,
message: format!(
"{coordinate} specifies the relative URL {raw_value}, but no `{CONNECT_SOURCE_ARGUMENT_NAME}` is defined. Either use an absolute URL including scheme (e.g. https://), or add a `@{source_directive_name}`.",
raw_value = coordinate.node
raw_value = coordinate.node,
source_directive_name = schema.source_directive_name,
),
locations: coordinate.node.line_column_range(source_map).into_iter().collect()
})
Expand All @@ -332,7 +311,7 @@ fn validate_field(
http_arg,
source_map,
HttpHeadersCoordinate::Connect {
directive_name: connect_directive_name,
directive_name: schema.connect_directive_name,
object: &object.name,
field: &field.name,
},
Expand Down
Loading