Minor cleanup in generate_relay_resolvers_operations_for_nested_objects
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
@@ -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;
@@ -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,
@@ -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],
@@ -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),
@@ -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,
