Skip to content

Commit 23694f8

Browse files
alunyovfacebook-github-bot
authored andcommitted
Refactoring of the type generation/type assertion for provided variables to support internal use case with shared artifacts.
Differential Revision: D48801823 fbshipit-source-id: a556107a34b040935bb7c9d417492e4a031e5d90
1 parent 1ef73b2 commit 23694f8

24 files changed

+220
-188
lines changed

compiler/crates/relay-codegen/src/build_ast.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,15 @@ use crate::ast::QueryID;
7575
use crate::ast::RequestParameters;
7676
use crate::constants::CODEGEN_CONSTANTS;
7777
use crate::object;
78-
use crate::top_level_statements::TopLevelStatements;
7978

8079
pub fn build_request_params_ast_key(
8180
schema: &SDLSchema,
8281
request_parameters: RequestParameters<'_>,
8382
ast_builder: &mut AstBuilder,
8483
operation: &OperationDefinition,
85-
top_level_statements: &TopLevelStatements,
8684
definition_source_location: WithLocation<StringKey>,
8785
project_config: &ProjectConfig,
86+
maybe_provided_variables: Option<AstKey>,
8887
) -> AstKey {
8988
let mut operation_builder = CodegenBuilder::new(
9089
schema,
@@ -93,7 +92,11 @@ pub fn build_request_params_ast_key(
9392
project_config,
9493
definition_source_location,
9594
);
96-
operation_builder.build_request_parameters(operation, request_parameters, top_level_statements)
95+
operation_builder.build_request_parameters(
96+
operation,
97+
request_parameters,
98+
maybe_provided_variables,
99+
)
97100
}
98101

99102
pub fn build_provided_variables(
@@ -1837,7 +1840,7 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
18371840
&mut self,
18381841
operation: &OperationDefinition,
18391842
request_parameters: RequestParameters<'_>,
1840-
top_level_statements: &TopLevelStatements,
1843+
maybe_provided_variables: Option<AstKey>,
18411844
) -> AstKey {
18421845
let mut metadata_items: Vec<ObjectEntry> = operation
18431846
.directives
@@ -1926,20 +1929,10 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
19261929
},
19271930
});
19281931

