Skip to content

Commit

Permalink
Use require('virtual id file') to replace persisted id in artifacts
Browse files Browse the repository at this point in the history
Reviewed By: alunyov

Differential Revision: D30001696

fbshipit-source-id: c6c92146667fe33605fdb87c01b023dedf6c38d0
  • Loading branch information
tyao1 authored and facebook-github-bot committed Sep 2, 2021
1 parent 5326517 commit eead274
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 52 deletions.
9 changes: 7 additions & 2 deletions compiler/crates/relay-codegen/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ impl AstBuilder {
}
}

pub struct RequestParameters {
pub id: Option<String>,
pub enum QueryID {
Persisted { id: String, text_hash: String },
External(StringKey),
}

pub struct RequestParameters<'a> {
pub id: &'a Option<QueryID>,
pub metadata: FnvHashMap<String, String>,
pub name: StringKey,
pub operation_kind: OperationKind,
Expand Down
18 changes: 9 additions & 9 deletions compiler/crates/relay-codegen/src/build_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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<ObjectEntry> = operation
.directives
Expand Down Expand Up @@ -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,
},
};

Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
8 changes: 4 additions & 4 deletions compiler/crates/relay-codegen/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -57,7 +57,7 @@ pub fn print_request(
pub fn print_request_params(
schema: &SDLSchema,
operation: &OperationDefinition,
query_id: Option<String>,
query_id: &Option<QueryID>,
js_module_format: JsModuleFormat,
) -> String {
let mut request_parameters = build_request_params(operation);
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/relay-compiler/src/artifact_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<OperationDefinition>,
Expand Down Expand Up @@ -213,14 +207,20 @@ fn generate_operation(
skip_types: bool,
) -> Vec<u8> {
let mut request_parameters = build_request_params(&normalization_operation);
let operation_hash: Option<String> =
if let Some(QueryID::Persisted { id, text_hash }) = id_and_text_hash {
request_parameters.id = Some(id.clone());
let operation_hash: Option<String> = 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(),
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-compiler/src/build_project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -297,6 +296,7 @@ pub async fn commit_project(
&config.root_dir,
&persist_config,
config,
project_config,
operation_persister.as_ref(),
&log_event,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
*/

use crate::{
build_project::QueryID,
config::Config,
config::{Config, ProjectConfig},
config::{OperationPersister, PersistConfig},
errors::BuildProjectError,
Artifact, ArtifactContent,
Expand All @@ -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! {
Expand All @@ -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> {
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions compiler/crates/relay-compiler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ pub struct Config {
pub generate_extra_artifacts: Option<GenerateExtraArtifactsFn>,
pub generate_fragment_text_artifact: Option<GenerateFragmentTextArtifactFn>,
pub generate_operation_text_artifact: Option<GenerateOperationTextArtifactFn>,
pub generate_virtual_id_file_name: Option<Box<dyn Fn(StringKey) -> StringKey + Send + Sync>>,

/// Path to which to write the output of the compilation
pub artifact_writer: Box<dyn ArtifactWriter + Send + Sync>,

Expand Down Expand Up @@ -187,6 +189,7 @@ impl From<CliConfig> 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(),
Expand Down Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down

0 comments on commit eead274

Please sign in to comment.