Skip to content

Commit

Permalink
Update the name of the generated files for resolvers to include proje…
Browse files Browse the repository at this point in the history
…ct name as prefix (#4418)

Summary:
We have two methods that generate files for resolvers. This diff is passing the `project_name` to these methods, and we're using it as a prefix for fragment/operation name. These fragment/operation names will be used to generate artifacts.

With this prefix we will prevent possible collisions of generated artifacts for projects that have resolvers with the same name.

Pull Request resolved: #4418

Test Plan:
- Updated Snapshot Tests
- Run compiler in OSS
- Run compiler on XPLAT + Adding new model resolver

Also confirmed that there is no flow errors with this change on the Zenon refactoing: D48738810

Reviewed By: captbaritone

Differential Revision: D48608429

Pulled By: alunyov

fbshipit-source-id: bd59c426458e04ce41a8d416a576f497aeb443d3
  • Loading branch information
alunyov authored and facebook-github-bot committed Aug 29, 2023
1 parent 15c2715 commit b50b78b
Show file tree
Hide file tree
Showing 101 changed files with 555 additions and 432 deletions.
6 changes: 3 additions & 3 deletions compiler/crates/relay-bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use clap::ArgEnum;
use clap::Parser;
use common::ConsoleLogger;
use intern::string_key::Intern;
use intern::Lookup;
use log::error;
use log::info;
use relay_compiler::build_project::artifact_writer::ArtifactValidationWriter;
Expand All @@ -26,6 +25,7 @@ use relay_compiler::FileSourceKind;
use relay_compiler::LocalPersister;
use relay_compiler::OperationPersister;
use relay_compiler::PersistConfig;
use relay_compiler::ProjectName;
use relay_compiler::RemotePersister;
use relay_lsp::start_language_server;
use relay_lsp::DummyExtraDataProvider;
Expand Down Expand Up @@ -217,7 +217,7 @@ fn set_project_flag(config: &mut Config, projects: Vec<String>) -> Result<(), Er
project_config.enabled = false;
}
for selected_project in projects {
let selected_project = selected_project.intern();
let selected_project = ProjectName::from(selected_project.intern());

if let Some(project_config) = config.projects.get_mut(&selected_project) {
project_config.enabled = true;
Expand All @@ -229,7 +229,7 @@ fn set_project_flag(config: &mut Config, projects: Vec<String>) -> Result<(), Er
config
.projects
.keys()
.map(|name| name.lookup())
.map(|name| name.to_string())
.collect::<Vec<_>>()
.join(", ")
),
Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/relay-codegen/tests/client_edges/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use relay_codegen::print_fragment;
use relay_codegen::print_operation;
use relay_codegen::JsModuleFormat;
use relay_config::ProjectConfig;
use relay_config::ProjectName;
use relay_test_schema::get_test_schema_with_extensions;
use relay_transforms::client_edges;
use relay_transforms::relay_resolvers;
Expand All @@ -30,7 +31,7 @@ pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
let program = Program::from_definitions(Arc::clone(&schema), ir);
let next_program = sort_selections(
&client_edges(&program, &Default::default())
.and_then(|program| relay_resolvers(&program, true))
.and_then(|program| relay_resolvers(ProjectName::default(), &program, true))
.unwrap(),
);
let mut result = next_program
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-compiler-playground/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn get_project_config(
) -> Result<ProjectConfig, PlaygroundError> {
let feature_flags: FeatureFlags = serde_json::from_str(feature_flags_json)
.map_err(|err| PlaygroundError::ConfigError(format!("{}", err)))?;
let project_name = "test_project".intern();
let project_name = "test_project".intern().into();
let typegen_config: TypegenConfig = typegen_config_json
.map(|str| {
serde_json::from_str(str)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use docblock_syntax::DocblockAST;
use errors::try_all;
use fnv::FnvHashMap;
use graphql_syntax::ExecutableDefinition;
use relay_config::ProjectName;
use relay_docblock::extend_schema_with_resolver_type_system_definition;
use schema::SDLSchema;

use crate::compiler_state::CompilerState;
use crate::compiler_state::ProjectName;
use crate::config::ProjectConfig;
use crate::docblocks::build_schema_documents_from_docblocks;
use crate::docblocks::parse_docblock_asts_from_sources;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use std::sync::Arc;

use common::DiagnosticsResult;
use fnv::FnvHashMap;
use relay_config::ProjectName;
use schema::SDLSchema;

use super::build_resolvers_schema::extend_schema_with_resolvers;
use crate::compiler_state::CompilerState;
use crate::compiler_state::ProjectName;
use crate::config::ProjectConfig;
use crate::GraphQLAsts;

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 @@ -50,6 +50,7 @@ use log::warn;
use rayon::iter::IntoParallelRefIterator;
use rayon::slice::ParallelSlice;
use relay_codegen::Printer;
use relay_config::ProjectName;
use relay_transforms::apply_transforms;
use relay_transforms::CustomTransformsConfig;
use relay_transforms::Programs;
Expand All @@ -70,7 +71,6 @@ use crate::artifact_map::ArtifactMap;
use crate::artifact_map::ArtifactSourceKey;
use crate::compiler_state::ArtifactMapKind;
use crate::compiler_state::CompilerState;
use crate::compiler_state::ProjectName;
use crate::config::Config;
use crate::config::ProjectConfig;
use crate::errors::BuildProjectError;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use graphql_ir::FragmentDefinitionNameSet;
use graphql_ir::OperationDefinitionName;
use graphql_syntax::ExecutableDefinition;
use relay_config::ProjectConfig;
use relay_config::ProjectName;
use relay_transforms::get_resolver_fragment_dependency_name;
use schema::SDLSchema;
use schema::Schema;

use crate::compiler_state::ProjectName;
use crate::errors::BuildProjectError;
use crate::GraphQLAsts;

Expand Down
11 changes: 4 additions & 7 deletions compiler/crates/relay-compiler/src/compiler_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use dashmap::DashSet;
use fnv::FnvBuildHasher;
use fnv::FnvHashMap;
use fnv::FnvHashSet;
use intern::string_key::StringKey;
use rayon::prelude::*;
use relay_config::ProjectName;
use relay_config::SchemaConfig;
use schema::SDLSchema;
use schema_diff::definitions::SchemaChange;
Expand All @@ -54,9 +54,6 @@ use crate::file_source::LocatedGraphQLSource;
use crate::file_source::LocatedJavascriptSourceFeatures;
use crate::file_source::SourceControlUpdateStatus;

/// Name of a compiler project.
pub type ProjectName = StringKey;

/// Set of project names.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(from = "DeserializableProjectSet")]
Expand All @@ -79,7 +76,7 @@ impl ProjectSet {
existing_names.push(project_name);
}

pub fn iter(&self) -> slice::Iter<'_, StringKey> {
pub fn iter(&self) -> slice::Iter<'_, ProjectName> {
self.0.iter()
}

Expand All @@ -94,7 +91,7 @@ impl ProjectSet {

impl IntoIterator for ProjectSet {
type Item = ProjectName;
type IntoIter = vec::IntoIter<StringKey>;
type IntoIter = vec::IntoIter<ProjectName>;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
Expand Down Expand Up @@ -439,7 +436,7 @@ impl CompilerState {
/// This method is looking at the pending schema changes to see if they may be breaking (removed types, renamed field, etc)
pub fn has_breaking_schema_change(
&self,
project_name: StringKey,
project_name: ProjectName,
schema_config: &SchemaConfig,
) -> bool {
if let Some(extension) = self.extensions.get(&project_name) {
Expand Down
7 changes: 3 additions & 4 deletions compiler/crates/relay-compiler/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use fnv::FnvHashSet;
use graphql_ir::OperationDefinition;
use graphql_ir::Program;
use indexmap::IndexMap;
use intern::string_key::Intern;
use intern::string_key::StringKey;
use js_config_loader::LoaderSource;
use log::warn;
Expand All @@ -38,6 +37,7 @@ pub use relay_config::LocalPersistConfig;
use relay_config::ModuleImportConfig;
pub use relay_config::PersistConfig;
pub use relay_config::ProjectConfig;
use relay_config::ProjectName;
pub use relay_config::RemotePersistConfig;
use relay_config::SchemaConfig;
pub use relay_config::SchemaLocation;
Expand All @@ -59,7 +59,6 @@ use crate::build_project::generate_extra_artifacts::GenerateExtraArtifactsFn;
use crate::build_project::get_artifacts_file_hash_map::GetArtifactsFileHashMapFn;
use crate::build_project::AdditionalValidations;
use crate::compiler_state::CompilerState;
use crate::compiler_state::ProjectName;
use crate::compiler_state::ProjectSet;
use crate::errors::ConfigValidationError;
use crate::errors::Error;
Expand Down Expand Up @@ -662,7 +661,7 @@ struct MultiProjectConfigFile {
#[serde(deny_unknown_fields, rename_all = "camelCase", default)]
pub struct SingleProjectConfigFile {
#[serde(skip)]
pub project_name: StringKey,
pub project_name: ProjectName,

/// Path to schema.graphql
pub schema: PathBuf,
Expand Down Expand Up @@ -740,7 +739,7 @@ pub struct SingleProjectConfigFile {
impl Default for SingleProjectConfigFile {
fn default() -> Self {
Self {
project_name: "default".intern(),
project_name: ProjectName::default(),
schema: Default::default(),
src: Default::default(),
artifact_directory: Default::default(),
Expand Down
7 changes: 5 additions & 2 deletions compiler/crates/relay-compiler/src/docblocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ fn parse_source(
definitions: Option<&Vec<ExecutableDefinition>>,
) -> DiagnosticsResult<Option<SchemaDocument>> {
let maybe_ir = parse_docblock_ast(
&ast,
project_config.name,
ast,
definitions,
ParseOptions {
enable_output_type: &project_config
Expand All @@ -81,6 +82,8 @@ fn parse_source(
},
)?;
maybe_ir
.map(|ir| ir.to_graphql_schema_ast(schema, &project_config.schema_config))
.map(|ir| {
ir.to_graphql_schema_ast(project_config.name, schema, &project_config.schema_config)
})
.transpose()
}
3 changes: 1 addition & 2 deletions compiler/crates/relay-compiler/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ use std::path::PathBuf;
use common::Diagnostic;
use glob::PatternError;
use persist_query::PersistError;
use relay_config::ProjectName;
use thiserror::Error;

use crate::compiler_state::ProjectName;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Error)]
Expand Down
22 changes: 11 additions & 11 deletions compiler/crates/relay-compiler/src/file_source/file_categorizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ use common::sync::ParallelIterator;
use fnv::FnvHashSet;
use log::warn;
use rayon::iter::IntoParallelRefIterator;
use relay_config::ProjectName;
use relay_typegen::TypegenLanguage;

use super::file_filter::FileFilter;
use super::File;
use super::FileGroup;
use crate::compiler_state::ProjectName;
use crate::compiler_state::ProjectSet;
use crate::config::Config;
use crate::config::SchemaLocation;
Expand Down Expand Up @@ -418,23 +418,23 @@ mod tests {
.categorize(&PathBuf::from("src/js/a.js"))
.unwrap(),
FileGroup::Source {
project_set: ProjectSet::of("public".intern()),
project_set: ProjectSet::of("public".intern().into()),
},
);
assert_eq!(
categorizer
.categorize(&PathBuf::from("src/js/nested/b.js"))
.unwrap(),
FileGroup::Source {
project_set: ProjectSet::of("public".intern()),
project_set: ProjectSet::of("public".intern().into()),
},
);
assert_eq!(
categorizer
.categorize(&PathBuf::from("src/js/internal/nested/c.js"))
.unwrap(),
FileGroup::Source {
project_set: ProjectSet::of("internal".intern()),
project_set: ProjectSet::of("internal".intern().into()),
},
);
assert_eq!(
Expand All @@ -446,47 +446,47 @@ mod tests {
.categorize(&PathBuf::from("src/custom/custom-generated/c.js"))
.unwrap(),
FileGroup::Source {
project_set: ProjectSet::of("with_custom_generated_dir".intern()),
project_set: ProjectSet::of("with_custom_generated_dir".intern().into()),
},
);
assert_eq!(
categorizer
.categorize(&PathBuf::from("src/js/internal/nested/__generated__/c.js"))
.unwrap(),
FileGroup::Generated {
project_name: "internal".intern()
project_name: "internal".intern().into()
},
);
assert_eq!(
categorizer
.categorize(&PathBuf::from("graphql/custom-generated/c.js"))
.unwrap(),
FileGroup::Generated {
project_name: "with_custom_generated_dir".intern()
project_name: "with_custom_generated_dir".intern().into()
},
);
assert_eq!(
categorizer
.categorize(&PathBuf::from("graphql/public.graphql"))
.unwrap(),
FileGroup::Schema {
project_set: ProjectSet::of("public".intern())
project_set: ProjectSet::of("public".intern().into())
},
);
assert_eq!(
categorizer
.categorize(&PathBuf::from("graphql/__generated__/internal.graphql"))
.unwrap(),
FileGroup::Schema {
project_set: ProjectSet::of("internal".intern())
project_set: ProjectSet::of("internal".intern().into())
},
);
assert_eq!(
categorizer
.categorize(&PathBuf::from("src/typescript/a.ts"))
.unwrap(),
FileGroup::Source {
project_set: ProjectSet::of("typescript".intern()),
project_set: ProjectSet::of("typescript".intern().into()),
},
);
}
Expand Down Expand Up @@ -540,7 +540,7 @@ mod tests {
assert_eq!(
categorizer.categorize(&PathBuf::from("src/custom_overlapping/__generated__/c.js")),
Err(Cow::Borrowed(
"Overlapping input sources are incompatible with relative generated directories. Got file in a relative generated directory with source set ProjectSet([\"with_custom_generated_dir\", \"overlapping_generated_dir\"])."
"Overlapping input sources are incompatible with relative generated directories. Got file in a relative generated directory with source set ProjectSet([ProjectName(\"with_custom_generated_dir\"), ProjectName(\"overlapping_generated_dir\")])."
)),
);
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/crates/relay-compiler/src/file_source/file_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::path::PathBuf;

use fnv::FnvHashSet;
use glob::Pattern;
use intern::string_key::StringKey;
use relay_config::ProjectName;

use crate::config::Config;
use crate::config::SchemaLocation;
Expand Down Expand Up @@ -58,7 +58,7 @@ impl FileFilter {
}

// Get roots for extensions, schemas and output dirs
fn get_extra_roots(config: &Config, enabled_projects: &FnvHashSet<StringKey>) -> Vec<PathBuf> {
fn get_extra_roots(config: &Config, enabled_projects: &FnvHashSet<ProjectName>) -> Vec<PathBuf> {
let mut roots = vec![];
for project_config in config.projects.values() {
if !enabled_projects.contains(&project_config.name) {
Expand All @@ -78,7 +78,7 @@ fn get_extra_roots(config: &Config, enabled_projects: &FnvHashSet<StringKey>) ->
unify_roots(roots)
}

fn get_sources_root(config: &Config, enabled_projects: &FnvHashSet<StringKey>) -> Vec<PathBuf> {
fn get_sources_root(config: &Config, enabled_projects: &FnvHashSet<ProjectName>) -> Vec<PathBuf> {
unify_roots(
config
.sources
Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/relay-compiler/src/file_source/file_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* LICENSE file in the root directory of this source tree.
*/

use crate::compiler_state::ProjectName;
use relay_config::ProjectName;

use crate::compiler_state::ProjectSet;

#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down
2 changes: 1 addition & 1 deletion compiler/crates/relay-compiler/src/graphql_asts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ use graphql_ir::ExecutableDefinitionName;
use graphql_ir::FragmentDefinitionName;
use graphql_ir::OperationDefinitionName;
use graphql_syntax::ExecutableDefinition;
use relay_config::ProjectName;

use crate::artifact_map::ArtifactSourceKey;
use crate::compiler_state::GraphQLSources;
use crate::compiler_state::ProjectName;
use crate::errors::Error;
use crate::errors::Result;
use crate::file_source::LocatedGraphQLSource;
Expand Down
1 change: 1 addition & 0 deletions compiler/crates/relay-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ pub use file_source::SourceReader;
pub use graphql_asts::GraphQLAsts;
pub use operation_persister::LocalPersister;
pub use operation_persister::RemotePersister;
pub use relay_config::ProjectName;
Loading

0 comments on commit b50b78b

Please sign in to comment.