Skip to content

Commit d3c8d1c

Browse files
tobias-tenglerfacebook-github-bot
authored andcommitted
Add feature flag for fragment arguments (#4648)
Summary: Adds a new `enable_fragment_argument_transform` feature flag that allows to enable the existing parsing and transform of fragment variable definitions and fragment spread arguments. Pull Request resolved: #4648 Reviewed By: josephsavona Differential Revision: D54872375 Pulled By: captbaritone fbshipit-source-id: 405182b131c984a2a64dbc3ca40a325624714833
1 parent 05d64cc commit d3c8d1c

File tree

16 files changed

+368
-33
lines changed

16 files changed

+368
-33
lines changed

compiler/crates/common/src/feature_flags.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ pub struct FeatureFlags {
9999
/// excludes resolver metadata from reader ast
100100
#[serde(default)]
101101
pub disable_resolver_reader_ast: bool,
102+
103+
/// Add support for parsing and transforming variable definitions on fragment
104+
/// definitions and arguments on fragment spreads.
105+
#[serde(default)]
106+
pub enable_fragment_argument_transform: bool,
102107
}
103108

104109
#[derive(Debug, Deserialize, Clone, Serialize, Default)]

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ pub fn parse_executable(
5959
parse_executable_with_error_recovery(source, source_location).into()
6060
}
6161

62+
/// Parses a GraphQL document that's restricted to executable
63+
/// definitions with custom feature flags passed as `features`.
64+
pub fn parse_executable_with_features(
65+
source: &str,
66+
source_location: SourceLocationKey,
67+
features: ParserFeatures,
68+
) -> DiagnosticsResult<ExecutableDocument> {
69+
parse_executable_with_error_recovery_and_parser_features(source, source_location, features)
70+
.into()
71+
}
72+
6273
/// Parses a GraphQL document that's restricted to executable definitions,
6374
/// with error recovery.
6475
pub fn parse_executable_with_error_recovery(
@@ -81,17 +92,6 @@ pub fn parse_executable_with_error_recovery_and_parser_features(
8192
parser.parse_executable_document()
8293
}
8394

84-
/// Parses a GraphQL document that's restricted to executable
85-
/// definitions with custom feature flags passed as `features`.
86-
pub fn parse_executable_with_features(
87-
source: &str,
88-
source_location: SourceLocationKey,
89-
features: ParserFeatures,
90-
) -> DiagnosticsResult<ExecutableDocument> {
91-
parse_executable_with_error_recovery_and_parser_features(source, source_location, features)
92-
.into()
93-
}
94-
9595
/// Parses a GraphQL document that's restricted to type system definitions
9696
/// including schema definition, type definitions and type system extensions.
9797
pub fn parse_schema_document(

compiler/crates/graphql-syntax/src/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ use crate::syntax_error::SyntaxError;
2222

2323
type ParseResult<T> = Result<T, ()>;
2424

25-
#[derive(Default, PartialEq)]
25+
#[derive(Default, Clone, Copy, PartialEq)]
2626
pub enum FragmentArgumentSyntaxKind {
2727
#[default]
2828
None,
2929
OnlyFragmentVariableDefinitions,
3030
SpreadArgumentsAndFragmentVariableDefinitions,
3131
}
3232

33-
#[derive(Default)]
33+
#[derive(Default, Clone, Copy)]
3434
pub struct ParserFeatures {
3535
/// Whether and how to enable the experimental fragment variables definitions syntax
3636
pub fragment_argument_capability: FragmentArgumentSyntaxKind,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@ async fn build_projects<TPerfLogger: PerfLogger + 'static>(
303303
GraphQLAsts::from_graphql_sources_map(
304304
&compiler_state.graphql_sources,
305305
&dirty_artifact_sources,
306+
&config,
306307
)
307308
})?;
308309

compiler/crates/relay-compiler/src/graphql_asts.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::collections::hash_map::Entry;
99
use std::collections::HashSet;
1010
use std::path::Path;
1111
use std::path::PathBuf;
12+
use std::sync::Arc;
1213

1314
use common::Diagnostic;
1415
use common::SourceLocationKey;
@@ -18,13 +19,16 @@ use graphql_ir::ExecutableDefinitionName;
1819
use graphql_ir::FragmentDefinitionName;
1920
use graphql_ir::OperationDefinitionName;
2021
use graphql_syntax::ExecutableDefinition;
22+
use relay_config::ProjectConfig;
2123
use relay_config::ProjectName;
2224

2325
use crate::artifact_map::ArtifactSourceKey;
2426
use crate::compiler_state::GraphQLSources;
27+
use crate::config::Config;
2528
use crate::errors::Error;
2629
use crate::errors::Result;
2730
use crate::file_source::LocatedGraphQLSource;
31+
use crate::utils::get_parser_features;
2832

2933
#[derive(Debug)]
3034
pub struct GraphQLAsts {
@@ -50,10 +54,13 @@ impl GraphQLAsts {
5054
pub fn from_graphql_sources_map(
5155
graphql_sources_map: &FnvHashMap<ProjectName, GraphQLSources>,
5256
dirty_artifact_sources: &FnvHashMap<ProjectName, Vec<ArtifactSourceKey>>,
57+
config: &Arc<Config>,
5358
) -> Result<FnvHashMap<ProjectName, GraphQLAsts>> {
5459
graphql_sources_map
5560
.iter()
5661
.map(|(&project_name, sources)| {
62+
let project_config = &config.projects[&project_name];
63+
5764
let asts = GraphQLAsts::from_graphql_sources(
5865
sources,
5966
dirty_artifact_sources
@@ -73,6 +80,7 @@ impl GraphQLAsts {
7380
})
7481
.collect()
7582
}),
83+
project_config,
7684
)?;
7785
Ok((project_name, asts))
7886
})
@@ -85,7 +93,10 @@ impl GraphQLAsts {
8593
pub fn from_graphql_sources(
8694
graphql_sources: &GraphQLSources,
8795
dirty_definitions: Option<Vec<&ExecutableDefinitionName>>,
96+
project_config: &ProjectConfig,
8897
) -> Result<Self> {
98+
let parser_features = get_parser_features(project_config);
99+
89100
let mut syntax_errors = Vec::new();
90101

91102
let mut asts: FnvHashMap<PathBuf, Vec<ExecutableDefinition>> = Default::default();
@@ -108,9 +119,10 @@ impl GraphQLAsts {
108119
{
109120
let source_location =
110121
SourceLocationKey::embedded(&file_name.to_string_lossy(), *index);
111-
match graphql_syntax::parse_executable(
122+
match graphql_syntax::parse_executable_with_features(
112123
&graphql_source.text_source().text,
113124
source_location,
125+
parser_features,
114126
) {
115127
Ok(document) => {
116128
for def in &document.definitions {
@@ -145,9 +157,10 @@ impl GraphQLAsts {
145157
// TODO: parse name instead of the whole graphql text
146158
let source_location =
147159
SourceLocationKey::embedded(&file_name.to_string_lossy(), *index);
148-
if let Ok(document) = graphql_syntax::parse_executable(
160+
if let Ok(document) = graphql_syntax::parse_executable_with_features(
149161
&graphql_source.text_source().text,
150162
source_location,
163+
parser_features,
151164
) {
152165
for def in document.definitions {
153166
match def {
@@ -210,9 +223,10 @@ impl GraphQLAsts {
210223
{
211224
let source_location =
212225
SourceLocationKey::embedded(&file_name.to_string_lossy(), *index);
213-
match graphql_syntax::parse_executable(
226+
match graphql_syntax::parse_executable_with_features(
214227
&graphql_source.text_source().text,
215228
source_location,
229+
parser_features,
216230
) {
217231
Ok(document) => {
218232
definitions_for_file.extend(document.definitions);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ mod operation_persister;
2323
mod red_to_green;
2424
pub mod saved_state;
2525
pub mod status_reporter;
26+
mod utils;
2627

2728
pub use artifact_map::ArtifactSourceKey;
2829
pub use build_project::add_to_mercurial;
@@ -72,3 +73,4 @@ pub use graphql_asts::GraphQLAsts;
7273
pub use operation_persister::LocalPersister;
7374
pub use operation_persister::RemotePersister;
7475
pub use relay_config::ProjectName;
76+
pub use utils::get_parser_features;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
use graphql_syntax::FragmentArgumentSyntaxKind;
9+
use graphql_syntax::ParserFeatures;
10+
use relay_config::ProjectConfig;
11+
12+
pub fn get_parser_features(project_config: &ProjectConfig) -> ParserFeatures {
13+
ParserFeatures {
14+
fragment_argument_capability: if project_config
15+
.feature_flags
16+
.enable_fragment_argument_transform
17+
{
18+
FragmentArgumentSyntaxKind::SpreadArgumentsAndFragmentVariableDefinitions
19+
} else {
20+
FragmentArgumentSyntaxKind::None
21+
},
22+
}
23+
}

compiler/crates/relay-compiler/tests/compile_relay_artifacts/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String>
124124
allow_required_in_mutation_response: FeatureFlag::Disabled,
125125
allow_resolvers_in_mutation_response: FeatureFlag::Disabled,
126126
disable_resolver_reader_ast: false,
127+
enable_fragment_argument_transform: false,
127128
};
128129

129130
let default_project_config = ProjectConfig {

compiler/crates/relay-compiler/tests/compile_relay_artifacts_with_custom_id/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub async fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String>
103103
allow_required_in_mutation_response: FeatureFlag::Disabled,
104104
allow_resolvers_in_mutation_response: FeatureFlag::Disabled,
105105
disable_resolver_reader_ast: false,
106+
enable_fragment_argument_transform: false,
106107
};
107108

108109
let default_schema_config = SchemaConfig::default();

0 commit comments

Comments
 (0)