Skip to content

Commit b41ae4c

Browse files
captbaritonefacebook-github-bot
authored andcommitted
Support semantic non-nullable RelayResolverValue
Reviewed By: gordyf Differential Revision: D64009842 fbshipit-source-id: 30417bd3323a30d27a9d959e59123262cebde24c
1 parent bb6162e commit b41ae4c

File tree

3 files changed

+42
-18
lines changed

3 files changed

+42
-18
lines changed

compiler/crates/relay-schema-generation/src/lib.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ fn return_type_to_type_annotation(
914914
type_definitions: &FxHashMap<ModuleResolutionKey, DocblockIr>,
915915
use_semantic_non_null: bool,
916916
) -> DiagnosticsResult<(TypeAnnotation, Vec<i64>)> {
917-
let (return_type, mut is_optional) = schema_extractor::unwrap_nullable_type(return_type);
917+
let (return_type, is_optional) = schema_extractor::unwrap_nullable_type(return_type);
918918
let mut semantic_non_null_levels: Vec<i64> = vec![];
919919

920920
let location = to_location(source_location, return_type);
@@ -1029,20 +1029,16 @@ fn return_type_to_type_annotation(
10291029
)]);
10301030
}
10311031
}
1032-
"RelayResolverValue" => {
1033-
// Special case for `RelayResolverValue`, it is always optional
1034-
is_optional = true;
1035-
TypeAnnotation::Named(NamedTypeAnnotation {
1036-
name: Identifier {
1032+
"RelayResolverValue" => TypeAnnotation::Named(NamedTypeAnnotation {
1033+
name: Identifier {
1034+
span: location.span(),
1035+
token: Token {
10371036
span: location.span(),
1038-
token: Token {
1039-
span: location.span(),
1040-
kind: TokenKind::Identifier,
1041-
},
1042-
value: intern!("RelayResolverValue"),
1037+
kind: TokenKind::Identifier,
10431038
},
1044-
})
1045-
}
1039+
value: intern!("RelayResolverValue"),
1040+
},
1041+
}),
10461042
_ => {
10471043
return Err(vec![Diagnostic::error(
10481044
SchemaGenerationError::UnSupportedGeneric {

compiler/crates/relay-schema-generation/tests/docblock/fixtures/return-relay-resolver-value.expected

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,24 @@ Field(
7676
},
7777
root_fragment: None,
7878
deprecated: None,
79-
semantic_non_null: None,
79+
semantic_non_null: Some(
80+
ConstantDirective {
81+
span: 419:431,
82+
at: Token {
83+
span: 0:0,
84+
kind: Empty,
85+
},
86+
name: Identifier {
87+
span: 419:431,
88+
token: Token {
89+
span: 0:0,
90+
kind: Empty,
91+
},
92+
value: "semanticNonNull",
93+
},
94+
arguments: None,
95+
},
96+
),
8097
live: None,
8198
location: module.js:419:431,
8299
fragment_arguments: None,
@@ -88,7 +105,7 @@ Field(
88105
),
89106
)
90107
extend type Cat {
91-
complexValue: RelayResolverValue @relay_resolver(fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", type_confirmed: true, has_output_type: true, import_name: "complexValue", import_path: "module.js") @resolver_source_hash(value: "fc15c065174264428a3632fe9cf329d6")
108+
complexValue: RelayResolverValue @relay_resolver(fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", type_confirmed: true, has_output_type: true, import_name: "complexValue", import_path: "module.js") @resolver_source_hash(value: "fc15c065174264428a3632fe9cf329d6") @semanticNonNull
92109
}
93110

94111

packages/relay-runtime/experimental.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,20 @@ export type IdOf<A: string, Typename: void | string = void> = [
2727
? {id: DataID}
2828
: {id: DataID, __typename: Typename};
2929

30-
// Annotates a `RelayResolverValue` GraphQL return type
31-
// eslint-disable-next-line no-unused-vars
32-
export type RelayResolverValue<A> = A;
30+
/**
31+
* Annotates a `RelayResolverValue` GraphQL return type. Using this type in the
32+
* return position of a Relay Resolver informs Relay that it should model this
33+
* field as returning a `RelayResolverValue` type. See the docs for more
34+
* information:
35+
*
36+
* https://relay.dev/docs/next/guides/relay-resolvers/return-types/#javascript-values
37+
*
38+
* Note: This type forces the value to be non-maybe. This is required in order
39+
* to allow the Relay compiler to to "see", via static analysis, if the field
40+
* can return null or not. If the field is nullable, you can type it as
41+
* returning `?RelayResolverValue<T>`.
42+
*/
43+
export type RelayResolverValue<A> = $NonMaybeType<A>;
3344

3445
type ErrorResult<Error> = {
3546
ok: false,

0 commit comments

Comments
 (0)