Skip to content

Commit

Permalink
Store connection_interface on the project_config
Browse files Browse the repository at this point in the history
Reviewed By: tyao1

Differential Revision: D33163325

fbshipit-source-id: 7f0395a5431fe3df7c42dbf38fd08a110d0f83f4
  • Loading branch information
alunyov authored and facebook-github-bot committed Dec 17, 2021
1 parent b0c5a84 commit e72ea12
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 68 deletions.
4 changes: 1 addition & 3 deletions compiler/crates/relay-compiler-playground/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use intern::string_key::Intern;
use relay_codegen::{print_fragment, print_operation, JsModuleFormat};
use relay_config::ProjectConfig;
use relay_schema::build_schema_with_extensions;
use relay_transforms::{apply_transforms, ConnectionInterface, Programs};
use relay_transforms::{apply_transforms, Programs};
use relay_typegen::{
generate_fragment_type_exports_section, generate_operation_type_exports_section, TypegenConfig,
};
Expand Down Expand Up @@ -300,7 +300,6 @@ fn get_programs(

let project_name = "test_project".intern();
let base_fragment_names = Arc::new(Default::default());
let connection_interface = ConnectionInterface::default();
let feature_flags: FeatureFlags = serde_json::from_str(feature_flags_json)
.map_err(|err| PlaygroundError::ConfigError(format!("{}", err)))?;
let perf_logger = NoopPerfLogger;
Expand All @@ -313,7 +312,6 @@ fn get_programs(
&project_config,
Arc::new(program),
base_fragment_names,
&connection_interface,
Arc::new(perf_logger),
None,
)
Expand Down
18 changes: 5 additions & 13 deletions compiler/crates/relay-compiler/src/build_project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use artifact_generated_types::ArtifactGeneratedTypes;
use build_ir::BuildIRResult;
pub use build_ir::SourceHashes;
pub use build_schema::build_schema;
use common::{sync::*, FeatureFlags, PerfLogEvent, PerfLogger};
use common::{sync::*, PerfLogEvent, PerfLogger};
use dashmap::{mapref::entry::Entry, DashSet};
use fnv::{FnvBuildHasher, FnvHashMap, FnvHashSet};
pub use generate_artifacts::{
Expand Down Expand Up @@ -99,19 +99,14 @@ pub fn build_raw_program(

pub fn validate_program(
config: &Config,
feature_flags: &FeatureFlags,
project_config: &ProjectConfig,
program: &Program,
log_event: &impl PerfLogEvent,
) -> Result<(), BuildProjectError> {
let timer = log_event.start("validate_time");
log_event.number("validate_documents_count", program.document_count());
let result = validate(
program,
feature_flags,
&config.connection_interface,
&config.additional_validations,
)
.map_err(|errors| BuildProjectError::ValidationErrors { errors });
let result = validate(program, project_config, &config.additional_validations)
.map_err(|errors| BuildProjectError::ValidationErrors { errors });

log_event.stop(timer);

Expand All @@ -120,7 +115,6 @@ pub fn validate_program(

/// Apply various chains of transforms to create a set of output programs.
pub fn transform_program(
config: &Config,
project_config: &ProjectConfig,
program: Arc<Program>,
base_fragment_names: Arc<StringKeySet>,
Expand All @@ -132,7 +126,6 @@ pub fn transform_program(
project_config,
program,
base_fragment_names,
&config.connection_interface,
perf_logger,
Some(print_stats),
)
Expand Down Expand Up @@ -178,7 +171,7 @@ pub fn build_programs(
let (validation_results, _) = rayon::join(
|| {
// Call validation rules that go beyond type checking.
validate_program(config, &project_config.feature_flags, &program, log_event)
validate_program(config, project_config, &program, log_event)
},
|| {
find_resolver_dependencies(
Expand All @@ -194,7 +187,6 @@ pub fn build_programs(
validation_results?;

let programs = transform_program(
config,
project_config,
Arc::new(program),
Arc::new(base_fragment_names),
Expand Down
10 changes: 5 additions & 5 deletions compiler/crates/relay-compiler/src/build_project/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,35 @@
use common::{DiagnosticsResult, FeatureFlags};
use errors::try_all;
use graphql_ir::Program;
use relay_config::ProjectConfig;
use relay_transforms::{
disallow_circular_no_inline_fragments, disallow_reserved_aliases, disallow_typename_on_root,
validate_assignable_directive, validate_connections, validate_module_names,
validate_no_double_underscore_alias, validate_no_inline_fragments_with_raw_response_type,
validate_relay_directives, validate_unused_fragment_variables, validate_unused_variables,
validate_updatable_directive, ConnectionInterface,
validate_updatable_directive,
};

pub type AdditionalValidations =
Box<dyn Fn(&Program, &FeatureFlags) -> DiagnosticsResult<()> + Sync + Send>;

pub fn validate(
program: &Program,
feature_flags: &FeatureFlags,
connection_interface: &ConnectionInterface,
project_config: &ProjectConfig,
additional_validations: &Option<AdditionalValidations>,
) -> DiagnosticsResult<()> {
try_all(vec![
disallow_reserved_aliases(program),
validate_no_double_underscore_alias(program),
validate_unused_variables(program),
validate_unused_fragment_variables(program),
validate_connections(program, connection_interface),
validate_connections(program, &project_config.schema_config.connection_interface),
validate_relay_directives(program),
validate_module_names(program),
validate_no_inline_fragments_with_raw_response_type(program),
disallow_typename_on_root(program),
if let Some(ref validate) = additional_validations {
validate(program, feature_flags)
validate(program, &project_config.feature_flags)
} else {
Ok(())
},
Expand Down
17 changes: 7 additions & 10 deletions compiler/crates/relay-compiler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ use intern::string_key::{Intern, StringKey};
use persist_query::PersistError;
use rayon::prelude::*;
use regex::Regex;
use relay_config::{FlowTypegenConfig, JsModuleFormat, TypegenConfig, TypegenLanguage};
use relay_config::{
FlowTypegenConfig, JsModuleFormat, SchemaConfig, TypegenConfig, TypegenLanguage,
};
pub use relay_config::{PersistConfig, ProjectConfig, SchemaLocation};
use relay_transforms::ConnectionInterface;
use serde::de::Error as DeError;
use serde::{Deserialize, Deserializer, Serialize};
use serde_json::Value;
Expand Down Expand Up @@ -81,8 +82,6 @@ pub struct Config {
/// Do not reuse persist ids from artifacts even if the text hash matches.
pub repersist_operations: bool,

pub connection_interface: ConnectionInterface,

pub saved_state_config: Option<ScmAwareClockData>,
pub saved_state_loader: Option<Box<dyn SavedStateLoader + Send + Sync>>,
pub saved_state_version: String,
Expand Down Expand Up @@ -257,6 +256,7 @@ impl Config {
shard_output: config_file_project.shard_output,
shard_strip_regex,
schema_location,
schema_config: config_file_project.schema_config,
typegen_config: config_file_project.typegen_config,
persist: config_file_project.persist,
variable_names_comment: config_file_project.variable_names_comment,
Expand Down Expand Up @@ -299,7 +299,6 @@ impl Config {
saved_state_config: config_file.saved_state_config,
saved_state_loader: None,
saved_state_version: hex::encode(hash.result()),
connection_interface: config_file.connection_interface,
operation_persister: None,
compile_everything: false,
repersist_operations: false,
Expand Down Expand Up @@ -449,7 +448,6 @@ impl fmt::Debug for Config {
generate_extra_artifacts,
saved_state_config,
saved_state_loader,
connection_interface,
saved_state_version,
operation_persister,
post_artifacts_write,
Expand Down Expand Up @@ -484,7 +482,6 @@ impl fmt::Debug for Config {
"saved_state_loader",
&option_fn_to_string(saved_state_loader),
)
.field("connection_interface", connection_interface)
.field("saved_state_version", saved_state_version)
.field(
"post_artifacts_write",
Expand Down Expand Up @@ -534,9 +531,6 @@ struct MultiProjectConfigFile {
/// Configuration of projects to compile.
projects: FnvIndexMap<ProjectName, ConfigFileProject>,

#[serde(default)]
connection_interface: ConnectionInterface,

#[serde(default)]
feature_flags: FeatureFlags,

Expand Down Expand Up @@ -810,6 +804,9 @@ struct ConfigFileProject {

#[serde(default)]
js_module_format: JsModuleFormat,

#[serde(default)]
pub schema_config: SchemaConfig,
}

type PersistId = String;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use relay_codegen::{
};
use relay_compiler::{validate, ProjectConfig};
use relay_test_schema::{get_test_schema, get_test_schema_with_extensions};
use relay_transforms::{apply_transforms, ConnectionInterface, DIRECTIVE_SPLIT_OPERATION};
use relay_transforms::{apply_transforms, DIRECTIVE_SPLIT_OPERATION};
use std::{array, sync::Arc};

pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
Expand Down Expand Up @@ -59,16 +59,6 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?;
let program = Program::from_definitions(Arc::clone(&schema), ir);

let connection_interface = ConnectionInterface::default();

validate(
&program,
&FeatureFlags::default(),
&connection_interface,
&None,
)
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?;

let feature_flags = FeatureFlags {
enable_flight_transform: true,
hash_supported_argument: FeatureFlag::Limited {
Expand All @@ -82,18 +72,21 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
enable_client_edges: FeatureFlag::Enabled,
enable_provided_variables: FeatureFlag::Enabled,
};

let project_config = ProjectConfig {
name: "test".intern(),
feature_flags: Arc::new(feature_flags),
..Default::default()
};

validate(&program, &project_config, &None)
.map_err(|diagnostics| diagnostics_to_sorted_string(fixture.content, &diagnostics))?;

// TODO pass base fragment names
let programs = apply_transforms(
&project_config,
Arc::new(program),
Default::default(),
&connection_interface,
Arc::new(ConsoleLogger),
None,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

use intern::string_key::{Intern, StringKey};

use serde::{Deserialize, Serialize};

/// Configuration where Relay should expect some fields in the schema.
Expand Down
4 changes: 3 additions & 1 deletion compiler/crates/relay-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#![deny(rust_2018_idioms)]
#![deny(clippy::all)]

mod connection_interface;
mod js_module_format;
mod project_config;
mod typegen_config;

pub use connection_interface::ConnectionInterface;
pub use js_module_format::JsModuleFormat;
pub use project_config::{PersistConfig, ProjectConfig, ProjectName, SchemaLocation};
pub use project_config::{PersistConfig, ProjectConfig, ProjectName, SchemaConfig, SchemaLocation};
pub use typegen_config::{FlowTypegenConfig, FlowTypegenPhase, TypegenConfig, TypegenLanguage};
13 changes: 11 additions & 2 deletions compiler/crates/relay-config/src/project_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use intern::string_key::{Intern, StringKey};
use regex::Regex;
use serde::{Deserialize, Serialize};

use crate::{JsModuleFormat, TypegenConfig};
use crate::{connection_interface::ConnectionInterface, JsModuleFormat, TypegenConfig};

type FnvIndexMap<K, V> = IndexMap<K, V, FnvBuildHasher>;

Expand All @@ -37,6 +37,12 @@ pub enum SchemaLocation {
Directory(PathBuf),
}

#[derive(Default, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaConfig {
pub connection_interface: ConnectionInterface,
}

pub struct ProjectConfig {
pub name: ProjectName,
pub base: Option<ProjectName>,
Expand All @@ -47,7 +53,7 @@ pub struct ProjectConfig {
pub schema_extensions: Vec<PathBuf>,
pub enabled: bool,
pub schema_location: SchemaLocation,
// pub schema_config: SchemaConfig, TODO: Add this and use in D33131805
pub schema_config: SchemaConfig,
pub typegen_config: TypegenConfig,
pub persist: Option<PersistConfig>,
pub variable_names_comment: bool,
Expand All @@ -74,6 +80,7 @@ impl Default for ProjectConfig {
schema_extensions: vec![],
enabled: true,
schema_location: SchemaLocation::File(PathBuf::default()),
schema_config: Default::default(),
typegen_config: Default::default(),
persist: None,
variable_names_comment: false,
Expand All @@ -99,6 +106,7 @@ impl Debug for ProjectConfig {
schema_extensions,
enabled,
schema_location,
schema_config,
typegen_config,
persist,
variable_names_comment,
Expand All @@ -120,6 +128,7 @@ impl Debug for ProjectConfig {
.field("schema_extensions", schema_extensions)
.field("enabled", enabled)
.field("schema_location", schema_location)
.field("schema_config", schema_config)
.field("typegen_config", typegen_config)
.field("persist", persist)
.field("variable_names_comment", variable_names_comment)
Expand Down
5 changes: 1 addition & 4 deletions compiler/crates/relay-lsp/src/graphql_tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use graphql_syntax::parse_executable_with_error_recovery;
use graphql_text_printer::print_full_operation;
use intern::string_key::{Intern, StringKey};
use lsp_types::{request::Request, Url};
use relay_compiler::config::{Config, ProjectConfig};
use relay_compiler::config::ProjectConfig;
use relay_transforms::{apply_transforms, Programs};
use schema::SDLSchema;
use schema_documentation::SchemaDocumentation;
Expand Down Expand Up @@ -124,15 +124,13 @@ fn get_operation_only_program(
/// that may generate full operation text
fn transform_program<TPerfLogger: PerfLogger + 'static>(
project_config: &ProjectConfig,
config: Arc<Config>,
program: Arc<Program>,
perf_logger: Arc<TPerfLogger>,
) -> Result<Programs, String> {
apply_transforms(
project_config,
program,
Default::default(),
&config.connection_interface,
perf_logger,
None,
)
Expand Down Expand Up @@ -241,7 +239,6 @@ pub(crate) fn get_query_text<
if let Some(program) = get_operation_only_program(operation, fragments, &program) {
let programs = transform_program(
project_config,
Arc::clone(&state.config),
Arc::new(program),
Arc::clone(&state.perf_logger),
)
Expand Down
5 changes: 2 additions & 3 deletions compiler/crates/relay-lsp/src/server/lsp_state_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use std::sync::{Arc, RwLock};

use common::{FeatureFlags, PerfLogEvent, PerfLogger};
use common::{PerfLogEvent, PerfLogger};
use dashmap::mapref::entry::Entry;
use fnv::FnvHashMap;
use graphql_watchman::WatchmanFileSourceSubscriptionNextChange;
Expand Down Expand Up @@ -378,13 +378,12 @@ impl<TPerfLogger: PerfLogger + 'static, TSchemaDocumentation: SchemaDocumentatio

validate_program(
&self.lsp_state.config,
&FeatureFlags::default(),
project_config,
&base_program,
log_event,
)?;

transform_program(
&self.lsp_state.config,
project_config,
Arc::new(base_program),
Arc::new(base_fragment_names),
Expand Down
1 change: 0 additions & 1 deletion compiler/crates/relay-transforms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ parking_lot = { version = "0.11.2", features = ["send_guard"] }
regex = "1.5.4"
relay-config = { path = "../relay-config" }
schema = { path = "../schema" }
serde = { version = "1.0.126", features = ["derive", "rc"] }
thiserror = "1.0.29"

[dev-dependencies]
Expand Down
Loading

0 comments on commit e72ea12

Please sign in to comment.