Skip to content

Commit f45b35f

Browse files
alunyovfacebook-github-bot
authored andcommitted
new feature flag that will allow to opt-out @fetchable types from the node query generation in @refetchable
Reviewed By: tyao1 Differential Revision: D58039454 fbshipit-source-id: 60fe22bf44fcafe767515387de6983d5e2b93546
1 parent f0b17b0 commit f45b35f

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

compiler/crates/common/src/feature_flags.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ pub struct FeatureFlags {
113113
/// in the schema.
114114
#[serde(default)]
115115
pub disallow_required_on_non_null_fields: bool,
116+
117+
/// Feature flag to prefer `fetch_MyType()` generatior over `node()` query generator
118+
/// in @refetchable transform
119+
#[serde(default)]
120+
pub prefer_fetchable_in_refetch_queries: bool,
116121
}
117122

118123
fn default_as_true() -> bool {

compiler/crates/relay-transforms/src/apply_transforms.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,7 @@ fn apply_common_transforms(
184184
transform_subscriptions(&program)
185185
})?;
186186
program = log_event.time("transform_refetchable_fragment", || {
187-
transform_refetchable_fragment(
188-
&program,
189-
&project_config.schema_config,
190-
&base_fragment_names,
191-
false,
192-
)
187+
transform_refetchable_fragment(&program, project_config, &base_fragment_names, false)
193188
})?;
194189

195190
program = log_event.time("relay_actor_change_transform", || {
@@ -734,12 +729,7 @@ fn apply_typegen_transforms(
734729
})?;
735730
log_event.time("flatten", || flatten(&mut program, false, false))?;
736731
program = log_event.time("transform_refetchable_fragment", || {
737-
transform_refetchable_fragment(
738-
&program,
739-
&project_config.schema_config,
740-
&base_fragment_names,
741-
true,
742-
)
732+
transform_refetchable_fragment(&program, project_config, &base_fragment_names, true)
743733
})?;
744734
program = log_event.time("remove_base_fragments", || {
745735
remove_base_fragments(&program, &base_fragment_names)

compiler/crates/relay-transforms/src/client_edges.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ impl<'program, 'pc> ClientEdgesTransform<'program, 'pc> {
268268
selections,
269269
};
270270

271-
let mut transformer =
272-
RefetchableFragment::new(self.program, &self.project_config.schema_config, false);
271+
let mut transformer = RefetchableFragment::new(self.program, self.project_config, false);
273272

274273
let refetchable_fragment = transformer
275274
.transform_refetch_fragment_with_refetchable_directive(

compiler/crates/relay-transforms/src/refetchable_fragment.rs

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use graphql_syntax::OperationKind;
3535
use intern::string_key::StringKeyMap;
3636
use node_query_generator::NODE_QUERY_GENERATOR;
3737
use query_query_generator::QUERY_QUERY_GENERATOR;
38+
use relay_config::ProjectConfig;
3839
use relay_config::SchemaConfig;
3940
use schema::SDLSchema;
4041
use schema::Schema;
@@ -70,13 +71,13 @@ use crate::root_variables::VariableMap;
7071
/// Fragment to Root IR nodes.
7172
pub fn transform_refetchable_fragment(
7273
program: &Program,
73-
schema_config: &SchemaConfig,
74+
project_config: &ProjectConfig,
7475
base_fragment_names: &'_ FragmentDefinitionNameSet,
7576
for_typegen: bool,
7677
) -> DiagnosticsResult<Program> {
7778
let mut next_program = Program::new(Arc::clone(&program.schema));
7879

79-
let mut transformer = RefetchableFragment::new(program, schema_config, for_typegen);
80+
let mut transformer = RefetchableFragment::new(program, project_config, for_typegen);
8081

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

114115
type ExistingRefetchOperations = StringKeyMap<WithLocation<FragmentDefinitionName>>;
115116

116-
pub struct RefetchableFragment<'program, 'sc> {
117+
pub struct RefetchableFragment<'program, 'pc> {
117118
connection_constants: ConnectionConstants,
118119
existing_refetch_operations: ExistingRefetchOperations,
119120
for_typegen: bool,
120121
program: &'program Program,
121-
schema_config: &'sc SchemaConfig,
122+
project_config: &'pc ProjectConfig,
122123
}
123124

124-
impl<'program, 'sc> RefetchableFragment<'program, 'sc> {
125+
impl<'program, 'pc> RefetchableFragment<'program, 'pc> {
125126
pub fn new(
126127
program: &'program Program,
127-
schema_config: &'sc SchemaConfig,
128+
project_config: &'pc ProjectConfig,
128129
for_typegen: bool,
129130
) -> Self {
130131
RefetchableFragment {
131132
connection_constants: Default::default(),
132133
existing_refetch_operations: Default::default(),
133134
for_typegen,
134135
program,
135-
schema_config,
136+
project_config,
136137
}
137138
}
138139

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

173-
for generator in GENERATORS.iter() {
174+
let generators = get_query_generators(self.project_config);
175+
176+
for generator in generators {
174177
if let Some(refetch_root) = (generator.build_refetch_operation)(
175178
&self.program.schema,
176-
self.schema_config,
179+
&self.project_config.schema_config,
177180
fragment,
178181
refetchable_directive.query_name.item,
179182
&variables_map,
@@ -185,7 +188,7 @@ impl<'program, 'sc> RefetchableFragment<'program, 'sc> {
185188
}
186189
}
187190
let mut descriptions = String::new();
188-
for generator in GENERATORS.iter() {
191+
for generator in generators.iter() {
189192
writeln!(descriptions, " - {}", generator.description).unwrap();
190193
}
191194
descriptions.pop();
@@ -367,6 +370,24 @@ const GENERATORS: [QueryGenerator; 4] = [
367370
FETCHABLE_QUERY_GENERATOR,
368371
];
369372

373+
const PREFER_FETCHABLE_GENERATORS: [QueryGenerator; 4] = [
374+
VIEWER_QUERY_GENERATOR,
375+
QUERY_QUERY_GENERATOR,
376+
FETCHABLE_QUERY_GENERATOR,
377+
NODE_QUERY_GENERATOR,
378+
];
379+
380+
fn get_query_generators(project_config: &ProjectConfig) -> &'static [QueryGenerator; 4] {
381+
if project_config
382+
.feature_flags
383+
.prefer_fetchable_in_refetch_queries
384+
{
385+
&PREFER_FETCHABLE_GENERATORS
386+
} else {
387+
&GENERATORS
388+
}
389+
}
390+
370391
pub struct RefetchRoot {
371392
pub fragment: Arc<FragmentDefinition>,
372393
pub selections: Vec<Selection>,

compiler/crates/relay-transforms/src/refetchable_fragment/node_query_generator.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ fn build_refetch_operation(
5353
variables_map: &VariableMap,
5454
) -> DiagnosticsResult<Option<RefetchRoot>> {
5555
let id_name = schema_config.node_interface_id_field;
56-
5756
let node_interface_id = schema.get_type(CONSTANTS.node_type_name).and_then(|type_| {
5857
if let Type::Interface(id) = type_ {
5958
Some(id)

0 commit comments

Comments
 (0)