Skip to content

Commit

Permalink
new feature flag that will allow to opt-out @fetchable types from the…
Browse files Browse the repository at this point in the history
… `node` query generation in @refetchable

Reviewed By: tyao1

Differential Revision: D58039454

fbshipit-source-id: 60fe22bf44fcafe767515387de6983d5e2b93546
  • Loading branch information
alunyov authored and facebook-github-bot committed Jun 3, 2024
1 parent f0b17b0 commit f45b35f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 25 deletions.
5 changes: 5 additions & 0 deletions compiler/crates/common/src/feature_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ pub struct FeatureFlags {
/// in the schema.
#[serde(default)]
pub disallow_required_on_non_null_fields: bool,

/// Feature flag to prefer `fetch_MyType()` generatior over `node()` query generator
/// in @refetchable transform
#[serde(default)]
pub prefer_fetchable_in_refetch_queries: bool,
}

fn default_as_true() -> bool {
Expand Down
14 changes: 2 additions & 12 deletions compiler/crates/relay-transforms/src/apply_transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,7 @@ fn apply_common_transforms(
transform_subscriptions(&program)
})?;
program = log_event.time("transform_refetchable_fragment", || {
transform_refetchable_fragment(
&program,
&project_config.schema_config,
&base_fragment_names,
false,
)
transform_refetchable_fragment(&program, project_config, &base_fragment_names, false)
})?;

program = log_event.time("relay_actor_change_transform", || {
Expand Down Expand Up @@ -734,12 +729,7 @@ fn apply_typegen_transforms(
})?;
log_event.time("flatten", || flatten(&mut program, false, false))?;
program = log_event.time("transform_refetchable_fragment", || {
transform_refetchable_fragment(
&program,
&project_config.schema_config,
&base_fragment_names,
true,
)
transform_refetchable_fragment(&program, project_config, &base_fragment_names, true)
})?;
program = log_event.time("remove_base_fragments", || {
remove_base_fragments(&program, &base_fragment_names)
Expand Down
3 changes: 1 addition & 2 deletions compiler/crates/relay-transforms/src/client_edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ impl<'program, 'pc> ClientEdgesTransform<'program, 'pc> {
selections,
};

let mut transformer =
RefetchableFragment::new(self.program, &self.project_config.schema_config, false);
let mut transformer = RefetchableFragment::new(self.program, self.project_config, false);

let refetchable_fragment = transformer
.transform_refetch_fragment_with_refetchable_directive(
Expand Down
41 changes: 31 additions & 10 deletions compiler/crates/relay-transforms/src/refetchable_fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use graphql_syntax::OperationKind;
use intern::string_key::StringKeyMap;
use node_query_generator::NODE_QUERY_GENERATOR;
use query_query_generator::QUERY_QUERY_GENERATOR;
use relay_config::ProjectConfig;
use relay_config::SchemaConfig;
use schema::SDLSchema;
use schema::Schema;
Expand Down Expand Up @@ -70,13 +71,13 @@ use crate::root_variables::VariableMap;
/// Fragment to Root IR nodes.
pub fn transform_refetchable_fragment(
program: &Program,
schema_config: &SchemaConfig,
project_config: &ProjectConfig,
base_fragment_names: &'_ FragmentDefinitionNameSet,
for_typegen: bool,
) -> DiagnosticsResult<Program> {
let mut next_program = Program::new(Arc::clone(&program.schema));

let mut transformer = RefetchableFragment::new(program, schema_config, for_typegen);
let mut transformer = RefetchableFragment::new(program, project_config, for_typegen);

for operation in program.operations() {
next_program.insert_operation(Arc::clone(operation));
Expand Down Expand Up @@ -113,26 +114,26 @@ pub fn transform_refetchable_fragment(

type ExistingRefetchOperations = StringKeyMap<WithLocation<FragmentDefinitionName>>;

pub struct RefetchableFragment<'program, 'sc> {
pub struct RefetchableFragment<'program, 'pc> {
connection_constants: ConnectionConstants,
existing_refetch_operations: ExistingRefetchOperations,
for_typegen: bool,
program: &'program Program,
schema_config: &'sc SchemaConfig,
project_config: &'pc ProjectConfig,
}

impl<'program, 'sc> RefetchableFragment<'program, 'sc> {
impl<'program, 'pc> RefetchableFragment<'program, 'pc> {
pub fn new(
program: &'program Program,
schema_config: &'sc SchemaConfig,
project_config: &'pc ProjectConfig,
for_typegen: bool,
) -> Self {
RefetchableFragment {
connection_constants: Default::default(),
existing_refetch_operations: Default::default(),
for_typegen,
program,
schema_config,
project_config,
}
}

Expand Down Expand Up @@ -170,10 +171,12 @@ impl<'program, 'sc> RefetchableFragment<'program, 'sc> {
let variables_map =
InferVariablesVisitor::new(self.program).infer_fragment_variables(fragment);

for generator in GENERATORS.iter() {
let generators = get_query_generators(self.project_config);

for generator in generators {
if let Some(refetch_root) = (generator.build_refetch_operation)(
&self.program.schema,
self.schema_config,
&self.project_config.schema_config,
fragment,
refetchable_directive.query_name.item,
&variables_map,
Expand All @@ -185,7 +188,7 @@ impl<'program, 'sc> RefetchableFragment<'program, 'sc> {
}
}
let mut descriptions = String::new();
for generator in GENERATORS.iter() {
for generator in generators.iter() {
writeln!(descriptions, " - {}", generator.description).unwrap();
}
descriptions.pop();
Expand Down Expand Up @@ -367,6 +370,24 @@ const GENERATORS: [QueryGenerator; 4] = [
FETCHABLE_QUERY_GENERATOR,
];

const PREFER_FETCHABLE_GENERATORS: [QueryGenerator; 4] = [
VIEWER_QUERY_GENERATOR,
QUERY_QUERY_GENERATOR,
FETCHABLE_QUERY_GENERATOR,
NODE_QUERY_GENERATOR,
];

fn get_query_generators(project_config: &ProjectConfig) -> &'static [QueryGenerator; 4] {
if project_config
.feature_flags
.prefer_fetchable_in_refetch_queries
{
&PREFER_FETCHABLE_GENERATORS
} else {
&GENERATORS
}
}

pub struct RefetchRoot {
pub fragment: Arc<FragmentDefinition>,
pub selections: Vec<Selection>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ fn build_refetch_operation(
variables_map: &VariableMap,
) -> DiagnosticsResult<Option<RefetchRoot>> {
let id_name = schema_config.node_interface_id_field;

let node_interface_id = schema.get_type(CONSTANTS.node_type_name).and_then(|type_| {
if let Type::Interface(id) = type_ {
Some(id)
Expand Down

0 comments on commit f45b35f

Please sign in to comment.