Skip to content

Commit 24951b9

Browse files
evanyeungfacebook-github-bot
authored andcommitted
Pipe information through compiler for simplified resolver generation for property lookup resolvers
Summary: This diff creates the foundation of a new quality of life feature that allows for quickly defining resolvers that just do a property lookup on the base model. The changes in this diff are just piping the information of whether or not the resolver is a regular function or a new property lookup resolver through the compiler. It starts in the schema generation module and must carry the information all the way through to the printer. A stacked diff will actually implement the logic to find and add the resolver information. Reviewed By: captbaritone Differential Revision: D66714731 fbshipit-source-id: 4eba405cc590f461339f3f165d9832b52854c75e
1 parent 249e4f3 commit 24951b9

File tree

85 files changed

+311
-71
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+311
-71
lines changed

compiler/crates/docblock-shared/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ lazy_static! {
4646
/// has validated that its Flow/TypeScript type matches the GraphQL type.
4747
pub static ref TYPE_CONFIRMED_ARGUMENT_NAME: ArgumentName =
4848
ArgumentName("type_confirmed".intern());
49+
/// Indicates that the resolver is just a property lookup on the underlying model (and we need to generate
50+
/// code to do this lookup)
51+
pub static ref RESOLVER_IS_PROPERTY_LOOKUP: ArgumentName =
52+
ArgumentName("is_property_lookup".intern());
4953
/// "Weak" resolver types are types which are backed by a JS model value, but which don't have a stable
5054
/// identity. Types in the generated schema are annotated with a directive using this name to signal
5155
/// to the rest of Relay that they are backed by a "weak" Relay Resolver model.

compiler/crates/relay-codegen/src/ast.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ pub enum GraphQLModuleDependency {
9797
},
9898
}
9999

