Skip to content
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
14 changes: 8 additions & 6 deletions compiler/crates/relay-transforms/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,25 +223,27 @@ pub enum ValidationMessage {
},

#[error(
"Field '{field_name}' marked with @exhaustive is missing selection for union members: {member_names}."
"Field '{field_name}' marked with @exhaustive is missing selection for {type_description}: {member_names}."
)]
MissingExhaustiveUnionMembersOnField {
MissingExhaustiveMembersOnField {
field_name: StringKey,
member_names: String,
type_description: &'static str,
},

#[error(
"Fragment '{fragment_name}' marked with @exhaustive is missing selection for union members: {member_names}."
"Fragment '{fragment_name}' marked with @exhaustive is missing selection for {type_description}: {member_names}."
)]
MissingExhaustiveUnionMembersOnFragment {
MissingExhaustiveMembersOnFragment {
fragment_name: StringKey,
member_names: String,
type_description: &'static str,
},

#[error(
"The @exhaustive directive can only be applied to fields or fragment definitions returning union types."
"The @exhaustive directive can only be applied to fields or fragment definitions returning union or interface types."
)]
ExhaustiveDirectiveOnNonUnionField,
ExhaustiveDirectiveOnNonUnionOrInterfaceField,
}

#[derive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ use intern::string_key::Intern;
use intern::string_key::StringKey;
use lazy_static::lazy_static;
use relay_config::ProjectConfig;
use schema::InterfaceID;
use schema::ObjectID;
use schema::SDLSchema;
use schema::Schema;
use schema::Type;
use schema::UnionID;

use crate::ValidationMessage;

Expand Down Expand Up @@ -93,9 +95,9 @@ impl<'schema, 'program, 'pc> ExhaustiveDirectiveValidator<'schema, 'program, 'pc
(ignored, disabled)
}

