Skip to content

Commit d1f3f8b

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. (facebook#4429)
Summary: Pull Request resolved: facebook#4429 Differential Revision: D48801823 Pulled By: alunyov fbshipit-source-id: bcfd9ee731f9314f46d0ccb6953d1e8015c2bda9
1 parent 1ef73b2 commit d1f3f8b

File tree

41 files changed

+274
-243
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+274
-243
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: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,21 @@ pub fn print_request(
7676
project_config: &ProjectConfig,
7777
top_level_statements: &mut TopLevelStatements,
7878
) -> String {
79+
let mut builder: AstBuilder = AstBuilder::default();
80+
let maybe_provided_variables = build_provided_variables(
81+
schema,
82+
&mut builder,
83+
operation,
84+
operation.name.map(|x| x.0),
85+
project_config,
86+
);
7987
Printer::without_dedupe(project_config).print_request(
8088
schema,
8189
operation,
8290
fragment,
8391
request_parameters,
8492
top_level_statements,
93+
maybe_provided_variables,
8594
)
8695
}
8796

@@ -94,21 +103,45 @@ pub fn print_request_params(
94103
) -> String {
95104
let mut request_parameters = build_request_params(operation);
96105
request_parameters.id = query_id;
97-
98-
let mut builder = AstBuilder::default();
106+
let mut builder: AstBuilder = AstBuilder::default();
107+
let maybe_provided_variables = build_provided_variables(
108+
schema,
109+
&mut builder,
110+
operation,
111+
operation.name.map(|x| x.0),
112+
project_config,
113+
);
99114
let request_parameters_ast_key = build_request_params_ast_key(
100115
schema,
101116
request_parameters,
102117
&mut builder,
103118
operation,
104-
top_level_statements,
105119
operation.name.map(|x| x.0),
106120
project_config,
121+
maybe_provided_variables,
107122
);
108123
let printer = JSONPrinter::new(&builder, project_config, top_level_statements);
109124
printer.print(request_parameters_ast_key, false)
110125
}
111126

127+
pub fn print_provided_variables(
128+
schema: &SDLSchema,
129+
operation: &OperationDefinition,
130+
project_config: &ProjectConfig,
131+
) -> Option<String> {
132+
let mut top_level_statements = Default::default();
133+
let mut builder = AstBuilder::default();
134+
let maybe_provided_variables = build_provided_variables(
135+
schema,
136+
&mut builder,
137+
operation,
138+
operation.name.map(|x| x.0),
139+
project_config,
140+
)?;
141+
let printer = JSONPrinter::new(&builder, project_config, &mut top_level_statements);
142+
Some(printer.print(maybe_provided_variables, false))
143+
}
144+
112145
pub struct Printer<'p> {
113146
project_config: &'p ProjectConfig,
114147
builder: AstBuilder,
@@ -132,21 +165,27 @@ impl<'p> Printer<'p> {
132165
}
133166
}
134167

135-
pub fn print_provided_variables(
168+
pub fn prepare_provided_variables_object(
136169
&mut self,
137170
schema: &SDLSchema,
138171
operation: &OperationDefinition,
139-
top_level_statements: &mut TopLevelStatements,
140-
) -> Option<String> {
141-
let key = build_provided_variables(
172+
) -> Option<AstKey> {
173+
build_provided_variables(
142174
schema,
143175
&mut self.builder,
144176
operation,
145177
operation.name.map(|x| x.0),
146178
self.project_config,
147-
)?;
179+
)
180+
}
181+
182+
pub fn print_provided_variables(
183+
&mut self,
184+
provided_variables: AstKey,
185+
top_level_statements: &mut TopLevelStatements,
186+
) -> Option<String> {
148187
let printer = JSONPrinter::new(&self.builder, self.project_config, top_level_statements);
149-
Some(printer.print(key, self.dedupe))
188+
Some(printer.print(provided_variables, self.dedupe))
150189
}
151190

152191
pub fn print_updatable_query(
@@ -183,15 +222,16 @@ impl<'p> Printer<'p> {
183222
fragment: &FragmentDefinition,
184223
request_parameters: RequestParameters<'_>,
185224
top_level_statements: &mut TopLevelStatements,
225+
maybe_provided_variables: Option<AstKey>,
186226
) -> String {
187227
let request_parameters = build_request_params_ast_key(
188228
schema,
189229
request_parameters,
190230
&mut self.builder,
191231
operation,
192-
top_level_statements,
193232
operation.name.map(|x| x.0),
194233
self.project_config,
234+
maybe_provided_variables,
195235
);
196236

197237
let key = build_request(
@@ -247,15 +287,16 @@ impl<'p> Printer<'p> {
247287
request_parameters: RequestParameters<'_>,
248288
operation: &OperationDefinition,
249289
top_level_statements: &mut TopLevelStatements,
290+
maybe_provided_variables: Option<AstKey>,
250291
) -> String {
251292
let key = build_request_params_ast_key(
252293
schema,
253294
request_parameters,
254295
&mut self.builder,
255296
operation,
256-
top_level_statements,
257297
operation.name.map(|x| x.0),
258298
self.project_config,
299+
maybe_provided_variables,
259300
);
260301
let printer = JSONPrinter::new(&self.builder, self.project_config, top_level_statements);
261302
printer.print(key, self.dedupe)

compiler/crates/relay-codegen/tests/connections/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
6666
&operation_fragment,
6767
request_parameters,
6868
&mut import_statements,
69+
None,
6970
);
7071
format!("{}{}", import_statements, request)
7172
})

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use graphql_text_printer::PrinterOptions;
1818
use intern::string_key::Intern;
1919
use relay_codegen::print_fragment;
2020
use relay_codegen::print_operation;
21+
use relay_codegen::print_provided_variables;
2122
use relay_config::ProjectConfig;
2223
use relay_schema::build_schema_with_extensions;
2324
use relay_transforms::apply_transforms;
@@ -254,6 +255,7 @@ pub fn parse_to_types_impl(
254255
&schema,
255256
&project_config,
256257
&fragment_locations,
258+
print_provided_variables(&schema, normalization_operation, &project_config),
257259
)
258260
}))
259261
.collect::<Vec<_>>()

0 commit comments

Comments
 (0)