From 598b217c6e3b82a2f4a4e8518c11b26532641423 Mon Sep 17 00:00:00 2001 From: Mark Polak Date: Sun, 11 Aug 2024 09:13:19 +0200 Subject: [PATCH] Add option to replace ScalarType with custom generic type in typegen transform --- compiler/crates/relay-transforms/src/lib.rs | 2 + .../src/typegen_type_override_directive.rs | 201 ++++++++++++++++++ compiler/crates/relay-typegen/src/visit.rs | 45 +++- ...stom_scalar_type_boolean_argument.expected | 39 ++++ ...ustom_scalar_type_boolean_argument.graphql | 25 +++ ...om_scalar_type_multiple_arguments.expected | 42 ++++ ...tom_scalar_type_multiple_arguments.graphql | 28 +++ ...lar_type_multiple_arguments_union.expected | 42 ++++ ...alar_type_multiple_arguments_union.graphql | 28 +++ ...y_custom_scalar_type_no_arguments.expected | 36 ++++ ...ay_custom_scalar_type_no_arguments.graphql | 22 ++ ...stom_scalar_type_number_arguments.expected | 47 ++++ ...ustom_scalar_type_number_arguments.graphql | 33 +++ ...ustom_scalar_type_single_argument.expected | 40 ++++ ...custom_scalar_type_single_argument.graphql | 26 +++ .../relay-typegen/tests/generate_flow_test.rs | 44 +++- ...stom_scalar_type_boolean_argument.expected | 37 ++++ ...ustom_scalar_type_boolean_argument.graphql | 25 +++ ...om_scalar_type_multiple_arguments.expected | 40 ++++ ...tom_scalar_type_multiple_arguments.graphql | 28 +++ ...lar_type_multiple_arguments_union.expected | 40 ++++ ...alar_type_multiple_arguments_union.graphql | 28 +++ ...y_custom_scalar_type_no_arguments.expected | 34 +++ ...ay_custom_scalar_type_no_arguments.graphql | 22 ++ ...stom_scalar_type_number_arguments.expected | 45 ++++ ...ustom_scalar_type_number_arguments.graphql | 33 +++ ...ustom_scalar_type_single_argument.expected | 38 ++++ ...custom_scalar_type_single_argument.graphql | 26 +++ .../tests/generate_typescript_test.rs | 44 +++- 29 files changed, 1137 insertions(+), 3 deletions(-) create mode 100644 compiler/crates/relay-transforms/src/typegen_type_override_directive.rs create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_no_arguments.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_no_arguments.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_number_arguments.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_number_arguments.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_single_argument.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_single_argument.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.graphql create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_single_argument.expected create mode 100644 compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_single_argument.graphql diff --git a/compiler/crates/relay-transforms/src/lib.rs b/compiler/crates/relay-transforms/src/lib.rs index d02b3c20215d2..c9d3b89954d1c 100644 --- a/compiler/crates/relay-transforms/src/lib.rs +++ b/compiler/crates/relay-transforms/src/lib.rs @@ -64,6 +64,7 @@ mod skip_updatable_queries; mod sort_selections; mod test_operation_metadata; mod transform_connections; +mod typegen_type_override_directive; mod unwrap_custom_directive_selection; mod util; mod validate_operation_variables; @@ -203,6 +204,7 @@ pub use skip_unreachable_node::skip_unreachable_node_strict; pub use sort_selections::sort_selections; pub use test_operation_metadata::generate_test_operation_metadata; pub use transform_connections::transform_connections; +pub use typegen_type_override_directive::TypgenTypeOverride; pub use unwrap_custom_directive_selection::unwrap_custom_directive_selection; pub use util::extract_variable_name; pub use util::generate_abstract_type_refinement_key; diff --git a/compiler/crates/relay-transforms/src/typegen_type_override_directive.rs b/compiler/crates/relay-transforms/src/typegen_type_override_directive.rs new file mode 100644 index 0000000000000..b496d5c381273 --- /dev/null +++ b/compiler/crates/relay-transforms/src/typegen_type_override_directive.rs @@ -0,0 +1,201 @@ +use std::path::PathBuf; + +use ::intern::string_key::Intern; +use ::intern::string_key::StringKey; +use common::ArgumentName; +use common::DirectiveName; +use common::NamedItem; +use common::WithLocation; +use graphql_ir::Argument; +use graphql_ir::ConstantValue; +use graphql_ir::Directive; +use graphql_ir::ScalarField; +use graphql_ir::Value; +use intern::intern; +use itertools::Itertools; +use lazy_static::lazy_static; + +lazy_static! { + static ref INTERNAL_TYPE_OVERRIDE_GENERIC_ARGUMENTS: StringKey = intern!("generic_arguments"); + static ref INTERNAL_TYPE_OVERRIDE_NAME: StringKey = intern!("name"); + static ref INTERNAL_TYPE_OVERRIDE_PATH: StringKey = intern!("path"); + static ref INTERNAL_TYPE_OVERRIDE_IS_UNION: StringKey = intern!("union"); +} + +/// The `@__RelayTypegenTypeOverride` directive allows you to specifiy a custom type +/// that should be used in the generated code instead of the default type. +/// +/// The directive is to be used with the `custom_transforms_config`. +/// Specifically the `custom_transforms.typegen_transforms` +/// +/// The directive has the following arguments: +/// - `name`: The name of the type as it should be imported in the generated code. +/// - `path`: The path to the file that exports the type. +/// - `generic_arguments`: The generic arguments of the type. +/// +/// Inside the a transform you can add this directive to a scalar using `transform_scalar_field`. +/// +/// Example: +/// ``` +/// use std::path::PathBuf; +/// use std::sync::Arc; +/// +/// use graphql_ir::ScalarField; +/// use graphql_ir::Selection; +/// use graphql_ir::Transformed; +/// use graphql_ir::Transformer; +/// use intern::string_key::Intern; +/// use relay_transforms::TypgenTypeOverride; +/// +/// struct MyCustomTransformer; +/// impl Transformer for MyCustomTransformer { +/// const NAME: &'static str = "MyCustomTransformer"; +/// const VISIT_ARGUMENTS: bool = false; +/// const VISIT_DIRECTIVES: bool = false; +/// +/// fn transform_scalar_field(&mut self, field: &ScalarField) -> Transformed { +/// let arguments = self.transform_arguments(&field.arguments); +/// let mut directives = self +/// .transform_directives(&field.directives) +/// .replace_or_else(|| field.directives.clone()); +/// +/// // Check if the scalar is one that needs your custom type +/// // By checking it's current directives or some other way +/// let typegen_type_override = TypgenTypeOverride { +/// name: "MyCustomScalar".intern(), +/// path: PathBuf::from("path/to/file"), +/// generic_arguments: vec![], +/// is_union: false, +/// }; +/// +/// directives.push(typegen_type_override.to_directive()); +/// +/// Transformed::Replace(Selection::ScalarField(Arc::new(ScalarField { +/// arguments: arguments.replace_or_else(|| field.arguments.clone()), +/// directives, +/// ..field.clone() +/// }))) +/// } +/// } +/// ``` +pub struct TypgenTypeOverride { + pub path: PathBuf, + pub name: StringKey, + pub generic_arguments: Vec, + pub is_union: bool, +} + +impl TypgenTypeOverride { + /// Get the `@__RelayTypegenTypeOverride` directive from the scalar field + /// and creates a `TypgenTypeOverride` from it. + /// Returns None if the directive is not present + pub fn get_override_from_directive(scalar_field: &ScalarField) -> Option { + let override_typegen_type_directive_on_field = + TypgenTypeOverride::get_directive(scalar_field)?; + + Some(TypgenTypeOverride::from( + override_typegen_type_directive_on_field, + )) + } + + fn get_directive(scalar_field: &ScalarField) -> Option<&Directive> { + scalar_field.directives.named(Self::directive_name()) + } + + fn directive_name() -> DirectiveName { + DirectiveName("__RelayTypegenTypeOverride".intern()) + } + + pub fn to_directive(&self) -> Directive { + let mut arguments: Vec = vec![ + Argument { + name: WithLocation::generated(ArgumentName(*INTERNAL_TYPE_OVERRIDE_NAME)), + value: WithLocation::generated(Value::Constant(ConstantValue::String(self.name))), + }, + Argument { + name: WithLocation::generated(ArgumentName(*INTERNAL_TYPE_OVERRIDE_IS_UNION)), + value: WithLocation::generated(Value::Constant(ConstantValue::Boolean( + self.is_union, + ))), + }, + Argument { + name: WithLocation::generated(ArgumentName(*INTERNAL_TYPE_OVERRIDE_PATH)), + value: WithLocation::generated(Value::Constant(ConstantValue::String( + self.path.to_string_lossy().intern(), + ))), + }, + ]; + + if !self.generic_arguments.is_empty() { + arguments.push(Argument { + name: WithLocation::generated(ArgumentName( + *INTERNAL_TYPE_OVERRIDE_GENERIC_ARGUMENTS, + )), + value: WithLocation::generated(Value::Constant(ConstantValue::List( + self.generic_arguments.clone(), + ))), + }); + } + + graphql_ir::Directive { + name: WithLocation::generated(Self::directive_name()), + arguments, + data: None, + } + } +} + +impl From<&Directive> for TypgenTypeOverride { + fn from(directive: &Directive) -> Self { + let directive = directive; + + let name = directive + .arguments + .named(ArgumentName(*INTERNAL_TYPE_OVERRIDE_NAME)) + .and_then(|a| a.value.item.get_string_literal()) + .expect("Expected a name argument for the @__RelayTypegenTypeOverride directive"); + + let path = directive + .arguments + .named(ArgumentName(*INTERNAL_TYPE_OVERRIDE_PATH)) + .and_then(|a| a.value.item.get_string_literal()) + .map(|path| PathBuf::from(path.to_string())) + .expect("Expected a path argument for the @__RelayTypegenTypeOverride directive"); + + let is_union = directive + .arguments + .named(ArgumentName(*INTERNAL_TYPE_OVERRIDE_IS_UNION)) + .and_then(|a| match a.value.item.get_constant() { + Some(ConstantValue::Boolean(b)) => Some(*b), + _ => None, + }) + .unwrap_or(false); + + let generic_arguments = directive + .arguments + .named(ArgumentName(*INTERNAL_TYPE_OVERRIDE_GENERIC_ARGUMENTS)) + .and_then(|a| match &a.value.item { + Value::Constant(ConstantValue::List(values)) => Some( + values + .iter() + .filter_map(|v| match v { + ConstantValue::String(s) => Some(ConstantValue::String(*s)), + ConstantValue::Int(i) => Some(ConstantValue::Int(*i)), + ConstantValue::Float(f) => Some(ConstantValue::Float(*f)), + ConstantValue::Boolean(b) => Some(ConstantValue::Boolean(*b)), + _ => None, + }) + .collect_vec(), + ), + _ => None, + }) + .unwrap_or(vec![]); + + TypgenTypeOverride { + name, + path, + generic_arguments, + is_union, + } + } +} diff --git a/compiler/crates/relay-typegen/src/visit.rs b/compiler/crates/relay-typegen/src/visit.rs index e7fb2f9162846..72da841b9a7c5 100644 --- a/compiler/crates/relay-typegen/src/visit.rs +++ b/compiler/crates/relay-typegen/src/visit.rs @@ -9,6 +9,7 @@ use std::collections::HashSet; use std::hash::Hash; use std::path::PathBuf; use std::sync::Arc; +use std::vec; use ::intern::intern; use ::intern::string_key::Intern; @@ -24,6 +25,7 @@ use docblock_shared::RELAY_RESOLVER_DIRECTIVE_NAME; use docblock_shared::RELAY_RESOLVER_MODEL_INSTANCE_FIELD; use docblock_shared::RESOLVER_VALUE_SCALAR_NAME; use graphql_ir::Condition; +use graphql_ir::ConstantValue; use graphql_ir::Directive; use graphql_ir::FragmentDefinitionName; use graphql_ir::FragmentSpread; @@ -55,6 +57,7 @@ use relay_transforms::RelayResolverMetadata; use relay_transforms::RequiredMetadataDirective; use relay_transforms::ResolverOutputTypeInfo; use relay_transforms::TypeConditionInfo; +use relay_transforms::TypgenTypeOverride; use relay_transforms::ASSIGNABLE_DIRECTIVE_FOR_TYPEGEN; use relay_transforms::CHILDREN_CAN_BUBBLE_METADATA_KEY; use relay_transforms::CLIENT_EXTENSION_DIRECTIVE_NAME; @@ -1202,7 +1205,9 @@ fn visit_scalar_field( } let ast = transform_type_reference_into_ast(&field_type, |type_| { - expect_scalar_type(typegen_context, encountered_enums, custom_scalars, type_) + override_typegen_type_directive(scalar_field, custom_scalars).unwrap_or_else(|| { + expect_scalar_type(typegen_context, encountered_enums, custom_scalars, type_) + }) }); type_selections.push(TypeSelection::ScalarField(TypeSelectionScalarField { @@ -1215,6 +1220,44 @@ fn visit_scalar_field( })); } +fn override_typegen_type_directive( + scalar_field: &ScalarField, + custom_scalars: &mut CustomScalarsImports, +) -> Option { + let TypgenTypeOverride { + name, + path, + generic_arguments, + is_union, + } = TypgenTypeOverride::get_override_from_directive(scalar_field)?; + + custom_scalars.insert((name, path.clone())); + + // Override without arguments + if generic_arguments.is_empty() { + return Some(AST::RawType(name)); + } + + let arguments = generic_arguments + .iter() + .filter_map(|v| match v { + ConstantValue::String(s) => Some(AST::StringLiteral(StringLiteral(*s))), + ConstantValue::Int(i) => Some(AST::RawType(format!("{}", i).intern())), + ConstantValue::Float(f) => Some(AST::RawType(format!("{}", f).intern())), + ConstantValue::Boolean(b) => Some(AST::RawType(format!("{}", b).intern())), + _ => panic!("Unsupported type for typegen override argument. Only String, Int, Float and Boolean are supported."), + }) + .collect(); + + Some(AST::GenericType { + outer: name, + inner: match is_union { + true => vec![AST::Union(SortedASTList::new(arguments))], + false => arguments, + }, + }) +} + #[allow(clippy::too_many_arguments)] fn visit_condition( typegen_context: &'_ TypegenContext<'_>, diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.expected b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.expected new file mode 100644 index 0000000000000..a7d249f823f19 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.expected @@ -0,0 +1,39 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + number @__RelayTypegenTypeOverride( + path: "isEven", + name: "IsEven", + generic_arguments: [ + true + ] + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + number: Float +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Boolean!] +) on FIELD +==================================== OUTPUT =================================== +import type { IsEven } from "isEven"; +import type { FragmentType } from "relay-runtime"; +declare export opaque type foo$fragmentType: FragmentType; +export type foo$data = {| + +number: ?IsEven, + +$fragmentType: foo$fragmentType, +|}; +export type foo$key = { + +$data?: foo$data, + +$fragmentSpreads: foo$fragmentType, + ... +}; diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.graphql b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.graphql new file mode 100644 index 0000000000000..1b907a7fad703 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.graphql @@ -0,0 +1,25 @@ +fragment foo on ViewData { + number @__RelayTypegenTypeOverride( + path: "isEven", + name: "IsEven", + generic_arguments: [ + true + ] + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + number: Float +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Boolean!] +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.expected b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.expected new file mode 100644 index 0000000000000..156f66788b07d --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.expected @@ -0,0 +1,42 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name", + "age" + ], + union: false + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] + union: Boolean +) on FIELD +==================================== OUTPUT =================================== +import type { LocalizedString } from "@i18n/localization"; +import type { FragmentType } from "relay-runtime"; +declare export opaque type foo$fragmentType: FragmentType; +export type foo$data = {| + +usernameLabel: ?LocalizedString<"name", "age">, + +$fragmentType: foo$fragmentType, +|}; +export type foo$key = { + +$data?: foo$data, + +$fragmentSpreads: foo$fragmentType, + ... +}; diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.graphql b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.graphql new file mode 100644 index 0000000000000..21001b6864174 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.graphql @@ -0,0 +1,28 @@ +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name", + "age" + ], + union: false + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] + union: Boolean +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected new file mode 100644 index 0000000000000..a99c5375130c5 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected @@ -0,0 +1,42 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name", + "age" + ], + union: true + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] + union: Boolean +) on FIELD +==================================== OUTPUT =================================== +import type { LocalizedString } from "@i18n/localization"; +import type { FragmentType } from "relay-runtime"; +declare export opaque type foo$fragmentType: FragmentType; +export type foo$data = {| + +usernameLabel: ?LocalizedString<"age" | "name">, + +$fragmentType: foo$fragmentType, +|}; +export type foo$key = { + +$data?: foo$data, + +$fragmentSpreads: foo$fragmentType, + ... +}; diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql new file mode 100644 index 0000000000000..da8a1fa5daa3b --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql @@ -0,0 +1,28 @@ +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name", + "age" + ], + union: true + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] + union: Boolean +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_no_arguments.expected b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_no_arguments.expected new file mode 100644 index 0000000000000..89fad5154bc91 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_no_arguments.expected @@ -0,0 +1,36 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString" + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] +) on FIELD +==================================== OUTPUT =================================== +import type { LocalizedString } from "@i18n/localization"; +import type { FragmentType } from "relay-runtime"; +declare export opaque type foo$fragmentType: FragmentType; +export type foo$data = {| + +usernameLabel: ?LocalizedString, + +$fragmentType: foo$fragmentType, +|}; +export type foo$key = { + +$data?: foo$data, + +$fragmentSpreads: foo$fragmentType, + ... +}; diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_no_arguments.graphql b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_no_arguments.graphql new file mode 100644 index 0000000000000..3e96e1b90b946 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_no_arguments.graphql @@ -0,0 +1,22 @@ +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString" + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_number_arguments.expected b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_number_arguments.expected new file mode 100644 index 0000000000000..e21248db5f45d --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_number_arguments.expected @@ -0,0 +1,47 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + number @__RelayTypegenTypeOverride( + path: "all-the-numbers", + name: "SpecialNumbers", + generic_arguments: [ + 1, + 1.5, + 2, + 3.9, + 42 + ], + union: true + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + number: Float +} + +# Custom scalar as the code transform could put several ConstantValue types here +scalar Argument +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Argument!] + union: Boolean +) on FIELD +==================================== OUTPUT =================================== +import type { SpecialNumbers } from "all-the-numbers"; +import type { FragmentType } from "relay-runtime"; +declare export opaque type foo$fragmentType: FragmentType; +export type foo$data = {| + +number: ?SpecialNumbers<1 | 1.5 | 2 | 3.9 | 42>, + +$fragmentType: foo$fragmentType, +|}; +export type foo$key = { + +$data?: foo$data, + +$fragmentSpreads: foo$fragmentType, + ... +}; diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_number_arguments.graphql b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_number_arguments.graphql new file mode 100644 index 0000000000000..7d2702d49dd19 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_number_arguments.graphql @@ -0,0 +1,33 @@ +fragment foo on ViewData { + number @__RelayTypegenTypeOverride( + path: "all-the-numbers", + name: "SpecialNumbers", + generic_arguments: [ + 1, + 1.5, + 2, + 3.9, + 42 + ], + union: true + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + number: Float +} + +# Custom scalar as the code transform could put several ConstantValue types here +scalar Argument +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Argument!] + union: Boolean +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_single_argument.expected b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_single_argument.expected new file mode 100644 index 0000000000000..c31a90765e9f5 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_single_argument.expected @@ -0,0 +1,40 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name" + ] + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +scalar Argument +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Argument!] +) on FIELD +==================================== OUTPUT =================================== +import type { LocalizedString } from "@i18n/localization"; +import type { FragmentType } from "relay-runtime"; +declare export opaque type foo$fragmentType: FragmentType; +export type foo$data = {| + +usernameLabel: ?LocalizedString<"name">, + +$fragmentType: foo$fragmentType, +|}; +export type foo$key = { + +$data?: foo$data, + +$fragmentSpreads: foo$fragmentType, + ... +}; diff --git a/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_single_argument.graphql b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_single_argument.graphql new file mode 100644 index 0000000000000..690279444bdbe --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_flow/fixtures/relay_custom_scalar_type_single_argument.graphql @@ -0,0 +1,26 @@ +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name" + ] + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +scalar Argument +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Argument!] +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_flow_test.rs b/compiler/crates/relay-typegen/tests/generate_flow_test.rs index 44f1196124641..39e829181e030 100644 --- a/compiler/crates/relay-typegen/tests/generate_flow_test.rs +++ b/compiler/crates/relay-typegen/tests/generate_flow_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<<7c3fafe35c403de1e151adb95c8d3605>> */ mod generate_flow; @@ -383,6 +383,48 @@ async fn relay_client_id_field() { test_fixture(transform_fixture, file!(), "relay-client-id-field.graphql", "generate_flow/fixtures/relay-client-id-field.expected", input, expected).await; } +#[tokio::test] +async fn relay_custom_scalar_type_boolean_argument() { + let input = include_str!("generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.graphql"); + let expected = include_str!("generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_boolean_argument.graphql", "generate_flow/fixtures/relay_custom_scalar_type_boolean_argument.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_multiple_arguments() { + let input = include_str!("generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.graphql"); + let expected = include_str!("generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_multiple_arguments.graphql", "generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_multiple_arguments_union() { + let input = include_str!("generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql"); + let expected = include_str!("generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_multiple_arguments_union.graphql", "generate_flow/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_no_arguments() { + let input = include_str!("generate_flow/fixtures/relay_custom_scalar_type_no_arguments.graphql"); + let expected = include_str!("generate_flow/fixtures/relay_custom_scalar_type_no_arguments.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_no_arguments.graphql", "generate_flow/fixtures/relay_custom_scalar_type_no_arguments.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_number_arguments() { + let input = include_str!("generate_flow/fixtures/relay_custom_scalar_type_number_arguments.graphql"); + let expected = include_str!("generate_flow/fixtures/relay_custom_scalar_type_number_arguments.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_number_arguments.graphql", "generate_flow/fixtures/relay_custom_scalar_type_number_arguments.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_single_argument() { + let input = include_str!("generate_flow/fixtures/relay_custom_scalar_type_single_argument.graphql"); + let expected = include_str!("generate_flow/fixtures/relay_custom_scalar_type_single_argument.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_single_argument.graphql", "generate_flow/fixtures/relay_custom_scalar_type_single_argument.expected", input, expected).await; +} + #[tokio::test] async fn relay_live_resolver() { let input = include_str!("generate_flow/fixtures/relay-live-resolver.graphql"); diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.expected b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.expected new file mode 100644 index 0000000000000..1897347e8edb4 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.expected @@ -0,0 +1,37 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + number @__RelayTypegenTypeOverride( + path: "isEven", + name: "IsEven", + generic_arguments: [ + true + ] + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + number: Float +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Boolean!] +) on FIELD +==================================== OUTPUT =================================== +import { IsEven } from "isEven"; +import { FragmentRefs } from "relay-runtime"; +export type foo$data = { + readonly number: IsEven | null | undefined; + readonly " $fragmentType": "foo"; +}; +export type foo$key = { + readonly " $data"?: foo$data; + readonly " $fragmentSpreads": FragmentRefs<"foo">; +}; diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.graphql b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.graphql new file mode 100644 index 0000000000000..1b907a7fad703 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.graphql @@ -0,0 +1,25 @@ +fragment foo on ViewData { + number @__RelayTypegenTypeOverride( + path: "isEven", + name: "IsEven", + generic_arguments: [ + true + ] + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + number: Float +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Boolean!] +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.expected b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.expected new file mode 100644 index 0000000000000..ac40896464df6 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.expected @@ -0,0 +1,40 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name", + "age" + ], + union: false + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] + union: Boolean +) on FIELD +==================================== OUTPUT =================================== +import { LocalizedString } from "@i18n/localization"; +import { FragmentRefs } from "relay-runtime"; +export type foo$data = { + readonly usernameLabel: LocalizedString<"name", "age"> | null | undefined; + readonly " $fragmentType": "foo"; +}; +export type foo$key = { + readonly " $data"?: foo$data; + readonly " $fragmentSpreads": FragmentRefs<"foo">; +}; diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.graphql b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.graphql new file mode 100644 index 0000000000000..21001b6864174 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.graphql @@ -0,0 +1,28 @@ +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name", + "age" + ], + union: false + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] + union: Boolean +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected new file mode 100644 index 0000000000000..26b4232a4cc68 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected @@ -0,0 +1,40 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name", + "age" + ], + union: true + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] + union: Boolean +) on FIELD +==================================== OUTPUT =================================== +import { LocalizedString } from "@i18n/localization"; +import { FragmentRefs } from "relay-runtime"; +export type foo$data = { + readonly usernameLabel: LocalizedString<"age" | "name"> | null | undefined; + readonly " $fragmentType": "foo"; +}; +export type foo$key = { + readonly " $data"?: foo$data; + readonly " $fragmentSpreads": FragmentRefs<"foo">; +}; diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql new file mode 100644 index 0000000000000..da8a1fa5daa3b --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql @@ -0,0 +1,28 @@ +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name", + "age" + ], + union: true + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] + union: Boolean +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.expected b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.expected new file mode 100644 index 0000000000000..a8466aa2f2410 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.expected @@ -0,0 +1,34 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString" + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] +) on FIELD +==================================== OUTPUT =================================== +import { LocalizedString } from "@i18n/localization"; +import { FragmentRefs } from "relay-runtime"; +export type foo$data = { + readonly usernameLabel: LocalizedString | null | undefined; + readonly " $fragmentType": "foo"; +}; +export type foo$key = { + readonly " $data"?: foo$data; + readonly " $fragmentSpreads": FragmentRefs<"foo">; +}; diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.graphql b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.graphql new file mode 100644 index 0000000000000..3e96e1b90b946 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.graphql @@ -0,0 +1,22 @@ +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString" + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [String!] +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.expected b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.expected new file mode 100644 index 0000000000000..a595ce560f958 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.expected @@ -0,0 +1,45 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + number @__RelayTypegenTypeOverride( + path: "all-the-numbers", + name: "SpecialNumbers", + generic_arguments: [ + 1, + 1.5, + 2, + 3.9, + 42 + ], + union: true + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + number: Float +} + +# Custom scalar as the code transform could put several ConstantValue types here +scalar Argument +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Argument!] + union: Boolean +) on FIELD +==================================== OUTPUT =================================== +import { SpecialNumbers } from "all-the-numbers"; +import { FragmentRefs } from "relay-runtime"; +export type foo$data = { + readonly number: SpecialNumbers<1 | 1.5 | 2 | 3.9 | 42> | null | undefined; + readonly " $fragmentType": "foo"; +}; +export type foo$key = { + readonly " $data"?: foo$data; + readonly " $fragmentSpreads": FragmentRefs<"foo">; +}; diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.graphql b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.graphql new file mode 100644 index 0000000000000..7d2702d49dd19 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.graphql @@ -0,0 +1,33 @@ +fragment foo on ViewData { + number @__RelayTypegenTypeOverride( + path: "all-the-numbers", + name: "SpecialNumbers", + generic_arguments: [ + 1, + 1.5, + 2, + 3.9, + 42 + ], + union: true + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + number: Float +} + +# Custom scalar as the code transform could put several ConstantValue types here +scalar Argument +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Argument!] + union: Boolean +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_single_argument.expected b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_single_argument.expected new file mode 100644 index 0000000000000..03c36ffca9920 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_single_argument.expected @@ -0,0 +1,38 @@ +==================================== INPUT ==================================== +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name" + ] + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +scalar Argument +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Argument!] +) on FIELD +==================================== OUTPUT =================================== +import { LocalizedString } from "@i18n/localization"; +import { FragmentRefs } from "relay-runtime"; +export type foo$data = { + readonly usernameLabel: LocalizedString<"name"> | null | undefined; + readonly " $fragmentType": "foo"; +}; +export type foo$key = { + readonly " $data"?: foo$data; + readonly " $fragmentSpreads": FragmentRefs<"foo">; +}; diff --git a/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_single_argument.graphql b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_single_argument.graphql new file mode 100644 index 0000000000000..690279444bdbe --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript/fixtures/relay_custom_scalar_type_single_argument.graphql @@ -0,0 +1,26 @@ +fragment foo on ViewData { + usernameLabel @__RelayTypegenTypeOverride( + path: "@i18n/localization", + name: "LocalizedString", + generic_arguments: [ + "name" + ] + ) +} + +# %extensions% + +extend type Query { + viewData: ViewData +} + +type ViewData { + usernameLabel: String +} + +scalar Argument +directive @__RelayTypegenTypeOverride( + path: String!, + name: String!, + generic_arguments: [Argument!] +) on FIELD \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_typescript_test.rs b/compiler/crates/relay-typegen/tests/generate_typescript_test.rs index 194d61b6e9cab..b835cca20a77f 100644 --- a/compiler/crates/relay-typegen/tests/generate_typescript_test.rs +++ b/compiler/crates/relay-typegen/tests/generate_typescript_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<<40ee37b571f2844f07f55ccb1407a04a>> + * @generated SignedSource<<8470963a2d052ea22040d9f2e6c14c2e>> */ mod generate_typescript; @@ -327,6 +327,48 @@ async fn relay_client_id_field() { test_fixture(transform_fixture, file!(), "relay-client-id-field.graphql", "generate_typescript/fixtures/relay-client-id-field.expected", input, expected).await; } +#[tokio::test] +async fn relay_custom_scalar_type_boolean_argument() { + let input = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.graphql"); + let expected = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_boolean_argument.graphql", "generate_typescript/fixtures/relay_custom_scalar_type_boolean_argument.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_multiple_arguments() { + let input = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.graphql"); + let expected = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_multiple_arguments.graphql", "generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_multiple_arguments_union() { + let input = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.graphql"); + let expected = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_multiple_arguments_union.graphql", "generate_typescript/fixtures/relay_custom_scalar_type_multiple_arguments_union.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_no_arguments() { + let input = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.graphql"); + let expected = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_no_arguments.graphql", "generate_typescript/fixtures/relay_custom_scalar_type_no_arguments.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_number_arguments() { + let input = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.graphql"); + let expected = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_number_arguments.graphql", "generate_typescript/fixtures/relay_custom_scalar_type_number_arguments.expected", input, expected).await; +} + +#[tokio::test] +async fn relay_custom_scalar_type_single_argument() { + let input = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_single_argument.graphql"); + let expected = include_str!("generate_typescript/fixtures/relay_custom_scalar_type_single_argument.expected"); + test_fixture(transform_fixture, file!(), "relay_custom_scalar_type_single_argument.graphql", "generate_typescript/fixtures/relay_custom_scalar_type_single_argument.expected", input, expected).await; +} + #[tokio::test] async fn relay_resolver_with_output_type_client_interface() { let input = include_str!("generate_typescript/fixtures/relay-resolver-with-output-type-client-interface.graphql");