1929-
let provided_variables = if top_level_statements
1930-
.contains(CODEGEN_CONSTANTS.provided_variables_definition.lookup())
1931-
{
1932-
Some(Primitive::Variable(
1933-
CODEGEN_CONSTANTS.provided_variables_definition,
1934-
))
1935-
} else {
1936-
self.build_operation_provided_variables(operation)
1937-
.map(Primitive::Key)
1938-
};
1939-
if let Some(value) = provided_variables {
1932+
if let Some(provided_variables) = maybe_provided_variables {
19401933
params_object.push(ObjectEntry {
19411934
key: CODEGEN_CONSTANTS.provided_variables,
1942-
value,
1935+
value: Primitive::Key(provided_variables),
19431936
});
19441937
}
19451938

compiler/crates/relay-codegen/src/constants.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ pub struct CodegenConstants {
9090
pub passing_value: StringKey,
9191
pub path: StringKey,
9292
pub plural: StringKey,
93-
pub provided_variables_definition: StringKey,
9493
pub provided_variables: StringKey,
9594
pub provider: StringKey,
9695
pub query: StringKey,
@@ -200,7 +199,6 @@ lazy_static! {
200199
passing_value: "passingValue".intern(),
201200
path: "path".intern(),
202201
plural: "plural".intern(),
203-
provided_variables_definition: "providedVariablesDefinition".intern(),
204202
provided_variables: "providedVariables".intern(),
205203
provider: "provider".intern(),
206204
query: "query".intern(),

compiler/crates/relay-codegen/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub use build_ast::CodegenVariant;
2828
pub use constants::CODEGEN_CONSTANTS;
2929
pub use printer::print_fragment;
3030
pub use printer::print_operation;
31+
pub use printer::print_provided_variables;
3132
pub use printer::print_request;
3233
pub use printer::print_request_params;
3334
pub use printer::JSONPrinter;

compiler/crates/relay-codegen/src/printer.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ pub fn print_request(
7575
request_parameters: RequestParameters<'_>,
7676
project_config: &ProjectConfig,
7777
top_level_statements: &mut TopLevelStatements,
78+
maybe_provided_variables: Option<AstKey>,
7879
) -> String {
7980
Printer::without_dedupe(project_config).print_request(
8081
schema,
8182
operation,
8283
fragment,
8384
request_parameters,
8485
top_level_statements,
86+
maybe_provided_variables,
8587
)
8688
}
8789

@@ -94,21 +96,45 @@ pub fn print_request_params(
9496
) -> String {
9597
let mut request_parameters = build_request_params(operation);
9698
request_parameters.id = query_id;
97-
9899
let mut builder = AstBuilder::default();
100+
let maybe_provided_variables = build_provided_variables(
101+
schema,
102+
&mut builder,
103+
operation,
104+
operation.name.map(|x| x.0),
105+
project_config,
106+
);
99107
let request_parameters_ast_key = build_request_params_ast_key(
100108
schema,
101109
request_parameters,
102110
&mut builder,
103111
operation,
104-
top_level_statements,
105112
operation.name.map(|x| x.0),
106113
project_config,
114+
maybe_provided_variables,
107115
);
108116
let printer = JSONPrinter::new(&builder, project_config, top_level_statements);
109117
printer.print(request_parameters_ast_key, false)
110118
}
111119

120+
pub fn print_provided_variables(
121+
schema: &SDLSchema,
122+
operation: &OperationDefinition,
123+
project_config: &ProjectConfig,
124+
) -> Option<String> {
125+
let mut top_level_statements = Default::default();
126+
let mut builder = AstBuilder::default();
127+
let maybe_provided_variables = build_provided_variables(
128+
schema,
129+
&mut builder,
130+
operation,
131+
operation.name.map(|x| x.0),
132+
project_config,
133+
)?;
134+
let printer = JSONPrinter::new(&builder, project_config, &mut top_level_statements);
135+
Some(printer.print(maybe_provided_variables, false))
136+
}
137+
112138
pub struct Printer<'p> {
113139
project_config: &'p ProjectConfig,
114140
builder: AstBuilder,
@@ -132,21 +158,27 @@ impl<'p> Printer<'p> {
132158
}
133159
}
134160

135-
pub fn print_provided_variables(
161+
pub fn prepare_provided_variables_object(
136162
&mut self,
137163
schema: &SDLSchema,
138164
operation: &OperationDefinition,
139-
top_level_statements: &mut TopLevelStatements,
140-
) -> Option<String> {
141-
let key = build_provided_variables(
165+
) -> Option<AstKey> {
166+
build_provided_variables(
142167
schema,
143168
&mut self.builder,
144169
operation,
145170
operation.name.map(|x| x.0),
146171
self.project_config,
147-
)?;
172+
)
173+
}
174+
175+
pub fn print_provided_variables(
176+
&mut self,
177+
provided_variables: AstKey,
178+
top_level_statements: &mut TopLevelStatements,
179+
) -> Option<String> {
148180
let printer = JSONPrinter::new(&self.builder, self.project_config, top_level_statements);
149-
Some(printer.print(key, self.dedupe))
181+
Some(printer.print(provided_variables, self.dedupe))
150182
}
151183

152184
pub fn print_updatable_query(
@@ -183,15 +215,16 @@ impl<'p> Printer<'p> {
183215
fragment: &FragmentDefinition,
184216
request_parameters: RequestParameters<'_>,
185217
top_level_statements: &mut TopLevelStatements,
218+
maybe_provided_variables: Option<AstKey>,
186219
) -> String {
187220
let request_parameters = build_request_params_ast_key(
188221
schema,
189222
request_parameters,
190223
&mut self.builder,
191224
operation,
192-
top_level_statements,
193225
operation.name.map(|x| x.0),
194226
self.project_config,
227+
maybe_provided_variables,
195228
);
196229

197230
let key = build_request(
@@ -247,15 +280,16 @@ impl<'p> Printer<'p> {
247280
request_parameters: RequestParameters<'_>,
248281
operation: &OperationDefinition,
249282
top_level_statements: &mut TopLevelStatements,
283+
maybe_provided_variables: Option<AstKey>,
250284
) -> String {
251285
let key = build_request_params_ast_key(
252286
schema,
253287
request_parameters,
254288
&mut self.builder,
255289
operation,
256-
top_level_statements,
257290
operation.name.map(|x| x.0),
258291
self.project_config,
292+
maybe_provided_variables,
259293
);
260294
let printer = JSONPrinter::new(&self.builder, self.project_config, top_level_statements);
261295
printer.print(key, self.dedupe)

compiler/crates/relay-compiler/src/artifact_content/content.rs

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,9 @@ use common::NamedItem;
1414
use graphql_ir::FragmentDefinition;
1515
use graphql_ir::FragmentDefinitionName;
1616
use graphql_ir::OperationDefinition;
17-
use intern::Lookup;
1817
use relay_codegen::build_request_params;
1918
use relay_codegen::Printer;
2019
use relay_codegen::QueryID;
21-
use relay_codegen::TopLevelStatement;
22-
use relay_codegen::CODEGEN_CONSTANTS;
2320
use relay_transforms::is_operation_preloadable;
2421
use relay_transforms::RelayDataDrivenDependencyMetadata;
2522
use relay_transforms::ASSIGNABLE_DIRECTIVE;
@@ -111,6 +108,7 @@ pub fn generate_updatable_query(
111108
schema,
112109
project_config,
113110
fragment_locations,
111+
None, // TODO: Add/investigrate support for provided variables in updatable queries
114112
)
115113
)?;
116114
}
@@ -129,7 +127,7 @@ pub fn generate_updatable_query(
129127
&project_config.typegen_config.language,
130128
&mut section,
131129
"node",
132-
generated_types.ast_type,
130+
Some(generated_types.ast_type),
133131
&request,
134132
)?;
135133
content_sections.push(ContentSection::Generic(section));
@@ -264,7 +262,16 @@ pub fn generate_operation(
264262
"relay-runtime",
265263
)?;
266264

265+
// -- Generate provided variables --
266+
let mut top_level_statements = Default::default();
267+
let maybe_provided_variables =
268+
printer.prepare_provided_variables_object(schema, normalization_operation);
269+
267270
if !skip_types {
271+
let provided_variables_text = maybe_provided_variables.and_then(|provided_variables| {
272+
printer.print_provided_variables(provided_variables, &mut top_level_statements)
273+
});
274+
268275
write!(
269276
section,
270277
"{}",
@@ -274,6 +281,7 @@ pub fn generate_operation(
274281
schema,
275282
project_config,
276283
fragment_locations,
284+
provided_variables_text,
277285
)
278286
)?;
279287
}
@@ -286,44 +294,26 @@ pub fn generate_operation(
286294

287295
// -- Begin Top Level Statements Section --
288296
let mut section = GenericSection::default();
289-
let mut top_level_statements = Default::default();
290-
if let Some(provided_variables) =
291-
printer.print_provided_variables(schema, normalization_operation, &mut top_level_statements)
292-
{
293-
let mut provided_variable_text = String::new();
294-
write_variable_value_with_type(
295-
&project_config.typegen_config.language,
296-
&mut provided_variable_text,
297-
CODEGEN_CONSTANTS.provided_variables_definition.lookup(),
298-
relay_typegen::PROVIDED_VARIABLE_TYPE,
299-
&provided_variables,
300-
)
301-
.unwrap();
302-
top_level_statements.insert(
303-
CODEGEN_CONSTANTS.provided_variables_definition.to_string(),
304-
TopLevelStatement::VariableDefinition(provided_variable_text),
305-
);
306-
}
297+
write!(section, "{}", &top_level_statements)?;
298+
content_sections.push(ContentSection::Generic(section));
299+
// -- End Top Level Statements Section --
307300

301+
// -- Begin Query Node Section --
308302
let request = printer.print_request(
309303
schema,
310304
normalization_operation,
311305
&operation_fragment,
312306
request_parameters,
313307
&mut top_level_statements,
308+
maybe_provided_variables,
314309
);
315310

316-
write!(section, "{}", &top_level_statements)?;
317-
content_sections.push(ContentSection::Generic(section));
318-
// -- End Top Level Statements Section --
319-
320-
// -- Begin Query Node Section --
321311
let mut section = GenericSection::default();
322312
write_variable_value_with_type(
323313
&project_config.typegen_config.language,
324314
&mut section,
325315
"node",
326-
generated_types.ast_type,
316+
Some(generated_types.ast_type),
327317
&request,
328318
)?;
329319
content_sections.push(ContentSection::Generic(section));
@@ -469,7 +459,7 @@ pub fn generate_split_operation(
469459
&project_config.typegen_config.language,
470460
&mut section,
471461
"node",
472-
"NormalizationSplitOperation",
462+
Some("NormalizationSplitOperation"),
473463
&operation,
474464
)?;
475465
content_sections.push(ContentSection::Generic(section));
@@ -631,7 +621,7 @@ fn generate_read_only_fragment(
631621
&project_config.typegen_config.language,
632622
&mut section,
633623
"node",
634-
generated_types.ast_type,
624+
Some(generated_types.ast_type),
635625
&fragment,
636626
)?;
637627
content_sections.push(ContentSection::Generic(section));
@@ -726,7 +716,7 @@ fn generate_assignable_fragment(
726716
&project_config.typegen_config.language,
727717
&mut section,
728718
"node",
729-
"any",
719+
Some("any"),
730720
"{}",
731721
)?;
732722
content_sections.push(ContentSection::Generic(section));
@@ -774,17 +764,17 @@ fn write_variable_value_with_type(
774764
language: &TypegenLanguage,
775765
section: &mut dyn Write,
776766
variable_name: &str,
777-
type_: &str,
767+
type_: Option<&str>,
778768
value: &str,
779769
) -> FmtResult {
780-
match language {
781-
TypegenLanguage::JavaScript => writeln!(section, "var {} = {};", variable_name, value),
782-
TypegenLanguage::Flow => {
770+
match (language, type_) {
771+
(TypegenLanguage::Flow, Some(type_)) => {
783772
writeln!(section, "var {}/*: {}*/ = {};", variable_name, type_, value)
784773
}
785-
TypegenLanguage::TypeScript => {
774+
(TypegenLanguage::TypeScript, Some(type_)) => {
786775
writeln!(section, "const {}: {} = {};", variable_name, type_, value)
787776
}
777+
(_, _) => writeln!(section, "var {} = {};", variable_name, value),
788778
}
789779
}
790780

compiler/crates/relay-typegen/src/flow.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ impl Writer for FlowPrinter {
8585
"FragmentType"
8686
}
8787

88-
fn write_local_type(&mut self, name: &str, value: &AST) -> FmtResult {
89-
write!(&mut self.result, "type {} = ", name)?;
88+
fn write_type_assertion(&mut self, name: &str, value: &AST) -> FmtResult {
89+
write!(&mut self.result, "({}: ", name)?;
9090
self.write(value)?;
91-
writeln!(&mut self.result, ";")
91+
writeln!(&mut self.result, ");")
9292
}
9393

9494
fn write_export_type(&mut self, name: &str, value: &AST) -> FmtResult {

compiler/crates/relay-typegen/src/javascript.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Writer for JavaScriptPrinter {
3535
""
3636
}
3737

38-
fn write_local_type(&mut self, _name: &str, _value: &AST) -> FmtResult {
38+
fn write_type_assertion(&mut self, _name: &str, _value: &AST) -> FmtResult {
3939
Ok(())
4040
}
4141

0 commit comments

Comments
 (0)