Skip to content

Commit 1386365

Browse files
kassensfacebook-github-bot
authored andcommitted
move ConnectionInterface into config file
Summary: This removes the FB specifics from the OSS part of the compiler and makes it easy to configure connection names to a different format. Reviewed By: tyao1 Differential Revision: D22819909 fbshipit-source-id: a6c54a5196d9ce060e4abe0e4c077eb69798e91f
1 parent c552bae commit 1386365

File tree

20 files changed

+100
-107
lines changed

20 files changed

+100
-107
lines changed

compiler/crates/graphql-transforms/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ indexmap = { version = "1.3", features = ["serde-1", "rayon"] }
110110
lazy_static = "1.0"
111111
parking_lot = "0.10.2"
112112
rayon = "1.2"
113+
serde = { version = "1.0", features = ["derive", "rc"] }
113114

114115
[dev-dependencies]
115116
fixture-tests = { path = "../fixture-tests" }

compiler/crates/graphql-transforms/src/connections/connection_interface.rs

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,33 @@
77

88
use interner::{Intern, StringKey};
99

10-
use lazy_static::lazy_static;
11-
use std::sync::Arc;
10+
use serde::Deserialize;
1211

13-
#[derive(Debug)]
12+
/// Configuration where Relay should expect some fields in the schema.
13+
#[derive(Debug, Deserialize)]
14+
#[serde(deny_unknown_fields, rename_all = "camelCase")]
1415
pub struct ConnectionInterface {
15-
pub cursor_selection_name: StringKey,
16-
pub edges_selection_name: StringKey,
17-
pub end_cursor_selection_name: StringKey,
18-
pub has_next_page_selection_name: StringKey,
19-
pub has_prev_page_selection_name: StringKey,
20-
pub node_selection_name: StringKey,
21-
pub page_info_selection_name: StringKey,
22-
pub start_cursor_selection_name: StringKey,
16+
pub cursor: StringKey,
17+
pub edges: StringKey,
18+
pub end_cursor: StringKey,
19+
pub has_next_page: StringKey,
20+
pub has_previous_page: StringKey,
21+
pub node: StringKey,
22+
pub page_info: StringKey,
23+
pub start_cursor: StringKey,
2324
}
2425

