Skip to content

Commit a5aecaf

Browse files
committed
Add DeferStreamInterface to rust compiler
1 parent d478890 commit a5aecaf

File tree

96 files changed

+530
-274
lines changed

Some content is hidden

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

96 files changed

+530
-274
lines changed

compiler/crates/graphql-ir/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ pub enum ValidationMessage {
409409
#[error("Invalid use of @stream on scalar field '{field_name}'")]
410410
InvalidStreamOnScalarField { field_name: StringKey },
411411

412-
#[error("Invalid use of @stream, the 'initial_count' argument is required.")]
412+
#[error("Invalid use of @stream, the 'initialCount' argument is required.")]
413413
StreamInitialCountRequired,
414414

415415
#[error("Variable `${variable_name}` is never used in operation `{operation_name}`")]

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

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ use relay_transforms::{
2020
extract_connection_metadata_from_directive, extract_handle_field_directives,
2121
extract_refetch_metadata_from_directive, extract_values_from_handle_field_directive,
2222
extract_variable_name, generate_abstract_type_refinement_key, remove_directive,
23-
ConnectionConstants, ConnectionMetadata, DeferDirective, RelayDirective, StreamDirective,
24-
ACTION_ARGUMENT, CLIENT_EXTENSION_DIRECTIVE_NAME, DEFER_STREAM_CONSTANTS,
25-
DIRECTIVE_SPLIT_OPERATION, INLINE_DATA_CONSTANTS, INTERNAL_METADATA_DIRECTIVE, MATCH_CONSTANTS,
26-
PATH_METADATA_ARGUMENT, REACT_FLIGHT_SCALAR_FLIGHT_FIELD_METADATA_KEY, REQUIRED_METADATA_KEY,
23+
ConnectionConstants, ConnectionMetadata, DeferDirective, DeferStreamInterface, RelayDirective,
24+
StreamDirective, ACTION_ARGUMENT, CLIENT_EXTENSION_DIRECTIVE_NAME, DIRECTIVE_SPLIT_OPERATION,
25+
INLINE_DATA_CONSTANTS, INTERNAL_METADATA_DIRECTIVE, MATCH_CONSTANTS, PATH_METADATA_ARGUMENT,
26+
REACT_FLIGHT_SCALAR_FLIGHT_FIELD_METADATA_KEY, REQUIRED_METADATA_KEY,
2727
TYPE_DISCRIMINATOR_DIRECTIVE_NAME,
2828
};
2929
use schema::Schema;
@@ -33,9 +33,14 @@ pub fn build_request_params_ast_key(
3333
request_parameters: RequestParameters,
3434
ast_builder: &mut AstBuilder,
3535
operation: &OperationDefinition,
36+
defer_stream_interface: &DeferStreamInterface,
3637
) -> AstKey {
37-
let mut operation_builder =
38-
CodegenBuilder::new(schema, CodegenVariant::Normalization, ast_builder);
38+
let mut operation_builder = CodegenBuilder::new(
39+
schema,
40+
CodegenVariant::Normalization,
41+
ast_builder,
42+
defer_stream_interface,
43+
);
3944
operation_builder.build_request_parameters(operation, request_parameters)
4045
}
4146

@@ -45,11 +50,21 @@ pub fn build_request(
4550
operation: &OperationDefinition,
4651
fragment: &FragmentDefinition,
4752
request_parameters: AstKey,
53+
defer_stream_interface: &DeferStreamInterface,
4854
) -> AstKey {
49-
let mut operation_builder =
50-
CodegenBuilder::new(schema, CodegenVariant::Normalization, ast_builder);
55+
let mut operation_builder = CodegenBuilder::new(
56+
schema,
57+
CodegenVariant::Normalization,
58+
ast_builder,
59+
defer_stream_interface,
60+
);
5161
let operation = Primitive::Key(operation_builder.build_operation(operation));
52-
let mut fragment_builder = CodegenBuilder::new(schema, CodegenVariant::Reader, ast_builder);
62+
let mut fragment_builder = CodegenBuilder::new(
63+
schema,
64+
CodegenVariant::Reader,
65+
ast_builder,
66+
defer_stream_interface,
67+
);
5368
let fragment = Primitive::Key(fragment_builder.build_fragment(fragment, true));
5469

5570
ast_builder.intern(Ast::Object(vec![
@@ -86,22 +101,35 @@ pub fn build_operation(
86101
schema: &Schema,
87102
ast_builder: &mut AstBuilder,
88103
operation: &OperationDefinition,
104+
defer_stream_interface: &DeferStreamInterface,
89105
) -> AstKey {
90-
let mut builder = CodegenBuilder::new(schema, CodegenVariant::Normalization, ast_builder);
106+
let mut builder = CodegenBuilder::new(
107+
schema,
108+
CodegenVariant::Normalization,
109+
ast_builder,
110+
defer_stream_interface,
111+
);
91112
builder.build_operation(operation)
92113
}
93114

94115
pub fn build_fragment(
95116
schema: &Schema,
96117
ast_builder: &mut AstBuilder,
97118
fragment: &FragmentDefinition,
119+
defer_stream_interface: &DeferStreamInterface,
98120
) -> AstKey {
99-
let mut builder = CodegenBuilder::new(schema, CodegenVariant::Reader, ast_builder);
121+
let mut builder = CodegenBuilder::new(
122+
schema,
123+
CodegenVariant::Reader,
124+
ast_builder,
125+
defer_stream_interface,
126+
);
100127
builder.build_fragment(fragment, false)
101128
}
102129

103-
struct CodegenBuilder<'schema, 'builder> {
130+
struct CodegenBuilder<'schema, 'builder, 'dsi> {
104131
connection_constants: ConnectionConstants,
132+
defer_stream_interface: &'dsi DeferStreamInterface,
105133
schema: &'schema Schema,
106134
variant: CodegenVariant,
107135
ast_builder: &'builder mut AstBuilder,
@@ -113,14 +141,16 @@ enum CodegenVariant {
113141
Normalization,
114142
}
115143

116-
impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> {
144+
impl<'schema, 'builder, 'dsi> CodegenBuilder<'schema, 'builder, 'dsi> {
117145
fn new(
118146
schema: &'schema Schema,
119147
variant: CodegenVariant,
120148
ast_builder: &'builder mut AstBuilder,
149+
defer_stream_interface: &'dsi DeferStreamInterface,
121150
) -> Self {
122151
Self {
123152
connection_constants: Default::default(),
153+
defer_stream_interface,
124154
schema,
125155
variant,
126156
ast_builder,
@@ -413,7 +443,7 @@ impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> {
413443
];
414444
if metadata.is_stream_connection {
415445
object.push(ObjectEntry {
416-
key: DEFER_STREAM_CONSTANTS.stream_name,
446+
key: self.defer_stream_interface.stream_name,
417447
value: Primitive::Bool(true),
418448
})
419449
}
@@ -460,7 +490,7 @@ impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> {
460490
Selection::InlineFragment(inline_frag) => {
461491
let defer = inline_frag
462492
.directives
463-
.named(DEFER_STREAM_CONSTANTS.defer_name);
493+
.named(self.defer_stream_interface.defer_name);
464494
if let Some(defer) = defer {
465495
vec![self.build_defer(&inline_frag, defer)]
466496
} else if let Some(inline_data_directive) = inline_frag
@@ -469,12 +499,8 @@ impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> {
469499
{
470500
// If inline fragment has @__inline directive (created by inline_data_fragment transform)
471501
// we will return selection wrapped with InlineDataFragmentSpread
472-
vec![
473-
self.build_inline_data_fragment_spread(
474-
&inline_frag,
475-
&inline_data_directive,
476-
),
477-
]
502+
vec![self
503+
.build_inline_data_fragment_spread(&inline_frag, &inline_data_directive)]
478504
} else if let Some(match_directive) = inline_frag
479505
.directives
480506
.named(MATCH_CONSTANTS.custom_module_directive_name)
@@ -485,7 +511,9 @@ impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> {
485511
}
486512
}
487513
Selection::LinkedField(field) => {
488-
let stream = field.directives.named(DEFER_STREAM_CONSTANTS.stream_name);
514+
let stream = field
515+
.directives
516+
.named(self.defer_stream_interface.stream_name);
489517

490518
match stream {
491519
Some(stream) => vec![self.build_stream(&field, stream)],
@@ -890,7 +918,8 @@ impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> {
890918
defer: &Directive,
891919
) -> Primitive {
892920
let next_selections = self.build_selections(inline_fragment.selections.iter());
893-
let DeferDirective { if_arg, label_arg } = DeferDirective::from(defer);
921+
let DeferDirective { if_arg, label_arg } =
922+
DeferDirective::from(defer, self.defer_stream_interface);
894923
let if_variable_name = extract_variable_name(if_arg);
895924
let label_name = label_arg.unwrap().value.item.expect_string_literal();
896925

@@ -918,7 +947,7 @@ impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> {
918947
let next_selections = vec![self.build_linked_field(&LinkedField {
919948
directives: remove_directive(
920949
&linked_field.directives,
921-
DEFER_STREAM_CONSTANTS.stream_name,
950+
self.defer_stream_interface.stream_name,
922951
),
923952
..linked_field.to_owned()
924953
})];
@@ -940,7 +969,7 @@ impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> {
940969
label_arg,
941970
use_customized_batch_arg,
942971
initial_count_arg: _,
943-
} = StreamDirective::from(stream);
972+
} = StreamDirective::from(stream, self.defer_stream_interface);
944973
let if_variable_name = extract_variable_name(if_arg);
945974
let use_customized_batch_variable_name =
946975
extract_variable_name(use_customized_batch_arg);

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

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,63 @@ use crate::indentation::print_indentation;
1515
use crate::utils::escape;
1616

1717
use graphql_ir::{FragmentDefinition, OperationDefinition};
18+
use relay_transforms::DeferStreamInterface;
1819
use schema::Schema;
1920

2021
use fnv::{FnvBuildHasher, FnvHashSet};
2122
use indexmap::IndexMap;
2223
use interner::StringKey;
2324
use std::fmt::{Result as FmtResult, Write};
2425

25-
pub fn print_operation(schema: &Schema, operation: &OperationDefinition) -> String {
26-
Printer::without_dedupe().print_operation(schema, operation)
26+
pub fn print_operation(
27+
schema: &Schema,
28+
operation: &OperationDefinition,
29+
defer_stream_interface: &DeferStreamInterface,
30+
) -> String {
31+
Printer::without_dedupe().print_operation(schema, operation, defer_stream_interface)
2732
}
2833

29-
pub fn print_fragment(schema: &Schema, fragment: &FragmentDefinition) -> String {
30-
Printer::without_dedupe().print_fragment(schema, fragment)
34+
pub fn print_fragment(
35+
schema: &Schema,
36+
fragment: &FragmentDefinition,
37+
defer_stream_interface: &DeferStreamInterface,
38+
) -> String {
39+
Printer::without_dedupe().print_fragment(schema, fragment, defer_stream_interface)
3140
}
3241

3342
pub fn print_request(
3443
schema: &Schema,
3544
operation: &OperationDefinition,
3645
fragment: &FragmentDefinition,
3746
request_parameters: RequestParameters,
47+
defer_stream_interface: &DeferStreamInterface,
3848
) -> String {
39-
Printer::without_dedupe().print_request(schema, operation, fragment, request_parameters)
49+
Printer::without_dedupe().print_request(
50+
schema,
51+
operation,
52+
fragment,
53+
request_parameters,
54+
defer_stream_interface,
55+
)
4056
}
4157

4258
pub fn print_request_params(
4359
schema: &Schema,
4460
operation: &OperationDefinition,
4561
query_id: Option<String>,
62+
defer_stream_interface: &DeferStreamInterface,
4663
) -> String {
4764
let mut request_parameters = build_request_params(operation);
4865
request_parameters.id = query_id;
4966

5067
let mut builder = AstBuilder::default();
51-
let request_parameters_ast_key =
52-
build_request_params_ast_key(schema, request_parameters, &mut builder, operation);
68+
let request_parameters_ast_key = build_request_params_ast_key(
69+
schema,
70+
request_parameters,
71+
&mut builder,
72+
operation,
73+
defer_stream_interface,
74+
);
5375
let printer = JSONPrinter::new(&builder);
5476
printer.print(request_parameters_ast_key, false)
5577
}
@@ -80,28 +102,45 @@ impl Printer {
80102
operation: &OperationDefinition,
81103
fragment: &FragmentDefinition,
82104
request_parameters: RequestParameters,
105+
defer_stream_interface: &DeferStreamInterface,
83106
) -> String {
84-
let request_parameters =
85-
build_request_params_ast_key(schema, request_parameters, &mut self.builder, operation);
107+
let request_parameters = build_request_params_ast_key(
108+
schema,
109+
request_parameters,
110+
&mut self.builder,
111+
operation,
112+
defer_stream_interface,
113+
);
86114
let key = build_request(
87115
schema,
88116
&mut self.builder,
89117
operation,
90118
fragment,
91119
request_parameters,
120+
defer_stream_interface,
92121
);
93122
let printer = JSONPrinter::new(&self.builder);
94123
printer.print(key, self.dedupe)
95124
}
96125

97-
pub fn print_operation(&mut self, schema: &Schema, operation: &OperationDefinition) -> String {
98-
let key = build_operation(schema, &mut self.builder, operation);
126+
pub fn print_operation(
127+
&mut self,
128+
schema: &Schema,
129+
operation: &OperationDefinition,
130+
defer_stream_interface: &DeferStreamInterface,
131+
) -> String {
132+
let key = build_operation(schema, &mut self.builder, operation, defer_stream_interface);
99133
let printer = JSONPrinter::new(&self.builder);
100134
printer.print(key, self.dedupe)
101135
}
102136

103-
pub fn print_fragment(&mut self, schema: &Schema, fragment: &FragmentDefinition) -> String {
104-
let key = build_fragment(schema, &mut self.builder, fragment);
137+
pub fn print_fragment(
138+
&mut self,
139+
schema: &Schema,
140+
fragment: &FragmentDefinition,
141+
defer_stream_interface: &DeferStreamInterface,
142+
) -> String {
143+
let key = build_fragment(schema, &mut self.builder, fragment, defer_stream_interface);
105144
let printer = JSONPrinter::new(&self.builder);
106145
printer.print(key, self.dedupe)
107146
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use graphql_ir::{build, Program};
1111
use graphql_syntax::parse_executable;
1212
use relay_codegen::{print_fragment, print_operation};
1313
use relay_test_schema::get_test_schema_with_extensions;
14-
use relay_transforms::{client_extensions, sort_selections};
14+
use relay_transforms::{client_extensions, sort_selections, DeferStreamInterface};
1515
use std::sync::Arc;
1616

1717
pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
@@ -22,13 +22,14 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
2222
let ir = build(&schema, &ast.definitions).unwrap();
2323
let program = Program::from_definitions(Arc::clone(&schema), ir);
2424
let next_program = sort_selections(&client_extensions(&program));
25+
let defer_stream_interface = DeferStreamInterface::default();
2526
let mut result = next_program
2627
.fragments()
27-
.map(|def| print_fragment(&schema, &def))
28+
.map(|def| print_fragment(&schema, &def, &defer_stream_interface))
2829
.chain(
2930
next_program
3031
.operations()
31-
.map(|def| print_operation(&schema, &def)),
32+
.map(|def| print_operation(&schema, &def, &defer_stream_interface)),
3233
)
3334
.collect::<Vec<_>>();
3435
result.sort_unstable();

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use graphql_syntax::parse_executable;
1212
use graphql_test_helpers::diagnostics_to_sorted_string;
1313
use relay_codegen::{build_request_params, Printer};
1414
use relay_test_schema::get_test_schema;
15-
use relay_transforms::{transform_connections, validate_connections, ConnectionInterface};
15+
use relay_transforms::{
16+
transform_connections, validate_connections, ConnectionInterface, DeferStreamInterface,
17+
};
1618
use std::sync::Arc;
1719

1820
pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
@@ -28,11 +30,13 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
2830
let program = Program::from_definitions(Arc::clone(&schema), ir);
2931

3032
let connection_interface = ConnectionInterface::default();
33+
let defer_stream_interface = DeferStreamInterface::default();
3134

3235
validate_connections(&program, &connection_interface)
3336
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?;
3437

35-
let next_program = transform_connections(&program, &connection_interface);
38+
let next_program =
39+
transform_connections(&program, &connection_interface, &defer_stream_interface);
3640

3741
let mut printed = next_program
3842
.operations()
@@ -46,11 +50,17 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
4650
type_condition: def.type_,
4751
};
4852
let request_parameters = build_request_params(&def);
49-
printer.print_request(&schema, def, &operation_fragment, request_parameters)
53+
printer.print_request(
54+
&schema,
55+
def,
56+
&operation_fragment,
57+
request_parameters,
58+
&defer_stream_interface,
59+
)
5060
})
5161
.collect::<Vec<_>>();
5262
for def in next_program.fragments() {
53-
printed.push(printer.print_fragment(&schema, def));
63+
printed.push(printer.print_fragment(&schema, def, &defer_stream_interface));
5464
}
5565
printed.sort();
5666
Ok(printed.join("\n\n"))

0 commit comments

Comments
 (0)