diff --git a/compiler/crates/relay-codegen/src/ast.rs b/compiler/crates/relay-codegen/src/ast.rs index 7f05b425144b2..507adc58dc4eb 100644 --- a/compiler/crates/relay-codegen/src/ast.rs +++ b/compiler/crates/relay-codegen/src/ast.rs @@ -111,8 +111,13 @@ impl AstBuilder { } } -pub struct RequestParameters { - pub id: Option, +pub enum QueryID { + Persisted { id: String, text_hash: String }, + External(StringKey), +} + +pub struct RequestParameters<'a> { + pub id: &'a Option, pub metadata: FnvHashMap, pub name: StringKey, pub operation_kind: OperationKind, diff --git a/compiler/crates/relay-codegen/src/build_ast.rs b/compiler/crates/relay-codegen/src/build_ast.rs index cddc8d23903f3..ccef40d47ee58 100644 --- a/compiler/crates/relay-codegen/src/build_ast.rs +++ b/compiler/crates/relay-codegen/src/build_ast.rs @@ -7,7 +7,7 @@ use std::path::Path; -use crate::ast::{Ast, AstBuilder, AstKey, ObjectEntry, Primitive, RequestParameters}; +use crate::ast::{Ast, AstBuilder, AstKey, ObjectEntry, Primitive, QueryID, RequestParameters}; use crate::constants::CODEGEN_CONSTANTS; use common::{NamedItem, WithLocation}; use graphql_ir::{ @@ -36,7 +36,7 @@ use schema::{SDLSchema, Schema}; pub fn build_request_params_ast_key( schema: &SDLSchema, - request_parameters: RequestParameters, + request_parameters: RequestParameters<'_>, ast_builder: &mut AstBuilder, operation: &OperationDefinition, ) -> AstKey { @@ -78,12 +78,12 @@ pub fn build_request( ])) } -pub fn build_request_params(operation: &OperationDefinition) -> RequestParameters { +pub fn build_request_params(operation: &OperationDefinition) -> RequestParameters<'_> { RequestParameters { name: operation.name.item, operation_kind: operation.kind, metadata: Default::default(), - id: None, + id: &None, text: None, } } @@ -1645,7 +1645,7 @@ impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> { fn build_request_parameters( &mut self, operation: &OperationDefinition, - mut request_parameters: RequestParameters, + mut request_parameters: RequestParameters<'_>, ) -> AstKey { let mut metadata_items: Vec = operation .directives @@ -1710,10 +1710,10 @@ impl<'schema, 'builder> CodegenBuilder<'schema, 'builder> { let id_prop = ObjectEntry { key: CODEGEN_CONSTANTS.id, - value: if let Some(id) = request_parameters.id { - Primitive::RawString(id) - } else { - Primitive::Null + value: match request_parameters.id { + Some(QueryID::Persisted { id, .. }) => Primitive::RawString(id.clone()), + Some(QueryID::External(name)) => Primitive::JSModuleDependency(*name), + None => Primitive::Null, }, }; diff --git a/compiler/crates/relay-codegen/src/lib.rs b/compiler/crates/relay-codegen/src/lib.rs index 82133e36dfa0a..af5592019201f 100644 --- a/compiler/crates/relay-codegen/src/lib.rs +++ b/compiler/crates/relay-codegen/src/lib.rs @@ -17,7 +17,7 @@ mod js_module_format; mod printer; mod utils; -pub use ast::{Primitive, RequestParameters}; +pub use ast::{Primitive, QueryID, RequestParameters}; pub use build_ast::build_request_params; pub use js_module_format::JsModuleFormat; pub use printer::{print_fragment, print_operation, print_request, print_request_params, Printer}; diff --git a/compiler/crates/relay-codegen/src/printer.rs b/compiler/crates/relay-codegen/src/printer.rs index bb48bb9d5fb4b..a89fcb70ee531 100644 --- a/compiler/crates/relay-codegen/src/printer.rs +++ b/compiler/crates/relay-codegen/src/printer.rs @@ -5,7 +5,7 @@ * LICENSE file in the root directory of this source tree. */ -use crate::ast::{Ast, AstBuilder, AstKey, ObjectEntry, Primitive, RequestParameters}; +use crate::ast::{Ast, AstBuilder, AstKey, ObjectEntry, Primitive, QueryID, RequestParameters}; use crate::build_ast::{ build_fragment, build_operation, build_request, build_request_params, build_request_params_ast_key, @@ -43,7 +43,7 @@ pub fn print_request( schema: &SDLSchema, operation: &OperationDefinition, fragment: &FragmentDefinition, - request_parameters: RequestParameters, + request_parameters: RequestParameters<'_>, js_module_format: JsModuleFormat, ) -> String { Printer::without_dedupe(js_module_format).print_request( @@ -57,7 +57,7 @@ pub fn print_request( pub fn print_request_params( schema: &SDLSchema, operation: &OperationDefinition, - query_id: Option, + query_id: &Option, js_module_format: JsModuleFormat, ) -> String { let mut request_parameters = build_request_params(operation); @@ -98,7 +98,7 @@ impl Printer { schema: &SDLSchema, operation: &OperationDefinition, fragment: &FragmentDefinition, - request_parameters: RequestParameters, + request_parameters: RequestParameters<'_>, ) -> String { let request_parameters = build_request_params_ast_key(schema, request_parameters, &mut self.builder, operation); diff --git a/compiler/crates/relay-compiler/src/artifact_map.rs b/compiler/crates/relay-compiler/src/artifact_map.rs index 6f2f136573a58..2224e3db9b773 100644 --- a/compiler/crates/relay-compiler/src/artifact_map.rs +++ b/compiler/crates/relay-compiler/src/artifact_map.rs @@ -5,9 +5,10 @@ * LICENSE file in the root directory of this source tree. */ -use crate::build_project::{Artifact, ArtifactContent, QueryID}; +use crate::build_project::{Artifact, ArtifactContent}; use fnv::{FnvBuildHasher, FnvHashMap}; use interner::StringKey; +use relay_codegen::QueryID; use serde::{Deserialize, Serialize}; use std::{collections::hash_map::Entry, path::PathBuf}; diff --git a/compiler/crates/relay-compiler/src/build_project/artifact_content.rs b/compiler/crates/relay-compiler/src/build_project/artifact_content.rs index e6f099e682511..3b0aeb83ec0dc 100644 --- a/compiler/crates/relay-compiler/src/build_project/artifact_content.rs +++ b/compiler/crates/relay-compiler/src/build_project/artifact_content.rs @@ -9,8 +9,7 @@ use super::is_operation_preloadable; use crate::config::{Config, ProjectConfig}; use common::{NamedItem, SourceLocationKey}; use graphql_ir::{Directive, FragmentDefinition, OperationDefinition}; -use interner::StringKey; -use relay_codegen::{build_request_params, Printer}; +use relay_codegen::{build_request_params, Printer, QueryID}; use relay_transforms::{ DATA_DRIVEN_DEPENDENCY_METADATA_KEY, INLINE_DATA_CONSTANTS, REACT_FLIGHT_LOCAL_COMPONENTS_METADATA_ARG_KEY, REACT_FLIGHT_LOCAL_COMPONENTS_METADATA_KEY, @@ -22,11 +21,6 @@ use signedsource::{sign_file, SIGNING_TOKEN}; use std::fmt::{Result, Write}; use std::sync::Arc; -pub enum QueryID { - Persisted { id: String, text_hash: String }, - External(StringKey), -} - pub enum ArtifactContent { Operation { normalization_operation: Arc, @@ -213,14 +207,20 @@ fn generate_operation( skip_types: bool, ) -> Vec { let mut request_parameters = build_request_params(&normalization_operation); - let operation_hash: Option = - if let Some(QueryID::Persisted { id, text_hash }) = id_and_text_hash { - request_parameters.id = Some(id.clone()); + let operation_hash: Option = match id_and_text_hash { + Some(QueryID::Persisted { text_hash, .. }) => { + request_parameters.id = id_and_text_hash; Some(text_hash.clone()) - } else { + } + Some(QueryID::External(_)) => { + request_parameters.id = id_and_text_hash; + None + } + None => { request_parameters.text = Some(text.into()); None - }; + } + }; let operation_fragment = FragmentDefinition { name: reader_operation.name, variable_definitions: reader_operation.variable_definitions.clone(), @@ -249,7 +249,7 @@ fn generate_operation( writeln!(content, "'use strict';\n").unwrap(); } - if let Some(id) = &request_parameters.id { + if let Some(QueryID::Persisted { id, .. }) = &request_parameters.id { writeln!(content, "// @relayRequestID {}", id).unwrap(); } if project_config.variable_names_comment { diff --git a/compiler/crates/relay-compiler/src/build_project/mod.rs b/compiler/crates/relay-compiler/src/build_project/mod.rs index 2823697c41241..8c2158154f36b 100644 --- a/compiler/crates/relay-compiler/src/build_project/mod.rs +++ b/compiler/crates/relay-compiler/src/build_project/mod.rs @@ -26,7 +26,6 @@ use crate::config::{Config, ProjectConfig}; use crate::errors::BuildProjectError; use crate::file_source::SourceControlUpdateStatus; use crate::{artifact_map::ArtifactMap, graphql_asts::GraphQLAsts}; -pub use artifact_content::QueryID; use build_ir::BuildIRResult; pub use build_ir::SourceHashes; pub use build_schema::build_schema; @@ -297,6 +296,7 @@ pub async fn commit_project( &config.root_dir, &persist_config, config, + project_config, operation_persister.as_ref(), &log_event, ) diff --git a/compiler/crates/relay-compiler/src/build_project/persist_operations.rs b/compiler/crates/relay-compiler/src/build_project/persist_operations.rs index 216183f8a3c0b..f5d6a99d5f565 100644 --- a/compiler/crates/relay-compiler/src/build_project/persist_operations.rs +++ b/compiler/crates/relay-compiler/src/build_project/persist_operations.rs @@ -6,8 +6,7 @@ */ use crate::{ - build_project::QueryID, - config::Config, + config::{Config, ProjectConfig}, config::{OperationPersister, PersistConfig}, errors::BuildProjectError, Artifact, ArtifactContent, @@ -18,6 +17,7 @@ use log::debug; use md5::{Digest, Md5}; use rayon::iter::IntoParallelRefMutIterator; use regex::Regex; +use relay_codegen::QueryID; use std::{fs, path::PathBuf}; lazy_static! { @@ -30,6 +30,7 @@ pub async fn persist_operations( root_dir: &PathBuf, persist_config: &PersistConfig, config: &Config, + project_config: &ProjectConfig, operation_persister: &'_ (dyn OperationPersister + Send + Sync), log_event: &impl PerfLogEvent, ) -> Result<(), BuildProjectError> { @@ -39,29 +40,47 @@ pub async fn persist_operations( if let ArtifactContent::Operation { ref text, ref mut id_and_text_hash, + ref normalization_operation, .. } = artifact.content { - let text_hash = md5(text); - let artifact_path = root_dir.join(&artifact.path); - let extracted_persist_id = if config.repersist_operations { + if project_config + .feature_flags + .as_ref() + .text_artifacts + .is_enabled_for(normalization_operation.name.item) + { + let name = if let Some(generate_virtual_id_file_name) = + &config.generate_virtual_id_file_name + { + generate_virtual_id_file_name(normalization_operation.name.item) + } else { + normalization_operation.name.item + }; + *id_and_text_hash = Some(QueryID::External(name)); None } else { - extract_persist_id(&artifact_path, &text_hash) - }; - if let Some(id) = extracted_persist_id { - *id_and_text_hash = Some(QueryID::Persisted { id, text_hash }); - None - } else { - let text = text.clone(); - Some(async move { - operation_persister - .persist_artifact(text, persist_config) - .await - .map(|id| { - *id_and_text_hash = Some(QueryID::Persisted { id, text_hash }); - }) - }) + let text_hash = md5(text); + let artifact_path = root_dir.join(&artifact.path); + let extracted_persist_id = if config.repersist_operations { + None + } else { + extract_persist_id(&artifact_path, &text_hash) + }; + if let Some(id) = extracted_persist_id { + *id_and_text_hash = Some(QueryID::Persisted { id, text_hash }); + None + } else { + let text = text.clone(); + Some(async move { + operation_persister + .persist_artifact(text, persist_config) + .await + .map(|id| { + *id_and_text_hash = Some(QueryID::Persisted { id, text_hash }); + }) + }) + } } } else { None diff --git a/compiler/crates/relay-compiler/src/config.rs b/compiler/crates/relay-compiler/src/config.rs index 0633886765050..5250c6dd5eefe 100644 --- a/compiler/crates/relay-compiler/src/config.rs +++ b/compiler/crates/relay-compiler/src/config.rs @@ -65,6 +65,8 @@ pub struct Config { pub generate_extra_artifacts: Option, pub generate_fragment_text_artifact: Option, pub generate_operation_text_artifact: Option, + pub generate_virtual_id_file_name: Option StringKey + Send + Sync>>, + /// Path to which to write the output of the compilation pub artifact_writer: Box, @@ -187,6 +189,7 @@ impl From for Config { generate_extra_artifacts: None, generate_fragment_text_artifact: None, generate_operation_text_artifact: None, + generate_virtual_id_file_name: None, saved_state_config: None, saved_state_loader: None, saved_state_version: "MISSING".to_string(), @@ -335,6 +338,7 @@ impl Config { generate_extra_artifacts: None, generate_fragment_text_artifact: None, generate_operation_text_artifact: None, + generate_virtual_id_file_name: None, saved_state_config: config_file.saved_state_config, saved_state_loader: None, saved_state_version: hex::encode(hash.result()), diff --git a/compiler/crates/relay-compiler/src/lib.rs b/compiler/crates/relay-compiler/src/lib.rs index 4b2ada5dc9e30..1a6937bdf3cd1 100644 --- a/compiler/crates/relay-compiler/src/lib.rs +++ b/compiler/crates/relay-compiler/src/lib.rs @@ -31,7 +31,7 @@ pub use build_project::{ build_programs, build_raw_program, build_schema, create_path_for_artifact, generate_artifacts, generate_extra_artifacts::GenerateExtraArtifactsFn, is_operation_preloadable, transform_program, validate, validate_program, Artifact, - ArtifactContent, BuildProjectFailure, QueryID, SourceHashes, + ArtifactContent, BuildProjectFailure, SourceHashes, }; pub use config::{OperationPersister, PersistConfig}; pub use file_source::{