25-
lazy_static! {
26-
pub static ref OSS_CONNECTION_INTERFACE: Arc<ConnectionInterface> =
27-
Arc::new(ConnectionInterface {
28-
cursor_selection_name: "cursor".intern(),
29-
edges_selection_name: "edges".intern(),
30-
end_cursor_selection_name: "endCursor".intern(),
31-
has_next_page_selection_name: "hasNextPage".intern(),
32-
has_prev_page_selection_name: "hasPreviousPage".intern(),
33-
node_selection_name: "node".intern(),
34-
page_info_selection_name: "pageInfo".intern(),
35-
start_cursor_selection_name: "startCursor".intern(),
36-
});
37-
pub static ref FB_CONNECTION_INTERFACE: Arc<ConnectionInterface> =
38-
Arc::new(ConnectionInterface {
39-
cursor_selection_name: "cursor".intern(),
40-
edges_selection_name: "edges".intern(),
41-
end_cursor_selection_name: "end_cursor".intern(),
42-
has_next_page_selection_name: "has_next_page".intern(),
43-
has_prev_page_selection_name: "has_previous_page".intern(),
44-
node_selection_name: "node".intern(),
45-
page_info_selection_name: "page_info".intern(),
46-
start_cursor_selection_name: "start_cursor".intern(),
47-
});
26+
impl Default for ConnectionInterface {
27+
fn default() -> Self {
28+
ConnectionInterface {
29+
cursor: "cursor".intern(),
30+
edges: "edges".intern(),
31+
end_cursor: "endCursor".intern(),
32+
has_next_page: "hasNextPage".intern(),
33+
has_previous_page: "hasPreviousPage".intern(),
34+
node: "node".intern(),
35+
page_info: "pageInfo".intern(),
36+
start_cursor: "startCursor".intern(),
37+
}
38+
}
4839
}

compiler/crates/graphql-transforms/src/connections/connection_util.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ pub fn assert_connection_selections<'s>(
2727
for (ix, selection) in selections.iter().enumerate() {
2828
if let Selection::LinkedField(field) = selection {
2929
let field_name = schema.field(field.definition.item).name;
30-
if field_name == connection_interface.edges_selection_name {
30+
if field_name == connection_interface.edges {
3131
if edges_selection.is_some() {
3232
unreachable!("Unexpected duplicate selection for edges")
3333
}
3434
edges_selection = Some((ix, field.as_ref()));
3535
}
36-
if field_name == connection_interface.page_info_selection_name {
36+
if field_name == connection_interface.page_info {
3737
if page_info_selection.is_some() {
3838
unreachable!("Unexpected duplicate selection for page_info")
3939
}
@@ -268,10 +268,10 @@ pub fn build_edge_selections(
268268
connection_interface: &ConnectionInterface,
269269
) -> Selection {
270270
let cursor_field_id = schema
271-
.named_field(edge_type, connection_interface.cursor_selection_name)
271+
.named_field(edge_type, connection_interface.cursor)
272272
.expect("Expected presence of cursor field to have been previously validated.");
273273
let node_field_id = schema
274-
.named_field(edge_type, connection_interface.node_selection_name)
274+
.named_field(edge_type, connection_interface.node)
275275
.expect("Expected presence of node field to have been previously validated.");
276276
let typename_field_id = schema.typename_field();
277277

@@ -314,28 +314,16 @@ pub fn build_page_info_selections(
314314
connection_interface: &ConnectionInterface,
315315
) -> Selection {
316316
let end_cursor_field_id = schema
317-
.named_field(
318-
page_info_type,
319-
connection_interface.end_cursor_selection_name,
320-
)
317+
.named_field(page_info_type, connection_interface.end_cursor)
321318
.expect("Expected presence of end_cursor field to have been previously validated.");
322319
let has_next_page_field_id = schema
323-
.named_field(
324-
page_info_type,
325-
connection_interface.has_next_page_selection_name,
326-
)
320+
.named_field(page_info_type, connection_interface.has_next_page)
327321
.expect("Expected presence of has_next_page field to have been previously validated.");
328322
let has_prev_page_field_id = schema
329-
.named_field(
330-
page_info_type,
331-
connection_interface.has_prev_page_selection_name,
332-
)
323+
.named_field(page_info_type, connection_interface.has_previous_page)
333324
.expect("Expected presence of has_previous_page field to have been previously validated.");
334325
let start_cursor_field_id = schema
335-
.named_field(
336-
page_info_type,
337-
connection_interface.start_cursor_selection_name,
338-
)
326+
.named_field(page_info_type, connection_interface.start_cursor)
339327
.expect("Expected presence of start_cursor field to have been previously validated.");
340328

341329
if connection_metadata.direction == connection_constants.direction_forward {

compiler/crates/graphql-transforms/src/connections/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ mod connection_interface;
1010
mod connection_util;
1111

1212
pub use connection_constants::ConnectionConstants;
13-
pub use connection_interface::{
14-
ConnectionInterface, FB_CONNECTION_INTERFACE, OSS_CONNECTION_INTERFACE,
15-
};
13+
pub use connection_interface::ConnectionInterface;
1614
pub use connection_util::{
1715
assert_connection_selections, build_connection_metadata,
1816
build_connection_metadata_as_directive, build_edge_selections, build_page_info_selections,

compiler/crates/graphql-transforms/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ pub use connections::{
6464
extract_connection_metadata_from_directive, ConnectionConstants, ConnectionInterface,
6565
ConnectionMetadata,
6666
};
67-
pub use connections::{FB_CONNECTION_INTERFACE, OSS_CONNECTION_INTERFACE};
6867
pub use dedupe_type_discriminator::dedupe_type_discriminator;
6968
pub use defer_stream::{
7069
transform_defer_stream, DeferDirective, StreamDirective, DEFER_STREAM_CONSTANTS,

compiler/crates/graphql-transforms/src/transform_connections.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use std::sync::Arc;
2323

2424
pub fn transform_connections(
2525
program: &Program,
26-
connection_interface: Arc<ConnectionInterface>,
26+
connection_interface: &ConnectionInterface,
2727
) -> Program {
28-
let mut transform = ConnectionTransform::new(program, &connection_interface);
28+
let mut transform = ConnectionTransform::new(program, connection_interface);
2929
transform
3030
.transform_program(program)
3131
.replace_or_else(|| program.clone())
@@ -49,7 +49,7 @@ impl<'s> ConnectionTransform<'s> {
4949
connection_constants: ConnectionConstants::default(),
5050
connection_interface,
5151
current_path: None,
52-
current_document_name: connection_interface.cursor_selection_name, // Set an arbitrary value to avoid Option
52+
current_document_name: connection_interface.cursor, // Set an arbitrary value to avoid Option
5353
current_connection_metadata: Vec::new(),
5454
handle_field_constants,
5555
handle_field_constants_for_connection: HandleFieldConstants {
@@ -82,10 +82,7 @@ impl<'s> ConnectionTransform<'s> {
8282

8383
// Construct edges selection
8484
let edges_schema_field_id = schema
85-
.named_field(
86-
connection_field_type,
87-
self.connection_interface.edges_selection_name,
88-
)
85+
.named_field(connection_field_type, self.connection_interface.edges)
8986
.expect("Expected presence of edges field to have been previously validated.");
9087
let edges_schema_field = schema.field(edges_schema_field_id);
9188
let edges_field_name = edges_schema_field.name;
@@ -149,10 +146,7 @@ impl<'s> ConnectionTransform<'s> {
149146

150147
// Construct page_info selection
151148
let page_info_schema_field_id = schema
152-
.named_field(
153-
connection_field_type,
154-
self.connection_interface.page_info_selection_name,
155-
)
149+
.named_field(connection_field_type, self.connection_interface.page_info)
156150
.expect("Expected presence of page_info field to have been previously validated.");
157151
let page_info_schema_field = schema.field(page_info_schema_field_id);
158152
let page_info_field_name = page_info_schema_field.name;
@@ -222,7 +216,7 @@ impl<'s> ConnectionTransform<'s> {
222216
"{}$defer${}${}",
223217
self.current_document_name,
224218
key.lookup(),
225-
self.connection_interface.page_info_selection_name
219+
self.connection_interface.page_info
226220
)
227221
.intern(),
228222
)),

compiler/crates/graphql-transforms/src/validations/validate_connections.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl<'s> ConnectionValidation<'s> {
9797
)]);
9898
}
9999

100-
let edges_selection_name = self.connection_interface.edges_selection_name;
100+
let edges_selection_name = self.connection_interface.edges;
101101
let edges_selection: Option<&Selection> =
102102
connection_field.selections.iter().find(|sel| match sel {
103103
Selection::LinkedField(field) => {
@@ -170,7 +170,7 @@ impl<'s> ConnectionValidation<'s> {
170170
let connection_directive_name = connection_directive.name.item;
171171
let connection_type_name = schema.get_type_name(connection_field_type);
172172
let connection_field_name = connection_schema_field.name;
173-
let edges_selection_name = self.connection_interface.edges_selection_name;
173+
let edges_selection_name = self.connection_interface.edges;
174174

175175
// Validate edges selection
176176
let (_, edges_type) = self.validate_selection(
@@ -198,8 +198,8 @@ impl<'s> ConnectionValidation<'s> {
198198
)?;
199199

200200
let edge_type = edges_type.inner();
201-
let node_selection_name = self.connection_interface.node_selection_name;
202-
let cursor_selection_name = self.connection_interface.cursor_selection_name;
201+
let node_selection_name = self.connection_interface.node;
202+
let cursor_selection_name = self.connection_interface.cursor;
203203
validate!(
204204
// Validate edges.node selection
205205
self.validate_selection(
@@ -262,7 +262,7 @@ impl<'s> ConnectionValidation<'s> {
262262
let connection_directive_name = connection_directive.name.item;
263263
let connection_type_name = schema.get_type_name(connection_field_type);
264264
let connection_field_name = connection_schema_field.name;
265-
let page_info_selection_name = self.connection_interface.page_info_selection_name;
265+
let page_info_selection_name = self.connection_interface.page_info;
266266

267267
// Validate page_info selection
268268
let (_, page_info_type) = self.validate_selection(
@@ -286,10 +286,10 @@ impl<'s> ConnectionValidation<'s> {
286286

287287
let page_info_type = page_info_type.inner();
288288
let page_info_sub_fields = vec![
289-
self.connection_interface.end_cursor_selection_name,
290-
self.connection_interface.has_next_page_selection_name,
291-
self.connection_interface.has_prev_page_selection_name,
292-
self.connection_interface.start_cursor_selection_name,
289+
self.connection_interface.end_cursor,
290+
self.connection_interface.has_next_page,
291+
self.connection_interface.has_previous_page,
292+
self.connection_interface.start_cursor,
293293
];
294294

295295
validate_map(page_info_sub_fields.iter(), |page_info_sub_field_name| {
@@ -526,7 +526,7 @@ impl<'s> ConnectionValidation<'s> {
526526
if edges_field.alias.is_some() {
527527
return Err(vec![ValidationError::new(
528528
ValidationMessage::UnsupportedAliasingInStreamConnection {
529-
field_name: self.connection_interface.edges_selection_name,
529+
field_name: self.connection_interface.edges,
530530
},
531531
vec![edges_field.definition.location],
532532
)]);
@@ -538,7 +538,7 @@ impl<'s> ConnectionValidation<'s> {
538538
.find_map(|sel| match sel {
539539
Selection::LinkedField(field) => {
540540
if self.program.schema.field(field.definition.item).name
541-
== self.connection_interface.page_info_selection_name
541+
== self.connection_interface.page_info
542542
{
543543
Some(field)
544544
} else {
@@ -551,7 +551,7 @@ impl<'s> ConnectionValidation<'s> {
551551
if page_info_selection.alias.is_some() {
552552
return Err(vec![ValidationError::new(
553553
ValidationMessage::UnsupportedAliasingInStreamConnection {
554-
field_name: self.connection_interface.page_info_selection_name,
554+
field_name: self.connection_interface.page_info,
555555
},
556556
vec![page_info_selection.definition.location],
557557
)]);

compiler/crates/graphql-transforms/tests/refetchable_fragment/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use graphql_ir::{build, Program};
1212
use graphql_syntax::parse;
1313
use graphql_text_printer::{print_fragment, print_operation};
1414
use graphql_transforms::{
15-
transform_connections, transform_refetchable_fragment, OSS_CONNECTION_INTERFACE,
15+
transform_connections, transform_refetchable_fragment, ConnectionInterface,
1616
};
1717
use std::sync::Arc;
1818
use test_schema::get_test_schema;
@@ -30,7 +30,7 @@ pub fn transform_fixture(fixture: &Fixture) -> Result<String, String> {
3030
Err(err) => return Err(format!("{:?}", err)),
3131
};
3232
let program = Program::from_definitions(Arc::clone(&schema), ir);
33-
let program = transform_connections(&program, Arc::clone(&OSS_CONNECTION_INTERFACE));
33+
let program = transform_connections(&program, &ConnectionInterface::default());
3434
let base_fragments = Default::default();
3535
let next_program =
3636
transform_refetchable_fragment(&program, &base_fragments, false).map_err(|errors| {

compiler/crates/graphql-transforms/tests/transform_connections/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use fnv::FnvHashMap;
1111
use graphql_ir::{build, Program};
1212
use graphql_syntax::parse;
1313
use graphql_text_printer::{print_fragment, print_operation};
14-
use graphql_transforms::{transform_connections, validate_connections, OSS_CONNECTION_INTERFACE};
14+
use graphql_transforms::{transform_connections, validate_connections, ConnectionInterface};
1515
use std::sync::Arc;
1616
use test_schema::get_test_schema;
1717

@@ -39,7 +39,9 @@ pub fn transform_fixture(fixture: &Fixture) -> Result<String, String> {
3939

4040
let program = Program::from_definitions(Arc::clone(&schema), ir);
4141

42-
let validation_result = validate_connections(&program, &*OSS_CONNECTION_INTERFACE);
42+
let connection_interface = ConnectionInterface::default();
43+
44+
let validation_result = validate_connections(&program, &connection_interface);
4345
match validation_result {
4446
Ok(_) => {}
4547
Err(errors) => {
@@ -52,7 +54,7 @@ pub fn transform_fixture(fixture: &Fixture) -> Result<String, String> {
5254
}
5355
}
5456

55-
let next_program = transform_connections(&program, Arc::clone(&OSS_CONNECTION_INTERFACE));
57+
let next_program = transform_connections(&program, &connection_interface);
5658

5759
let mut printed = next_program
5860
.operations()

compiler/crates/graphql-transforms/tests/validate_connections/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use fixture_tests::Fixture;
1010
use fnv::FnvHashMap;
1111
use graphql_ir::{build, Program};
1212
use graphql_syntax::parse;
13-
use graphql_transforms::{validate_connections, OSS_CONNECTION_INTERFACE};
13+
use graphql_transforms::{validate_connections, ConnectionInterface};
1414
use std::sync::Arc;
1515
use test_schema::TEST_SCHEMA;
1616

@@ -35,7 +35,7 @@ pub fn transform_fixture(fixture: &Fixture) -> Result<String, String> {
3535
};
3636

3737
let program = Program::from_definitions(Arc::clone(&TEST_SCHEMA), ir);
38-
let validation_result = validate_connections(&program, &*OSS_CONNECTION_INTERFACE);
38+
let validation_result = validate_connections(&program, &ConnectionInterface::default());
3939

4040
match validation_result {
4141
Ok(_) => Ok("OK".to_owned()),

0 commit comments

Comments
 (0)