Skip to content

Commit

Permalink
consolidate __relay_model_instance definitions
Browse files Browse the repository at this point in the history
Summary:
The `"__relay_model_instance"` field is defined in several different places in the compiler code.

This diff defines it in `relay/oss/crates/docblock-shared/src/lib.rs` as the source of truth, where related fields also reside e.g. `RELAY_RESOLVER_MODEL_DIRECTIVE_NAME`.

Reviewed By: alunyov

Differential Revision: D51376238

fbshipit-source-id: 0b5c5481bc1500b4ebc166efcbe204722befb614
  • Loading branch information
monicatang authored and facebook-github-bot committed Nov 16, 2023
1 parent 45ed255 commit a2c57d9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
3 changes: 3 additions & 0 deletions compiler/crates/docblock-shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ lazy_static! {
pub static ref EDGE_TO_FIELD: StringKey = "edgeTo".intern();
pub static ref DEPRECATED_FIELD: StringKey = "deprecated".intern();
pub static ref LIVE_FIELD: StringKey = "live".intern();
// Using a longer name version for this "special" field
// help us avoid potential collision with product code (__self, __instance can be used for something else)
pub static ref RELAY_RESOLVER_MODEL_INSTANCE_FIELD: StringKey = "__relay_model_instance".intern();
pub static ref ROOT_FRAGMENT_FIELD: StringKey = "rootFragment".intern();
pub static ref OUTPUT_TYPE_FIELD: StringKey = "outputType".intern();
pub static ref WEAK_FIELD: StringKey = "weak".intern();
Expand Down
8 changes: 4 additions & 4 deletions compiler/crates/relay-docblock/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use docblock_shared::KEY_RESOLVER_ID_FIELD;
use docblock_shared::LIVE_ARGUMENT_NAME;
use docblock_shared::RELAY_RESOLVER_DIRECTIVE_NAME;
use docblock_shared::RELAY_RESOLVER_MODEL_DIRECTIVE_NAME;
use docblock_shared::RELAY_RESOLVER_MODEL_INSTANCE_FIELD;
use docblock_shared::RELAY_RESOLVER_SOURCE_HASH;
use docblock_shared::RELAY_RESOLVER_SOURCE_HASH_VALUE;
use docblock_shared::RELAY_RESOLVER_WEAK_OBJECT_DIRECTIVE;
Expand Down Expand Up @@ -84,7 +85,6 @@ lazy_static! {
static ref DEPRECATED_RESOLVER_DIRECTIVE_NAME: DirectiveName =
DirectiveName("deprecated".intern());
static ref DEPRECATED_REASON_ARGUMENT_NAME: ArgumentName = ArgumentName("reason".intern());
static ref RESOLVER_MODEL_INSTANCE_FIELD_NAME: StringKey = "__relay_model_instance".intern();
static ref MODEL_CUSTOM_SCALAR_TYPE_SUFFIX: StringKey = "Model".intern();
}

Expand Down Expand Up @@ -1518,13 +1518,13 @@ fn get_root_fragment_for_object(
project_name
.generate_name_for_object_and_field(
object.unwrap().name.item.0,
*RESOLVER_MODEL_INSTANCE_FIELD_NAME,
*RELAY_RESOLVER_MODEL_INSTANCE_FIELD,
)
.intern(),
)),
generated: true,
inject_fragment_data: Some(FragmentDataInjectionMode::Field(
*RESOLVER_MODEL_INSTANCE_FIELD_NAME,
*RELAY_RESOLVER_MODEL_INSTANCE_FIELD,
)),
})
} else {
Expand Down Expand Up @@ -1556,7 +1556,7 @@ fn generate_model_instance_field(
});

FieldDefinition {
name: string_key_as_identifier(*RESOLVER_MODEL_INSTANCE_FIELD_NAME),
name: string_key_as_identifier(*RELAY_RESOLVER_MODEL_INSTANCE_FIELD),
type_: TypeAnnotation::NonNull(Box::new(NonNullTypeAnnotation {
span,
type_: TypeAnnotation::Named(NamedTypeAnnotation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use common::NamedItem;
use common::WithLocation;
use docblock_shared::ResolverSourceHash;
use docblock_shared::RELAY_RESOLVER_MODEL_DIRECTIVE_NAME;
use docblock_shared::RELAY_RESOLVER_MODEL_INSTANCE_FIELD;
use docblock_shared::RELAY_RESOLVER_SOURCE_HASH;
use docblock_shared::RELAY_RESOLVER_SOURCE_HASH_VALUE;
use graphql_ir::associated_data_impl;
Expand All @@ -21,19 +22,10 @@ use graphql_ir::Program;
use graphql_ir::ScalarField;
use graphql_ir::Selection;
use intern::string_key::Intern;
use intern::string_key::StringKey;
use lazy_static::lazy_static;
use relay_config::ProjectName;
use relay_config::SchemaConfig;
use schema::Schema;

lazy_static! {
// Using a longer name version for this "special" field
// help us avoid potential collision with product code (__self, __instance can be used for something else)
static ref RESOLVER_MODEL_INSTANCE_FIELD_NAME: StringKey =
"__relay_model_instance".intern();
}

/// Currently, this is a wrapper of the hash of the resolver source code.
/// But we can change this `ArtifactSourceKeyData` to be an
/// enum and also represent the `fragment` or `operation` names.
Expand Down Expand Up @@ -61,19 +53,19 @@ pub fn generate_relay_resolvers_model_fragments(
let object_type = program.schema.get_type(object.name.item.0).unwrap();
let model_instance_field_id = program
.schema
.named_field(object_type, *RESOLVER_MODEL_INSTANCE_FIELD_NAME)
.named_field(object_type, *RELAY_RESOLVER_MODEL_INSTANCE_FIELD)
.unwrap_or_else(|| {
panic!(
"Objects with directive @{} expected to have field `{}`.",
*RELAY_RESOLVER_MODEL_DIRECTIVE_NAME, *RESOLVER_MODEL_INSTANCE_FIELD_NAME
*RELAY_RESOLVER_MODEL_DIRECTIVE_NAME, *RELAY_RESOLVER_MODEL_INSTANCE_FIELD
)
});

let model_fragment_name = FragmentDefinitionName(
project_name
.generate_name_for_object_and_field(
object.name.item.0,
*RESOLVER_MODEL_INSTANCE_FIELD_NAME,
*RELAY_RESOLVER_MODEL_INSTANCE_FIELD,
)
.intern(),
);
Expand Down

0 comments on commit a2c57d9

Please sign in to comment.