Skip to content

Commit

Permalink
Use import type in typescript for all imports
Browse files Browse the repository at this point in the history
  • Loading branch information
tomgasson committed Jul 29, 2022
1 parent ec62a80 commit 412acee
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 59 deletions.
93 changes: 40 additions & 53 deletions compiler/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 17 additions & 6 deletions compiler/crates/relay-compiler/src/artifact_content/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,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",
Expand Down Expand Up @@ -253,7 +253,7 @@ pub fn generate_operation(
}

write_import_type_from(
&project_config.typegen_config.language,
&project_config,
&mut section,
generated_types.imported_types,
"relay-runtime",
Expand Down Expand Up @@ -416,7 +416,7 @@ pub fn generate_split_operation(
writeln!(section, "/*::")?;
}
write_import_type_from(
&project_config.typegen_config.language,
&project_config,
&mut section,
"NormalizationSplitOperation",
"relay-runtime",
Expand Down Expand Up @@ -577,7 +577,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",
Expand Down Expand Up @@ -752,15 +752,26 @@ fn generate_use_strict_section(language: &TypegenLanguage) -> Result<GenericSect
}

fn write_import_type_from(
language: &TypegenLanguage,
project_config: &ProjectConfig,
section: &mut dyn Write,
type_: &str,
from: &str,
) -> 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
),
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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">;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fragment LinkedField on User {
name
profilePicture {
uri
width
height
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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::TypegenConfig;
use relay_typegen::TypegenLanguage;
use relay_typegen::{self};
use std::sync::Arc;

type FnvIndexMap<K, V> = IndexMap<K, V, FnvBuildHasher>;

pub fn transform_fixture(fixture: &Fixture<'_>) -> Result<String, String> {
let parts = fixture.content.split("%extensions%").collect::<Vec<_>>();
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 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,
)
});

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)
});

let mut result: Vec<String> = operation_strings.collect();
result.extend(fragment_strings);
Ok(result
.join("-------------------------------------------------------------------------------\n"))
}
Loading

0 comments on commit 412acee

Please sign in to comment.