From 505206292a327e444749f06a541f952c6de57812 Mon Sep 17 00:00:00 2001 From: Tianyu Yao Date: Tue, 14 May 2024 18:35:04 -0700 Subject: [PATCH] Support boolean Reviewed By: captbaritone Differential Revision: D57081977 fbshipit-source-id: 39d93f228adeda6a96d7fb0fd71747fa43ae6354 --- .../crates/relay-schema-generation/src/lib.rs | 27 +- .../fixtures/primitive-types.expected | 286 ++++++++++++++++++ .../docblock/fixtures/primitive-types.input | 47 +++ .../tests/docblock_test.rs | 9 +- 4 files changed, 361 insertions(+), 8 deletions(-) create mode 100644 compiler/crates/relay-schema-generation/tests/docblock/fixtures/primitive-types.expected create mode 100644 compiler/crates/relay-schema-generation/tests/docblock/fixtures/primitive-types.input diff --git a/compiler/crates/relay-schema-generation/src/lib.rs b/compiler/crates/relay-schema-generation/src/lib.rs index df8d1412f36e5..cde38b7397c30 100644 --- a/compiler/crates/relay-schema-generation/src/lib.rs +++ b/compiler/crates/relay-schema-generation/src/lib.rs @@ -602,12 +602,7 @@ impl RelayResolverExtractor { (flow_return_type, None) } } - FlowTypeAnnotation::StringTypeAnnotation(_) => (flow_return_type, None), - FlowTypeAnnotation::NumberTypeAnnotation(_) => (flow_return_type, None), - _ => { - // Only support named types in function returns - return self.error_result(SchemaGenerationError::UnsupportedType, flow_return_type); - } + _ => (flow_return_type, None), }; if node.params.is_empty() { @@ -793,9 +788,27 @@ fn return_type_to_type_annotation( name: string_key_to_identifier(identifier), }) } + FlowTypeAnnotation::BooleanTypeAnnotation(node) => { + let identifier = WithLocation { + item: intern!("Boolean"), + location: to_location(source_location, node.as_ref()), + }; + TypeAnnotation::Named(NamedTypeAnnotation { + name: string_key_to_identifier(identifier), + }) + } + FlowTypeAnnotation::BooleanLiteralTypeAnnotation(node) => { + let identifier = WithLocation { + item: intern!("Boolean"), + location: to_location(source_location, node.as_ref()), + }; + TypeAnnotation::Named(NamedTypeAnnotation { + name: string_key_to_identifier(identifier), + }) + } _ => { return Err(vec![Diagnostic::error( - SchemaGenerationError::TODO, + SchemaGenerationError::UnsupportedType, location, )]); } diff --git a/compiler/crates/relay-schema-generation/tests/docblock/fixtures/primitive-types.expected b/compiler/crates/relay-schema-generation/tests/docblock/fixtures/primitive-types.expected new file mode 100644 index 0000000000000..ab5f1355a15d6 --- /dev/null +++ b/compiler/crates/relay-schema-generation/tests/docblock/fixtures/primitive-types.expected @@ -0,0 +1,286 @@ +==================================== INPUT ==================================== +/** + * 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. + */ + +//- module.js + +import type CatFlowType from 'Cat'; + +/** + * @RelayResolver + */ +export function Cat(id: DataID): CatFlowType { + return {}; +} + + +/** + * @RelayResolver + */ +export function ages(cat: CatFlowType): ?number { + return 2; +} + + +/** + * @RelayResolver + */ +export function hungry1(cat: CatFlowType): ?boolean { + return false; +} + +/** + * @RelayResolver + */ +export function hungry2(cat: CatFlowType): ?false { + return false; +} + +/** + * @RelayResolver + */ +export function hungry3(cat: CatFlowType): true { + return false; +} +==================================== OUTPUT =================================== +StrongObjectResolver( + StrongObjectIr { + type_name: Identifier { + span: 272:275, + token: Token { + span: 272:275, + kind: Identifier, + }, + value: "Cat", + }, + rhs_location: module.js:272:275, + root_fragment: WithLocation { + location: module.js:272:275, + item: FragmentDefinitionName( + "Cat__id", + ), + }, + description: None, + deprecated: None, + live: None, + semantic_non_null: None, + location: module.js:272:275, + implements_interfaces: [], + source_hash: ResolverSourceHash( + "5e42d8055bf96703110b4485122e0e0f", + ), + }, +) +type Cat @__RelayResolverModel { + id: ID! + __relay_model_instance: RelayResolverValue! @relay_resolver(import_path: "module.js", fragment_name: "Cat__id", generated_fragment: true, inject_fragment_data: "id", import_name: "Cat") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f") @unselectable(reason: "This field is intended only for Relay's internal use") +} + + +TerseRelayResolver( + TerseRelayResolverIr { + field: FieldDefinition { + name: Identifier { + span: 362:366, + token: Token { + span: 362:366, + kind: Identifier, + }, + value: "ages", + }, + type_: Named( + NamedTypeAnnotation { + name: Identifier { + span: 387:393, + token: Token { + span: 387:393, + kind: Identifier, + }, + value: "Float", + }, + }, + ), + arguments: None, + directives: [], + description: None, + hack_source: None, + span: 362:366, + }, + type_: WithLocation { + location: :272:275, + item: "Cat", + }, + root_fragment: None, + deprecated: None, + semantic_non_null: None, + live: None, + location: module.js:362:366, + fragment_arguments: None, + source_hash: ResolverSourceHash( + "5e42d8055bf96703110b4485122e0e0f", + ), + }, +) +extend type Cat { + ages: Float @relay_resolver(import_path: "module.js", fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", has_output_type: true, import_name: "ages") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f") +} + + +TerseRelayResolver( + TerseRelayResolverIr { + field: FieldDefinition { + name: Identifier { + span: 454:461, + token: Token { + span: 454:461, + kind: Identifier, + }, + value: "hungry1", + }, + type_: Named( + NamedTypeAnnotation { + name: Identifier { + span: 482:489, + token: Token { + span: 482:489, + kind: Identifier, + }, + value: "Boolean", + }, + }, + ), + arguments: None, + directives: [], + description: None, + hack_source: None, + span: 454:461, + }, + type_: WithLocation { + location: :272:275, + item: "Cat", + }, + root_fragment: None, + deprecated: None, + semantic_non_null: None, + live: None, + location: module.js:454:461, + fragment_arguments: None, + source_hash: ResolverSourceHash( + "5e42d8055bf96703110b4485122e0e0f", + ), + }, +) +extend type Cat { + hungry1: Boolean @relay_resolver(import_path: "module.js", fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", has_output_type: true, import_name: "hungry1") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f") +} + + +TerseRelayResolver( + TerseRelayResolverIr { + field: FieldDefinition { + name: Identifier { + span: 553:560, + token: Token { + span: 553:560, + kind: Identifier, + }, + value: "hungry2", + }, + type_: Named( + NamedTypeAnnotation { + name: Identifier { + span: 581:586, + token: Token { + span: 581:586, + kind: Identifier, + }, + value: "Boolean", + }, + }, + ), + arguments: None, + directives: [], + description: None, + hack_source: None, + span: 553:560, + }, + type_: WithLocation { + location: :272:275, + item: "Cat", + }, + root_fragment: None, + deprecated: None, + semantic_non_null: None, + live: None, + location: module.js:553:560, + fragment_arguments: None, + source_hash: ResolverSourceHash( + "5e42d8055bf96703110b4485122e0e0f", + ), + }, +) +extend type Cat { + hungry2: Boolean @relay_resolver(import_path: "module.js", fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", has_output_type: true, import_name: "hungry2") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f") +} + + +TerseRelayResolver( + TerseRelayResolverIr { + field: FieldDefinition { + name: Identifier { + span: 650:657, + token: Token { + span: 650:657, + kind: Identifier, + }, + value: "hungry3", + }, + type_: NonNull( + NonNullTypeAnnotation { + span: 677:681, + type_: Named( + NamedTypeAnnotation { + name: Identifier { + span: 677:681, + token: Token { + span: 677:681, + kind: Identifier, + }, + value: "Boolean", + }, + }, + ), + exclamation: Token { + span: 0:0, + kind: Empty, + }, + }, + ), + arguments: None, + directives: [], + description: None, + hack_source: None, + span: 650:657, + }, + type_: WithLocation { + location: :272:275, + item: "Cat", + }, + root_fragment: None, + deprecated: None, + semantic_non_null: None, + live: None, + location: module.js:650:657, + fragment_arguments: None, + source_hash: ResolverSourceHash( + "5e42d8055bf96703110b4485122e0e0f", + ), + }, +) +extend type Cat { + hungry3: Boolean! @relay_resolver(import_path: "module.js", fragment_name: "Cat____relay_model_instance", generated_fragment: true, inject_fragment_data: "__relay_model_instance", has_output_type: true, import_name: "hungry3") @resolver_source_hash(value: "5e42d8055bf96703110b4485122e0e0f") +} diff --git a/compiler/crates/relay-schema-generation/tests/docblock/fixtures/primitive-types.input b/compiler/crates/relay-schema-generation/tests/docblock/fixtures/primitive-types.input new file mode 100644 index 0000000000000..cef6503712012 --- /dev/null +++ b/compiler/crates/relay-schema-generation/tests/docblock/fixtures/primitive-types.input @@ -0,0 +1,47 @@ +/** + * 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. + */ + +//- module.js + +import type CatFlowType from 'Cat'; + +/** + * @RelayResolver + */ +export function Cat(id: DataID): CatFlowType { + return {}; +} + + +/** + * @RelayResolver + */ +export function ages(cat: CatFlowType): ?number { + return 2; +} + + +/** + * @RelayResolver + */ +export function hungry1(cat: CatFlowType): ?boolean { + return false; +} + +/** + * @RelayResolver + */ +export function hungry2(cat: CatFlowType): ?false { + return false; +} + +/** + * @RelayResolver + */ +export function hungry3(cat: CatFlowType): true { + return false; +} diff --git a/compiler/crates/relay-schema-generation/tests/docblock_test.rs b/compiler/crates/relay-schema-generation/tests/docblock_test.rs index 0e69acf1e5cc2..f051cccb74ef0 100644 --- a/compiler/crates/relay-schema-generation/tests/docblock_test.rs +++ b/compiler/crates/relay-schema-generation/tests/docblock_test.rs @@ -4,7 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * - * @generated SignedSource<> + * @generated SignedSource<> */ mod docblock; @@ -61,6 +61,13 @@ async fn plural_optional() { test_fixture(transform_fixture, file!(), "plural-optional.input", "docblock/fixtures/plural-optional.expected", input, expected).await; } +#[tokio::test] +async fn primitive_types() { + let input = include_str!("docblock/fixtures/primitive-types.input"); + let expected = include_str!("docblock/fixtures/primitive-types.expected"); + test_fixture(transform_fixture, file!(), "primitive-types.input", "docblock/fixtures/primitive-types.expected", input, expected).await; +} + #[tokio::test] async fn root_fragment() { let input = include_str!("docblock/fixtures/root-fragment.input");