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

Add a graphql-js compatible-ish error serialization for Apollo Router #853

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions crates/apollo-compiler/src/executable/from_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub(crate) fn document_from_ast(
errors.errors.push(
definition.location(),
BuildError::TypeSystemDefinition {
name: definition.name().cloned(),
describe: definition.describe(),
},
)
Expand Down
5 changes: 4 additions & 1 deletion crates/apollo-compiler/src/executable/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ pub struct InlineFragment {
#[derive(thiserror::Error, Debug, Clone)]
pub(crate) enum BuildError {
#[error("an executable document must not contain {describe}")]
TypeSystemDefinition { describe: &'static str },
TypeSystemDefinition {
name: Option<Name>,
describe: &'static str,
},

#[error("anonymous operation cannot be selected when the document contains other operations")]
AmbiguousAnonymousOperation,
Expand Down
22 changes: 13 additions & 9 deletions crates/apollo-compiler/src/validation/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ pub(crate) enum DiagnosticData {
RequiredArgument {
name: Name,
coordinate: SchemaCoordinate,
expected_type: Node<Type>,
definition_location: Option<NodeLocation>,
},
#[error("the required field `{coordinate}` is not provided")]
RequiredField {
name: Name,
coordinate: TypeAttributeCoordinate,
expected_type: Node<Type>,
definition_location: Option<NodeLocation>,
},
#[error(
Expand Down Expand Up @@ -130,9 +132,9 @@ pub(crate) enum DiagnosticData {
VariableInputType {
/// Variable name.
name: Name,
ty: Node<Type>,
/// The kind of type that the variable is declared with.
describe_type: &'static str,
type_location: Option<NodeLocation>,
},
#[error("missing query root operation type in schema definition")]
QueryRootOperationType,
Expand Down Expand Up @@ -163,12 +165,12 @@ pub(crate) enum DiagnosticData {
/// The source location where the directive that's being used was defined.
definition_location: Option<NodeLocation>,
},
#[error("expected value of type {ty}, found {describe_value_type}")]
#[error("expected value of type {ty}, found {}", .value.describe())]
UnsupportedValueType {
/// The kind of value provided.
describe_value_type: &'static str,
/// The actual value provided
value: Node<ast::Value>,
/// Expected concrete type
ty: String,
ty: Node<Type>,
definition_location: Option<NodeLocation>,
},
#[error("int cannot represent non 32-bit signed integer value")]
Expand Down Expand Up @@ -352,6 +354,7 @@ impl DiagnosticData {
DiagnosticData::RequiredArgument {
name,
coordinate: _,
expected_type: _,
definition_location,
} => {
report.with_label_opt(
Expand All @@ -363,6 +366,7 @@ impl DiagnosticData {
DiagnosticData::RequiredField {
name,
coordinate: _,
expected_type: _,
definition_location,
} => {
report.with_label_opt(
Expand Down Expand Up @@ -528,11 +532,11 @@ impl DiagnosticData {
}
DiagnosticData::VariableInputType {
name: _,
ty,
describe_type,
type_location,
} => {
report.with_label_opt(
type_location.or(main_location),
ty.location().or(main_location),
format_args!("this is {describe_type}"),
);
report.with_help("objects, unions, and interfaces cannot be used because variables can only be of input type");
Expand Down Expand Up @@ -560,13 +564,13 @@ impl DiagnosticData {
));
}
DiagnosticData::UnsupportedValueType {
describe_value_type,
value,
ty,
definition_location,
} => {
report.with_label_opt(
main_location,
format_args!("provided value is {describe_value_type}"),
format_args!("provided value is {}", value.describe()),
);
report.with_label_opt(
*definition_location,
Expand Down
1 change: 1 addition & 0 deletions crates/apollo-compiler/src/validation/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ pub(crate) fn validate_directives<'dir>(
dir.location(),
DiagnosticData::RequiredArgument {
name: arg_def.name.clone(),
expected_type: arg_def.ty.clone(),
coordinate: DirectiveArgumentCoordinate {
directive: directive_definition.name.clone(),
argument: arg_def.name.clone(),
Expand Down
1 change: 1 addition & 0 deletions crates/apollo-compiler/src/validation/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub(crate) fn validate_field(
field.location(),
DiagnosticData::RequiredArgument {
name: arg_definition.name.clone(),
expected_type: arg_definition.ty.clone(),
coordinate: FieldArgumentCoordinate {
ty: against_type.clone(),
field: field.name.clone(),
Expand Down
Loading