diff --git a/compiler/Cargo.lock b/compiler/Cargo.lock index 05f311c6e7add..6dd9e5164b386 100644 --- a/compiler/Cargo.lock +++ b/compiler/Cargo.lock @@ -95,31 +95,13 @@ checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" [[package]] name = "block-buffer" -version = "0.7.3" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0940dc441f31689269e10ac70eb1002a3a1d3ad1390e030043662eb7fe4688b" +checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324" dependencies = [ - "block-padding", - "byte-tools", - "byteorder", "generic-array", ] -[[package]] -name = "block-padding" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa79dedbb091f449f1f39e53edf88d5dbe95f895dae6135a8d7b881fb5af73f5" -dependencies = [ - "byte-tools", -] - -[[package]] -name = "byte-tools" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" - [[package]] name = "byteorder" version = "1.3.4" @@ -263,6 +245,15 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b" +[[package]] +name = "cpufeatures" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59a6001667ab124aebae2a495118e11d30984c3a653e99d86d58971708cf5e4b" +dependencies = [ + "libc", +] + [[package]] name = "crossbeam" version = "0.8.1" @@ -331,6 +322,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "ctor" version = "0.1.19" @@ -375,11 +376,12 @@ checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" [[package]] name = "digest" -version = "0.8.1" +version = "0.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" +checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506" dependencies = [ - "generic-array", + "block-buffer", + "crypto-common", ] [[package]] @@ -417,12 +419,6 @@ dependencies = [ "graphql-syntax", ] -[[package]] -name = "fake-simd" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed" - [[package]] name = "fastrand" version = "1.7.0" @@ -587,11 +583,12 @@ dependencies = [ [[package]] name = "generic-array" -version = "0.12.4" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffdf9f34f1447443d37393cc6c2b8313aebddcd96906caf34e54c68d8e57d7bd" +checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803" dependencies = [ "typenum", + "version_check", ] [[package]] @@ -1039,13 +1036,11 @@ checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" [[package]] name = "md-5" -version = "0.8.0" +version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a18af3dcaf2b0219366cdb4e2af65a6101457b415c3d1a5c71dd9c2b7c77b9c8" +checksum = "658646b21e0b72f7866c7038ab086d3d5e1cd6271f060fd37defb241949d0582" dependencies = [ - "block-buffer", "digest", - "opaque-debug", ] [[package]] @@ -1148,12 +1143,6 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "18a6dbe30758c9f83eb00cbea4ac95966305f5a7772f3f42ebfc7fc7eddbd8e1" -[[package]] -name = "opaque-debug" -version = "0.2.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" - [[package]] name = "openssl" version = "0.10.32" @@ -1892,26 +1881,24 @@ dependencies = [ [[package]] name = "sha-1" -version = "0.8.2" +version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7d94d0bede923b3cea61f3f1ff57ff8cdfd77b400fb8f9998949e0cf04163df" +checksum = "028f48d513f9678cda28f6e4064755b3fbb2af6acd672f2c209b62323f7aea0f" dependencies = [ - "block-buffer", + "cfg-if", + "cpufeatures", "digest", - "fake-simd", - "opaque-debug", ] [[package]] name = "sha2" -version = "0.8.2" +version = "0.10.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a256f46ea78a0c0d9ff00077504903ac881a1dafdc20da66545699e7776b3e69" +checksum = "55deaec60f81eefe3cce0dc50bda92d6d8e88f2a27df7c5033b42afeb1ed2676" dependencies = [ - "block-buffer", + "cfg-if", + "cpufeatures", "digest", - "fake-simd", - "opaque-debug", ] [[package]] @@ -2206,9 +2193,9 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] name = "typenum" -version = "1.12.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" +checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" [[package]] name = "unicase" diff --git a/compiler/crates/relay-compiler/src/artifact_content/content.rs b/compiler/crates/relay-compiler/src/artifact_content/content.rs index fb33c92e51a5a..f3364ccfd4145 100644 --- a/compiler/crates/relay-compiler/src/artifact_content/content.rs +++ b/compiler/crates/relay-compiler/src/artifact_content/content.rs @@ -93,7 +93,7 @@ pub fn generate_updatable_query( } write_import_type_from( - &project_config.typegen_config.language, + &project_config, &mut section, generated_types.imported_types, "relay-runtime", @@ -257,7 +257,7 @@ pub fn generate_operation( } write_import_type_from( - &project_config.typegen_config.language, + &project_config, &mut section, generated_types.imported_types, "relay-runtime", @@ -423,7 +423,7 @@ pub fn generate_split_operation( writeln!(section, "/*::")?; } write_import_type_from( - &project_config.typegen_config.language, + &project_config, &mut section, "NormalizationSplitOperation", "relay-runtime", @@ -595,7 +595,7 @@ fn generate_read_only_fragment( } write_import_type_from( - &project_config.typegen_config.language, + &project_config, &mut section, generated_types.imported_types, "relay-runtime", @@ -785,15 +785,26 @@ fn generate_use_strict_section(language: &TypegenLanguage) -> Result FmtResult { + let language = &project_config.typegen_config.language; match language { TypegenLanguage::JavaScript => Ok(()), TypegenLanguage::Flow => writeln!(section, "import type {{ {} }} from '{}';", type_, from), - TypegenLanguage::TypeScript => writeln!(section, "import {{ {} }} from '{}';", type_, from), + TypegenLanguage::TypeScript => writeln!( + section, + "import {}{{ {} }} from \"{}\";", + if project_config.typegen_config.use_import_type_syntax { + "type " + } else { + "" + }, + type_, + from + ), } } diff --git a/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax/fixtures/simple.expected b/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax/fixtures/simple.expected new file mode 100644 index 0000000000000..b175a1638d38d --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax/fixtures/simple.expected @@ -0,0 +1,24 @@ +==================================== INPUT ==================================== +fragment LinkedField on User { + name + profilePicture { + uri + width + height + } +} +==================================== OUTPUT =================================== +import type { FragmentRefs } from "relay-runtime"; +export type LinkedField$data = { + readonly name: string | null; + readonly profilePicture: { + readonly height: number | null; + readonly uri: string | null; + readonly width: number | null; + } | null; + readonly " $fragmentType": "LinkedField"; +}; +export type LinkedField$key = { + readonly " $data"?: LinkedField$data; + readonly " $fragmentSpreads": FragmentRefs<"LinkedField">; +}; diff --git a/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax/fixtures/simple.graphql b/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax/fixtures/simple.graphql new file mode 100644 index 0000000000000..80d214783ca3b --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax/fixtures/simple.graphql @@ -0,0 +1,8 @@ +fragment LinkedField on User { + name + profilePicture { + uri + width + height + } +} \ No newline at end of file diff --git a/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax/mod.rs b/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax/mod.rs new file mode 100644 index 0000000000000..1afa7cb553631 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax/mod.rs @@ -0,0 +1,119 @@ +/* + * 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. + */ + +use common::ConsoleLogger; +use common::FeatureFlag; +use common::FeatureFlags; +use common::SourceLocationKey; +use fixture_tests::Fixture; +use fnv::FnvBuildHasher; +use fnv::FnvHashMap; +use graphql_ir::build; +use graphql_ir::Program; +use graphql_syntax::parse_executable; +use indexmap::IndexMap; +use intern::string_key::Intern; +use relay_codegen::JsModuleFormat; +use relay_config::CustomScalarType; +use relay_config::CustomScalarTypeImport; +use relay_config::ProjectConfig; +use relay_test_schema::get_test_schema; +use relay_test_schema::get_test_schema_with_extensions; +use relay_transforms::apply_transforms; +use relay_typegen::FragmentLocations; +use relay_typegen::TypegenConfig; +use relay_typegen::TypegenLanguage; +use relay_typegen::{self}; +use std::sync::Arc; + +type FnvIndexMap = IndexMap; + +pub fn transform_fixture(fixture: &Fixture<'_>) -> Result { + let parts = fixture.content.split("%extensions%").collect::>(); + let (source, schema) = match parts.as_slice() { + [source, extensions] => (source, get_test_schema_with_extensions(extensions)), + [source] => (source, get_test_schema()), + _ => panic!(), + }; + + let source_location = SourceLocationKey::standalone(fixture.file_name); + + let mut sources = FnvHashMap::default(); + sources.insert(source_location, source); + let ast = parse_executable(source, source_location).unwrap_or_else(|e| { + panic!("Encountered error building AST: {:?}", e); + }); + let ir = build(&schema, &ast.definitions).unwrap_or_else(|e| { + panic!("Encountered error building IR {:?}", e); + }); + let program = Program::from_definitions(Arc::clone(&schema), ir); + let mut custom_scalar_types = FnvIndexMap::default(); + custom_scalar_types.insert( + "JSON".intern(), + CustomScalarType::Path(CustomScalarTypeImport { + name: "JSON".intern(), + path: "TypeDefsFile".into(), + }), + ); + let project_config = ProjectConfig { + name: "test".intern(), + js_module_format: JsModuleFormat::Haste, + typegen_config: TypegenConfig { + language: TypegenLanguage::TypeScript, + use_import_type_syntax: true, + custom_scalar_types, + ..Default::default() + }, + feature_flags: Arc::new(FeatureFlags { + enable_fragment_aliases: FeatureFlag::Enabled, + ..Default::default() + }), + ..Default::default() + }; + let programs = apply_transforms( + &project_config, + Arc::new(program), + Default::default(), + Arc::new(ConsoleLogger), + None, + None, + ) + .unwrap(); + + let fragment_locations = FragmentLocations::new(programs.typegen.fragments()); + let mut operations: Vec<_> = programs.typegen.operations().collect(); + operations.sort_by_key(|op| op.name.item); + let operation_strings = operations.into_iter().map(|typegen_operation| { + let normalization_operation = programs + .normalization + .operation(typegen_operation.name.item) + .unwrap(); + relay_typegen::generate_operation_type_exports_section( + typegen_operation, + normalization_operation, + &schema, + &project_config, + &fragment_locations, + ) + }); + + let mut fragments: Vec<_> = programs.typegen.fragments().collect(); + fragments.sort_by_key(|frag| frag.name.item); + let fragment_strings = fragments.into_iter().map(|frag| { + relay_typegen::generate_fragment_type_exports_section( + frag, + &schema, + &project_config, + &fragment_locations, + ) + }); + + let mut result: Vec = operation_strings.collect(); + result.extend(fragment_strings); + Ok(result + .join("-------------------------------------------------------------------------------\n")) +} diff --git a/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax_test.rs b/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax_test.rs new file mode 100644 index 0000000000000..11636b1e1f432 --- /dev/null +++ b/compiler/crates/relay-typegen/tests/generate_typescript_import_syntax_test.rs @@ -0,0 +1,20 @@ +/* + * 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. + * + * @generated SignedSource<> + */ + +mod generate_typescript_import_syntax; + +use generate_typescript_import_syntax::transform_fixture; +use fixture_tests::test_fixture; + +#[test] +fn simple() { + let input = include_str!("generate_typescript_import_syntax/fixtures/simple.graphql"); + let expected = include_str!("generate_typescript_import_syntax/fixtures/simple.expected"); + test_fixture(transform_fixture, "simple.graphql", "generate_typescript_import_syntax/fixtures/simple.expected", input, expected); +}