fn check_exhaustive_coverage(
fn check_exhaustive_union_coverage(
&self,
union_id: schema::UnionID,
union_id: UnionID,
ignored: &std::collections::HashSet<StringKey>,
has_selection_fn: impl Fn(ObjectID) -> bool,
) -> Vec<StringKey> {
Expand All @@ -114,6 +116,37 @@ impl<'schema, 'program, 'pc> ExhaustiveDirectiveValidator<'schema, 'program, 'pc
missing_members
}

fn check_exhaustive_interface_coverage(
&self,
interface_id: InterfaceID,
ignored: &std::collections::HashSet<StringKey>,
has_selection_fn: impl Fn(ObjectID) -> bool,
) -> Vec<StringKey> {
let mut implementing_objects = self
.schema
.interface(interface_id)
.recursively_implementing_objects(self.schema.as_ref())
.into_iter()
.collect::<Vec<_>>();
implementing_objects.sort_by_key(|object_id| {
self.schema.get_type_name(Type::Object(*object_id))
});

let mut missing_members = Vec::new();

for object_id in implementing_objects {
let member_name = self.schema.get_type_name(Type::Object(object_id));
if ignored.contains(&member_name) {
continue;
}
if !has_selection_fn(object_id) {
missing_members.push(member_name);
}
}

missing_members
}

fn validate_exhaustive(&mut self, field: &LinkedField) {
let mut ignored = std::collections::HashSet::new();
let mut has_directive = false;
Expand All @@ -138,21 +171,28 @@ impl<'schema, 'program, 'pc> ExhaustiveDirectiveValidator<'schema, 'program, 'pc

let field_def = self.schema.field(field.definition.item);
let return_type = field_def.type_.inner();
let union_id = match return_type {
Type::Union(id) => id,
let (missing_members, type_description) = match return_type {
Type::Union(id) => (
self.check_exhaustive_union_coverage(id, &ignored, |object_id| {
self.has_selection_for_object(field, object_id)
}),
"union members",
),
Type::Interface(id) => (
self.check_exhaustive_interface_coverage(id, &ignored, |object_id| {
self.has_selection_for_object(field, object_id)
}),
"interface implementations",
),
_ => {
self.errors.push(Diagnostic::error(
ValidationMessage::ExhaustiveDirectiveOnNonUnionField,
ValidationMessage::ExhaustiveDirectiveOnNonUnionOrInterfaceField,
field.definition.location,
));
return;
}
};

let missing_members = self.check_exhaustive_coverage(union_id, &ignored, |object_id| {
self.has_selection_for_object(field, object_id)
});

if !missing_members.is_empty() {
let field_name = field.alias.map_or(field_def.name.item, |a| a.item);
let member_names = missing_members
Expand All @@ -162,9 +202,10 @@ impl<'schema, 'program, 'pc> ExhaustiveDirectiveValidator<'schema, 'program, 'pc
.join(", ");

self.errors.push(Diagnostic::error(
ValidationMessage::MissingExhaustiveUnionMembersOnField {
ValidationMessage::MissingExhaustiveMembersOnField {
field_name,
member_names,
type_description,
},
field.definition.location,
));
Expand All @@ -182,21 +223,28 @@ impl<'schema, 'program, 'pc> ExhaustiveDirectiveValidator<'schema, 'program, 'pc
return;
}

let union_id = match fragment.type_condition {
Type::Union(id) => id,
let (missing_members, type_description) = match fragment.type_condition {
Type::Union(id) => (
self.check_exhaustive_union_coverage(id, &ignored, |object_id| {
self.has_selection_for_object_in_fragment(fragment, object_id)
}),
"union members",
),
Type::Interface(id) => (
self.check_exhaustive_interface_coverage(id, &ignored, |object_id| {
self.has_selection_for_object_in_fragment(fragment, object_id)
}),
"interface implementations",
),
_ => {
self.errors.push(Diagnostic::error(
ValidationMessage::ExhaustiveDirectiveOnNonUnionField,
ValidationMessage::ExhaustiveDirectiveOnNonUnionOrInterfaceField,
fragment.name.location,
));
return;
}
};

let missing_members = self.check_exhaustive_coverage(union_id, &ignored, |object_id| {
self.has_selection_for_object_in_fragment(fragment, object_id)
});

if !missing_members.is_empty() {
let fragment_name = fragment.name.item.into();
let member_names = missing_members
Expand All @@ -206,9 +254,10 @@ impl<'schema, 'program, 'pc> ExhaustiveDirectiveValidator<'schema, 'program, 'pc
.join(", ");

self.errors.push(Diagnostic::error(
ValidationMessage::MissingExhaustiveUnionMembersOnFragment {
ValidationMessage::MissingExhaustiveMembersOnFragment {
fragment_name,
member_names,
type_description,
},
fragment.name.location,
));
Expand Down Expand Up @@ -295,7 +344,7 @@ impl Validator for ExhaustiveDirectiveValidator<'_, '_, '_> {
fn validate_scalar_field(&mut self, field: &ScalarField) -> DiagnosticsResult<()> {
if field.directives.named(*EXHAUSTIVE_DIRECTIVE_NAME).is_some() {
self.errors.push(Diagnostic::error(
ValidationMessage::ExhaustiveDirectiveOnNonUnionField,
ValidationMessage::ExhaustiveDirectiveOnNonUnionOrInterfaceField,
field.definition.location,
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fragment DirectiveOnNonUnion on User {
name @exhaustive
}
==================================== ERROR ====================================
✖︎ The @exhaustive directive can only be applied to fields or fragment definitions returning union types.
✖︎ The @exhaustive directive can only be applied to fields or fragment definitions returning union or interface types.

directive-on-non-union.invalid.graphql:3:3
2 │ fragment DirectiveOnNonUnion on User {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
==================================== INPUT ====================================
fragment InterfaceAllMembersValid on User {
nameRenderable @exhaustive {
... on PlainUserNameRenderer {
__typename
}
... on MarkdownUserNameRenderer {
__typename
}
... on ImplementsImplementsUserNameRenderable {
__typename
}
... on ImplementsImplementsUserNameRenderableAndUserNameRenderable {
__typename
}
}
}
==================================== OUTPUT ===================================
fragment InterfaceAllMembersValid on User {
nameRenderable @exhaustive {
... on PlainUserNameRenderer {
__typename
}
... on MarkdownUserNameRenderer {
__typename
}
... on ImplementsImplementsUserNameRenderable {
__typename
}
... on ImplementsImplementsUserNameRenderableAndUserNameRenderable {
__typename
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
fragment InterfaceAllMembersValid on User {
nameRenderable @exhaustive {
... on PlainUserNameRenderer {
__typename
}
... on MarkdownUserNameRenderer {
__typename
}
... on ImplementsImplementsUserNameRenderable {
__typename
}
... on ImplementsImplementsUserNameRenderableAndUserNameRenderable {
__typename
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
==================================== INPUT ====================================
#expected-to-throw
fragment InterfaceMissingMember on User {
nameRenderable @exhaustive {
... on PlainUserNameRenderer { __typename }
... on MarkdownUserNameRenderer { __typename }
... on ImplementsImplementsUserNameRenderable { __typename }
}
}
==================================== ERROR ====================================
✖︎ Field 'nameRenderable' marked with @exhaustive is missing selection for interface implementations: 'ImplementsImplementsUserNameRenderableAndUserNameRenderable'.

interface-missing-member.invalid.graphql:3:3
2 │ fragment InterfaceMissingMember on User {
3 │ nameRenderable @exhaustive {
│ ^^^^^^^^^^^^^^
4 │ ... on PlainUserNameRenderer { __typename }
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fragment InterfaceMissingMember on User {
nameRenderable @exhaustive {
... on PlainUserNameRenderer { __typename }
... on MarkdownUserNameRenderer { __typename }
... on ImplementsImplementsUserNameRenderable { __typename }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,41 @@ async fn multiple_missing_members() {
)
.await;
}

#[tokio::test]
async fn interface_all_members_valid() {
let input = include_str!(
"validate_exhaustive_directive/fixtures/interface-all-members-valid.graphql"
);
let expected = include_str!(
"validate_exhaustive_directive/fixtures/interface-all-members-valid.expected"
);
test_fixture(
transform_fixture,
file!(),
"interface-all-members-valid.graphql",
"validate_exhaustive_directive/fixtures/interface-all-members-valid.expected",
input,
expected,
)
.await;
}

#[tokio::test]
async fn interface_missing_member() {
let input = include_str!(
"validate_exhaustive_directive/fixtures/interface-missing-member.invalid.graphql"
);
let expected = include_str!(
"validate_exhaustive_directive/fixtures/interface-missing-member.invalid.expected"
);
test_fixture(
transform_fixture,
file!(),
"interface-missing-member.invalid.graphql",
"validate_exhaustive_directive/fixtures/interface-missing-member.invalid.expected",
input,
expected,
)
.await;
}
Loading