100+
#[derive(Eq, PartialEq, Hash, Debug)]
101+
pub enum ResolverJSFunction {
102+
Module(JSModuleDependency),
103+
PropertyLookup(String),
104+
}
105+
100106
#[derive(Eq, PartialEq, Hash, Debug)]
101107
pub enum Primitive {
102108
Key(AstKey),
@@ -122,7 +128,7 @@ pub enum Primitive {
122128
RelayResolverModel {
123129
graphql_module_name: StringKey,
124130
graphql_module_path: StringKey,
125-
js_module: JSModuleDependency,
131+
resolver_fn: ResolverJSFunction,
126132
injected_field_name_details: Option<(StringKey, bool)>,
127133
},
128134
}

compiler/crates/relay-codegen/src/build_ast.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use relay_transforms::get_resolver_fragment_dependency_name;
5252
use relay_transforms::relay_resolvers::get_resolver_info;
5353
use relay_transforms::relay_resolvers::resolver_import_alias;
5454
use relay_transforms::relay_resolvers::ResolverInfo;
55+
use relay_transforms::relay_resolvers::ResolverSchemaGenType;
5556
use relay_transforms::remove_directive;
5657
use relay_transforms::CatchMetadataDirective;
5758
use relay_transforms::ClientEdgeMetadata;
@@ -94,6 +95,7 @@ use crate::ast::ObjectEntry;
9495
use crate::ast::Primitive;
9596
use crate::ast::QueryID;
9697
use crate::ast::RequestParameters;
98+
use crate::ast::ResolverJSFunction;
9799
use crate::ast::ResolverModuleReference;
98100
use crate::constants::CODEGEN_CONSTANTS;
99101
use crate::object;
@@ -1449,6 +1451,7 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
14491451
},
14501452
)),
14511453
type_confirmed: relay_resolver_metadata.type_confirmed,
1454+
resolver_type: ResolverSchemaGenType::ResolverModule,
14521455
};
14531456
let fragment_primitive = Primitive::Key(self.object(object! {
14541457
args: Primitive::SkippableNull,
@@ -1534,10 +1537,19 @@ impl<'schema, 'builder, 'config> CodegenBuilder<'schema, 'builder, 'config> {
15341537
),
15351538
);
15361539

1540+
let resolver_fn = match relay_resolver_metadata.resolver_type {
1541+
ResolverSchemaGenType::ResolverModule => {
1542+
ResolverJSFunction::Module(resolver_js_module)
1543+
}
1544+
ResolverSchemaGenType::PropertyLookup => {
1545+
ResolverJSFunction::PropertyLookup(field.name.item.to_string())
1546+
}
1547+
};
1548+
15371549
Primitive::RelayResolverModel {
15381550
graphql_module_name: fragment_name.item.0,
15391551
graphql_module_path: fragment_import_path,
1540-
js_module: resolver_js_module,
1552+
resolver_fn,
15411553
injected_field_name_details: match injection_mode {
15421554
FragmentDataInjectionMode::Field { name, is_required } => {
15431555
Some((name, is_required))

compiler/crates/relay-codegen/src/printer.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::ast::ObjectEntry;
3434
use crate::ast::Primitive;
3535
use crate::ast::QueryID;
3636
use crate::ast::RequestParameters;
37+
use crate::ast::ResolverJSFunction;
3738
use crate::ast::ResolverModuleReference;
3839
use crate::build_ast::build_fragment;
3940
use crate::build_ast::build_operation;
@@ -588,13 +589,13 @@ impl<'b> JSONPrinter<'b> {
588589
Primitive::RelayResolverModel {
589590
graphql_module_path,
590591
graphql_module_name,
591-
js_module,
592+
resolver_fn,
592593
injected_field_name_details,
593594
} => self.write_relay_resolver_model(
594595
f,
595596
*graphql_module_name,
596597
*graphql_module_path,
597-
js_module,
598+
resolver_fn,
598599
injected_field_name_details.as_ref().copied(),
599600
),
600601
}
@@ -661,7 +662,7 @@ impl<'b> JSONPrinter<'b> {
661662
f: &mut String,
662663
graphql_module_name: StringKey,
663664
graphql_module_path: StringKey,
664-
js_module: &JSModuleDependency,
665+
resolver_fn: &ResolverJSFunction,
665666
injected_field_name_details: Option<(StringKey, bool)>,
666667
) -> FmtResult {
667668
let relay_runtime_experimental = "relay-runtime/experimental";
@@ -685,11 +686,14 @@ impl<'b> JSONPrinter<'b> {
685686
)),
686687
)?;
687688
write!(f, ", ")?;
688-
self.write_js_dependency(
689-
f,
690-
js_module.import_name.clone(),
691-
get_module_path(self.js_module_format, js_module.path),
692-
)?;
689+
match resolver_fn {
690+
ResolverJSFunction::Module(js_module) => self.write_js_dependency(
691+
f,
692+
js_module.import_name.clone(),
693+
get_module_path(self.js_module_format, js_module.path),
694+
)?,
695+
ResolverJSFunction::PropertyLookup(property) => write!(f, "(o) => o.{}", property)?,
696+
}
693697
if let Some((field_name, is_required_field)) = injected_field_name_details {
694698
write!(f, ", '{}'", field_name)?;
695699
write!(f, ", {}", is_required_field)?;

compiler/crates/relay-docblock/src/docblock_ir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ fn parse_terse_relay_resolver_ir(
404404
fragment_arguments,
405405
source_hash,
406406
type_confirmed: false,
407+
is_property_lookup: false,
407408
})
408409
}
409410

compiler/crates/relay-docblock/src/ir.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use docblock_shared::RELAY_RESOLVER_MODEL_INSTANCE_FIELD;
3232
use docblock_shared::RELAY_RESOLVER_SOURCE_HASH;
3333
use docblock_shared::RELAY_RESOLVER_SOURCE_HASH_VALUE;
3434
use docblock_shared::RELAY_RESOLVER_WEAK_OBJECT_DIRECTIVE;
35+
use docblock_shared::RESOLVER_IS_PROPERTY_LOOKUP;
3536
use docblock_shared::RESOLVER_VALUE_SCALAR_NAME;
3637
use docblock_shared::TYPE_CONFIRMED_ARGUMENT_NAME;
3738
use graphql_ir::FragmentDefinitionName;
@@ -373,6 +374,7 @@ trait ResolverIr: Sized {
373374
fn source_hash(&self) -> ResolverSourceHash;
374375
fn semantic_non_null(&self) -> Option<ConstantDirective>;
375376
fn type_confirmed(&self) -> bool;
377+
fn is_property_lookup(&self) -> bool;
376378

377379
fn to_graphql_schema_ast(
378380
self,
@@ -514,7 +516,12 @@ trait ResolverIr: Sized {
514516
}
515517
}
516518
}
517-
519+
if self.is_property_lookup() {
520+
arguments.push(true_argument(
521+
RESOLVER_IS_PROPERTY_LOOKUP.0,
522+
Location::generated(),
523+
));
524+
}
518525
let schema = project_config.schema;
519526

520527
if let Some(output_type) = self.output_type() {
@@ -821,6 +828,7 @@ pub struct TerseRelayResolverIr {
821828
/// Indicates that the extraction method used has already validated that the
822829
/// implementaiton matches the GraphQL types.
823830
pub type_confirmed: bool,
831+
pub is_property_lookup: bool,
824832
}
825833

826834
impl ResolverIr for TerseRelayResolverIr {
@@ -926,6 +934,10 @@ impl ResolverIr for TerseRelayResolverIr {
926934
fn type_confirmed(&self) -> bool {
927935
self.type_confirmed
928936
}
937+
938+
fn is_property_lookup(&self) -> bool {
939+
self.is_property_lookup
940+
}
929941
}
930942

931943
impl ResolverTypeDefinitionIr for TerseRelayResolverIr {
@@ -1118,6 +1130,10 @@ impl ResolverIr for LegacyVerboseResolverIr {
11181130
fn type_confirmed(&self) -> bool {
11191131
false
11201132
}
1133+
1134+
fn is_property_lookup(&self) -> bool {
1135+
false
1136+
}
11211137
}
11221138

11231139
impl ResolverTypeDefinitionIr for LegacyVerboseResolverIr {
@@ -1267,6 +1283,10 @@ impl ResolverIr for StrongObjectIr {
12671283
fn type_confirmed(&self) -> bool {
12681284
self.type_confirmed
12691285
}
1286+
1287+
fn is_property_lookup(&self) -> bool {
1288+
false
1289+
}
12701290
}
12711291

12721292
/// Relay Resolver docblock representing a "model" type for a weak object
@@ -1456,6 +1476,10 @@ impl ResolverIr for WeakObjectIr {
14561476
fn type_confirmed(&self) -> bool {
14571477
self.type_confirmed
14581478
}
1479+
1480+
fn is_property_lookup(&self) -> bool {
1481+
false
1482+
}
14591483
}
14601484

14611485
fn string_argument(name: StringKey, value: WithLocation<StringKey>) -> ConstantArgument {

compiler/crates/relay-docblock/tests/parse/fixtures/terse-relay-resolver-args.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ Field(
128128
"ff0b47b51f0011ae9def59af3e3792a3",
129129
),
130130
type_confirmed: false,
131+
is_property_lookup: false,
131132
},
132133
),
133134
)

compiler/crates/relay-docblock/tests/parse/fixtures/terse-relay-resolver-disallow-non-nullable-list-item.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Field(
8585
"12e28b8739ff3ab018186a28de3ca726",
8686
),
8787
type_confirmed: false,
88+
is_property_lookup: false,
8889
},
8990
),
9091
)

compiler/crates/relay-docblock/tests/parse/fixtures/terse-relay-resolver-non-nullable.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Field(
7474
"ac789e28bceef3eeaab77ae5203f43a6",
7575
),
7676
type_confirmed: false,
77+
is_property_lookup: false,
7778
},
7879
),
7980
)

compiler/crates/relay-docblock/tests/parse/fixtures/terse-relay-resolver-semantic-non-null.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ Field(
114114
"ba2a3b6d7c4294fef33f921df3b20065",
115115
),
116116
type_confirmed: false,
117+
is_property_lookup: false,
117118
},
118119
),
119120
)

0 commit comments

Comments
 (0)