Skip to content

Commit

Permalink
Support semantic non-nullable RelayResolverValue
Browse files Browse the repository at this point in the history
Reviewed By: gordyf

Differential Revision: D64009842

fbshipit-source-id: 30417bd3323a30d27a9d959e59123262cebde24c
  • Loading branch information
captbaritone authored and facebook-github-bot committed Oct 8, 2024
1 parent bb6162e commit b41ae4c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
22 changes: 9 additions & 13 deletions compiler/crates/relay-schema-generation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ fn return_type_to_type_annotation(
type_definitions: &FxHashMap<ModuleResolutionKey, DocblockIr>,
use_semantic_non_null: bool,
) -> DiagnosticsResult<(TypeAnnotation, Vec<i64>)> {
let (return_type, mut is_optional) = schema_extractor::unwrap_nullable_type(return_type);
let (return_type, is_optional) = schema_extractor::unwrap_nullable_type(return_type);
let mut semantic_non_null_levels: Vec<i64> = vec![];

let location = to_location(source_location, return_type);
Expand Down Expand Up @@ -1029,20 +1029,16 @@ fn return_type_to_type_annotation(
)]);
}
}
"RelayResolverValue" => {
// Special case for `RelayResolverValue`, it is always optional
is_optional = true;
TypeAnnotation::Named(NamedTypeAnnotation {
name: Identifier {
"RelayResolverValue" => TypeAnnotation::Named(NamedTypeAnnotation {
name: Identifier {
span: location.span(),
token: Token {
span: location.span(),
token: Token {
span: location.span(),
kind: TokenKind::Identifier,
},
value: intern!("RelayResolverValue"),
kind: TokenKind::Identifier,
},
})
}
value: intern!("RelayResolverValue"),
},
}),
_ => {
return Err(vec![Diagnostic::error(
SchemaGenerationError::UnSupportedGeneric {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,24 @@ Field(
},
root_fragment: None,
deprecated: None,
semantic_non_null: None,
semantic_non_null: Some(
ConstantDirective {
span: 419:431,
at: Token {
span: 0:0,
kind: Empty,
},
name: Identifier {
span: 419:431,
token: Token {
span: 0:0,
kind: Empty,
},
value: "semanticNonNull",
},
arguments: None,
},
),
live: None,
location: module.js:419:431,
fragment_arguments: None,
Expand All @@ -88,7 +105,7 @@ Field(
),
)
extend type Cat {
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")
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
}


Expand Down
17 changes: 14 additions & 3 deletions packages/relay-runtime/experimental.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,20 @@ export type IdOf<A: string, Typename: void | string = void> = [
? {id: DataID}
: {id: DataID, __typename: Typename};

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

type ErrorResult<Error> = {
ok: false,
Expand Down

0 comments on commit b41ae4c

Please sign in to comment.