Skip to content

Commit

Permalink
Minor cleanup in generate_relay_resolvers_operations_for_nested_objects
Browse files Browse the repository at this point in the history
Summary:
* Rename functions for clarity
* Extract code in `generate_fat_selections_from_type` into shared function, which will be called for objects within interfaces in a future diff

Reviewed By: alunyov

Differential Revision: D41330210

fbshipit-source-id: 70e078935639291e0243ce053f5b43f5e408d901
  • Loading branch information
Robert Balicki authored and facebook-github-bot committed Nov 16, 2022
1 parent 2625e95 commit 11d22f0
Showing 1 changed file with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use lazy_static::lazy_static;
use relay_config::SchemaConfig;
use schema::Field;
use schema::FieldID;
use schema::ObjectID;
use schema::SDLSchema;
use schema::Schema;
use schema::Type;
Expand Down Expand Up @@ -63,7 +64,7 @@ fn generate_fat_selections_from_type(

let mut parent_types = HashSet::new();
parent_types.insert(type_);
generate_selections_from_fields(
generate_selections_from_object_fields(
schema,
schema_config,
&object.fields,
Expand Down Expand Up @@ -112,7 +113,7 @@ fn generate_fat_selections_from_type(
}
}

fn generate_selections_from_fields(
fn generate_selections_from_object_fields(
schema: &SDLSchema,
schema_config: &SchemaConfig,
field_ids: &[FieldID],
Expand Down Expand Up @@ -181,35 +182,13 @@ fn generate_selection_from_field(
directives: vec![],
}))),
Type::Object(object_id) => {
let object = schema.object(object_id);
if !object.is_extension {
return Err(vec![Diagnostic::error(
ValidationMessage::RelayResolverServerTypeNotSupported {
field_name: field.name.item,
type_name: schema.get_type_name(type_),
},
field.name.location,
)]);
}

if parent_types.contains(&type_) {
return Err(vec![Diagnostic::error(
ValidationMessage::RelayResolverTypeRecursionDetected {
type_name: schema.get_type_name(type_),
},
field.name.location,
)]);
}

parent_types.insert(type_);
let selections = generate_selections_from_fields(
let selections = generate_selections_from_object(
object_id,
parent_types,
schema,
schema_config,
&object.fields,
parent_types,
field,
)?;
parent_types.remove(&type_);

Ok(Selection::LinkedField(Arc::new(LinkedField {
alias: None,
definition: WithLocation::generated(*field_id),
Expand Down Expand Up @@ -244,6 +223,46 @@ fn generate_selection_from_field(
}
}

fn generate_selections_from_object(
object_id: ObjectID,
parent_types: &mut HashSet<Type>,
schema: &SDLSchema,
schema_config: &SchemaConfig,
field: &Field,
) -> Result<Vec<Selection>, Vec<Diagnostic>> {
let object = schema.object(object_id);
let type_ = Type::Object(object_id);

if !object.is_extension {
return Err(vec![Diagnostic::error(
ValidationMessage::RelayResolverServerTypeNotSupported {
field_name: field.name.item,
type_name: object.name.item.0,
},
field.name.location,
)]);
}

if parent_types.contains(&type_) {
return Err(vec![Diagnostic::error(
ValidationMessage::RelayResolverTypeRecursionDetected {
type_name: object.name.item.0,
},
field.name.location,
)]);
}
parent_types.insert(type_);
let selections = generate_selections_from_object_fields(
schema,
schema_config,
&object.fields,
parent_types,
)?;
parent_types.remove(&type_);

Ok(selections)
}

pub(crate) fn generate_name_for_nested_object_operation(
schema: &SDLSchema,
field: &Field,
Expand Down

0 comments on commit 11d22f0

Please sign in to comment.