Skip to content

Commit

Permalink
Do not prefix default project artifacts (#4426)
Browse files Browse the repository at this point in the history
Summary:
- Convert project name to enum with the `Default` variant.
- Update places where we're generating names for fragments/operations to use new formatter

Pull Request resolved: #4426

Test Plan:
cargo test in OSS
sanscastle

Reviewed By: voideanvalue

Differential Revision: D48803554

Pulled By: alunyov

fbshipit-source-id: c3e20da14325371dbb0265aab2204cd0ce770eab
  • Loading branch information
alunyov authored and facebook-github-bot committed Aug 30, 2023
1 parent 563c570 commit 27893cc
Show file tree
Hide file tree
Showing 36 changed files with 215 additions and 191 deletions.
Original file line number Diff line number Diff line change
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([ProjectName(\"with_custom_generated_dir\"), ProjectName(\"overlapping_generated_dir\")])."
"Overlapping input sources are incompatible with relative generated directories. Got file in a relative generated directory with source set ProjectSet([Named(\"with_custom_generated_dir\"), Named(\"overlapping_generated_dir\")])."
)),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ClientUser @__RelayResolverModel {
id: ID!
__relay_model_instance: RelayResolverValue
@relay_resolver(
fragment_name: "default_ClientUser__id"
fragment_name: "ClientUser__id"
import_path: "./path/to/UserModelResolver.js"
inject_fragment_data: "id"
)
Expand All @@ -20,7 +20,7 @@ type ClientUser @__RelayResolverModel {
extend type ClientUser {
name: RelayResolverValue
@relay_resolver(
fragment_name: "default_ClientUser____relay_model_instance"
fragment_name: "ClientUser____relay_model_instance"
import_path: "./path/to/UserNameResolver.js"
inject_fragment_data: "__relay_model_instance"
)
Expand Down Expand Up @@ -54,11 +54,11 @@ extend type Query {
"fragment": {
"args": null,
"kind": "FragmentSpread",
"name": "default_ClientUser____relay_model_instance"
"name": "ClientUser____relay_model_instance"
},
"kind": "RelayResolver",
"name": "name",
"resolverModule": require('relay-runtime/experimental').resolverDataInjector(require('default_ClientUser____relay_model_instance.graphql'), require('UserNameResolver'), '__relay_model_instance', false),
"resolverModule": require('relay-runtime/experimental').resolverDataInjector(require('ClientUser____relay_model_instance.graphql'), require('UserNameResolver'), '__relay_model_instance', false),
"path": "client_user.name"
}
],
Expand Down Expand Up @@ -154,19 +154,19 @@ Query Text is Empty.
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "default_ClientUser____relay_model_instance",
"name": "ClientUser____relay_model_instance",
"selections": [
{
"alias": null,
"args": null,
"fragment": {
"args": null,
"kind": "FragmentSpread",
"name": "default_ClientUser__id"
"name": "ClientUser__id"
},
"kind": "RelayResolver",
"name": "__relay_model_instance",
"resolverModule": require('relay-runtime/experimental').resolverDataInjector(require('default_ClientUser__id.graphql'), require('UserModelResolver'), 'id', true),
"resolverModule": require('relay-runtime/experimental').resolverDataInjector(require('ClientUser__id.graphql'), require('UserModelResolver'), 'id', true),
"path": "__relay_model_instance"
}
],
Expand All @@ -178,7 +178,7 @@ Query Text is Empty.
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "default_ClientUser__id",
"name": "ClientUser__id",
"selections": [
{
"kind": "ClientExtension",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ClientUser @__RelayResolverModel {
id: ID!
__relay_model_instance: RelayResolverValue
@relay_resolver(
fragment_name: "default_ClientUser__id"
fragment_name: "ClientUser__id"
import_path: "./path/to/UserModelResolver.js"
inject_fragment_data: "id"
)
Expand All @@ -19,7 +19,7 @@ type ClientUser @__RelayResolverModel {
extend type ClientUser {
name: RelayResolverValue
@relay_resolver(
fragment_name: "default_ClientUser____relay_model_instance"
fragment_name: "ClientUser____relay_model_instance"
import_path: "./path/to/UserNameResolver.js"
inject_fragment_data: "__relay_model_instance"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extend type Query {
{
"kind": "SplitOperation",
"metadata": {},
"name": "default_Query__client_types$normalization",
"name": "Query__client_types$normalization",
"selections": [
{
"kind": "ClientExtension",
Expand Down Expand Up @@ -64,7 +64,7 @@ extend type Query {
"normalizationInfo": {
"concreteType": "MyClientType",
"plural": false,
"normalizationNode": require('default_Query__client_types$normalization.graphql')
"normalizationNode": require('Query__client_types$normalization.graphql')
}
},
"linkedField": {
Expand Down Expand Up @@ -145,7 +145,7 @@ Query Text is Empty.
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "default_MyClientType____relay_model_instance",
"name": "MyClientType____relay_model_instance",
"selections": [
{
"kind": "ClientExtension",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extend type Query {
{
"kind": "SplitOperation",
"metadata": {},
"name": "default_Query__client_types$normalization",
"name": "Query__client_types$normalization",
"selections": [
{
"kind": "ClientExtension",
Expand Down Expand Up @@ -63,7 +63,7 @@ extend type Query {
"normalizationInfo": {
"concreteType": "MyClientType",
"plural": true,
"normalizationNode": require('default_Query__client_types$normalization.graphql')
"normalizationNode": require('Query__client_types$normalization.graphql')
}
},
"linkedField": {
Expand Down Expand Up @@ -144,7 +144,7 @@ Query Text is Empty.
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "default_MyClientType____relay_model_instance",
"name": "MyClientType____relay_model_instance",
"selections": [
{
"kind": "ClientExtension",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extend type Query {
{
"kind": "SplitOperation",
"metadata": {},
"name": "default_Query__client_type$normalization",
"name": "Query__client_type$normalization",
"selections": [
{
"kind": "ClientExtension",
Expand Down Expand Up @@ -63,7 +63,7 @@ extend type Query {
"normalizationInfo": {
"concreteType": "MyClientType",
"plural": false,
"normalizationNode": require('default_Query__client_type$normalization.graphql')
"normalizationNode": require('Query__client_type$normalization.graphql')
}
},
"linkedField": {
Expand Down Expand Up @@ -144,7 +144,7 @@ Query Text is Empty.
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "default_MyClientType____relay_model_instance",
"name": "MyClientType____relay_model_instance",
"selections": [
{
"kind": "ClientExtension",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ extend type User {
{
"kind": "SplitOperation",
"metadata": {},
"name": "default_User__pop_star_name$normalization",
"name": "User__pop_star_name$normalization",
"selections": [
{
"kind": "ClientExtension",
Expand Down Expand Up @@ -125,7 +125,7 @@ extend type User {
"normalizationInfo": {
"concreteType": "ClientUser",
"plural": false,
"normalizationNode": require('default_User__pop_star_name$normalization.graphql')
"normalizationNode": require('User__pop_star_name$normalization.graphql')
}
},
"linkedField": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type MyType @__RelayResolverModel {
__relay_model_instance: Int
@relay_resolver(
import_path: "/path/to/test/fixture/terse-relay-resolver-with-root-fragment-on-model.js"
fragment_name: "default_MyType__id"
fragment_name: "MyType__id"
inject_fragment_data: "id"
import_name: "MyType"
)
Expand Down Expand Up @@ -149,19 +149,19 @@ Query Text is Empty.
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "default_MyType____relay_model_instance",
"name": "MyType____relay_model_instance",
"selections": [
{
"alias": null,
"args": null,
"fragment": {
"args": null,
"kind": "FragmentSpread",
"name": "default_MyType__id"
"name": "MyType__id"
},
"kind": "RelayResolver",
"name": "__relay_model_instance",
"resolverModule": require('relay-runtime/experimental').resolverDataInjector(require('default_MyType__id.graphql'), require('terse-relay-resolver-with-root-fragment-on-model').MyType, 'id', true),
"resolverModule": require('relay-runtime/experimental').resolverDataInjector(require('MyType__id.graphql'), require('terse-relay-resolver-with-root-fragment-on-model').MyType, 'id', true),
"path": "__relay_model_instance"
}
],
Expand All @@ -173,7 +173,7 @@ Query Text is Empty.
"argumentDefinitions": [],
"kind": "Fragment",
"metadata": null,
"name": "default_MyType__id",
"name": "MyType__id",
"selections": [
{
"kind": "ClientExtension",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type MyType @__RelayResolverModel {
__relay_model_instance: Int
@relay_resolver(
import_path: "/path/to/test/fixture/terse-relay-resolver-with-root-fragment-on-model.js"
fragment_name: "default_MyType__id"
fragment_name: "MyType__id"
inject_fragment_data: "id"
import_name: "MyType"
)
Expand Down
3 changes: 2 additions & 1 deletion compiler/crates/relay-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod js_module_format;
mod module_import_config;
mod non_node_id_fields_config;
mod project_config;
mod project_name;
mod typegen_config;

pub use connection_interface::ConnectionInterface;
Expand All @@ -28,10 +29,10 @@ pub use project_config::LocalPersistAlgorithm;
pub use project_config::LocalPersistConfig;
pub use project_config::PersistConfig;
pub use project_config::ProjectConfig;
pub use project_config::ProjectName;
pub use project_config::RemotePersistConfig;
pub use project_config::SchemaConfig;
pub use project_config::SchemaLocation;
pub use project_name::ProjectName;
pub use typegen_config::CustomScalarType;
pub use typegen_config::CustomScalarTypeImport;
pub use typegen_config::FlowTypegenConfig;
Expand Down
28 changes: 1 addition & 27 deletions compiler/crates/relay-config/src/project_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,38 +35,12 @@ use crate::diagnostic_report_config::DiagnosticReportConfig;
use crate::module_import_config::ModuleImportConfig;
use crate::non_node_id_fields_config::NonNodeIdFieldsConfig;
use crate::JsModuleFormat;
use crate::ProjectName;
use crate::TypegenConfig;
use crate::TypegenLanguage;

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

#[derive(Debug, Clone, Copy, Serialize, Deserialize, Hash, PartialEq, Eq)]
pub struct ProjectName(StringKey);

impl Default for ProjectName {
fn default() -> Self {
Self("default".intern())
}
}

impl fmt::Display for ProjectName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl From<StringKey> for ProjectName {
fn from(key: StringKey) -> Self {
Self(key)
}
}

impl From<ProjectName> for StringKey {
fn from(project_name: ProjectName) -> Self {
project_name.0
}
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct RemotePersistConfig {
Expand Down
95 changes: 95 additions & 0 deletions compiler/crates/relay-config/src/project_name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

use std::fmt;

use intern::string_key::Intern;
use intern::string_key::StringKey;
use intern::Lookup;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use serde::Serializer;

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum ProjectName {
Default,
Named(StringKey),
}

impl ProjectName {
pub fn generate_name_for_object_and_field(
&self,
object_name: StringKey,
field_name: StringKey,
) -> String {
match self {
ProjectName::Named(project_name) => {
format!("{}_{}__{}", project_name, object_name, field_name)
}
ProjectName::Default => format!("{}__{}", object_name, field_name),
}
}
}

impl Default for ProjectName {
fn default() -> Self {
Self::Default
}
}

impl fmt::Display for ProjectName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Self::Named(value) = self {
write!(f, "{}", value)
} else {
write!(f, "default")
}
}
}

impl From<StringKey> for ProjectName {
fn from(key: StringKey) -> Self {
match key.lookup() {
"default" => Self::Default,
_ => Self::Named(key),
}
}
}

impl From<ProjectName> for StringKey {
fn from(project_name: ProjectName) -> Self {
match project_name {
ProjectName::Default => "default".intern(),
ProjectName::Named(name) => name,
}
}
}

impl Serialize for ProjectName {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(match self {
ProjectName::Default => "default",
ProjectName::Named(name) => name.lookup(),
})
}
}

impl<'de> Deserialize<'de> for ProjectName {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Deserialize::deserialize(deserializer).map(|s: String| match s.as_str() {
"default" => ProjectName::Default,
s => ProjectName::Named(s.intern()),
})
}
}
Loading

0 comments on commit 27893cc

Please sign in to comment.