From 72f66445b1d3a06dc1afe6f82cc64a992443bb2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Wa=C5=82ach?= <35966385+PatrykWalach@users.noreply.github.com> Date: Sat, 28 Dec 2024 09:38:16 +0100 Subject: [PATCH] no_babel_transform option & change entrypoint iso overloads to return void (#286) - add a no_babel_transform option to the config - if this is set to true, then iso literals for entrypoints have type void (even though they will actually be a x => x) - if this is set to true, then iso literals that are called (e.g. field definitions) will not throw an error - this gives a better overall experience to folks that cannot use the babel transform --- .../src/generate_artifacts.rs | 6 +- .../src/iso_overload_file.rs | 72 +++++++++++-------- .../isograph_compiler/src/compiler_state.rs | 14 ++-- .../src/compilation_options.rs | 8 +-- .../src/isograph-components/__isograph/iso.ts | 3 +- .../pet-demo/src/components/__isograph/iso.ts | 3 +- .../src/components/__isograph/iso.ts | 3 +- .../isograph-config-schema.json | 7 +- .../src/tests/__isograph/iso.ts | 3 +- 9 files changed, 66 insertions(+), 53 deletions(-) diff --git a/crates/graphql_artifact_generation/src/generate_artifacts.rs b/crates/graphql_artifact_generation/src/generate_artifacts.rs index 4db9e470f..1db43c6d4 100644 --- a/crates/graphql_artifact_generation/src/generate_artifacts.rs +++ b/crates/graphql_artifact_generation/src/generate_artifacts.rs @@ -7,7 +7,7 @@ use graphql_lang_types::{ }; use intern::{string_key::Intern, Lookup}; -use isograph_config::{GenerateFileExtensionsOption, OptionalValidationLevel}; +use isograph_config::GenerateFileExtensionsOption; use isograph_lang_types::{ ArgumentKeyAndValue, ClientFieldId, NonConstantValue, SelectableServerFieldId, SelectionType, ServerFieldSelection, TypeAnnotation, UnionVariant, VariableDefinition, @@ -79,7 +79,7 @@ pub fn get_artifact_path_and_content( project_root: &Path, artifact_directory: &Path, file_extensions: GenerateFileExtensionsOption, - on_missing_babel_transform: OptionalValidationLevel, + no_babel_transform: bool, ) -> Vec { let mut encountered_client_field_map = BTreeMap::new(); let mut path_and_contents = vec![]; @@ -309,7 +309,7 @@ pub fn get_artifact_path_and_content( path_and_contents.push(build_iso_overload_artifact( schema, file_extensions, - on_missing_babel_transform, + no_babel_transform, )); path_and_contents diff --git a/crates/graphql_artifact_generation/src/iso_overload_file.rs b/crates/graphql_artifact_generation/src/iso_overload_file.rs index 3a04edb99..5543e1aee 100644 --- a/crates/graphql_artifact_generation/src/iso_overload_file.rs +++ b/crates/graphql_artifact_generation/src/iso_overload_file.rs @@ -1,5 +1,5 @@ use intern::Lookup; -use isograph_config::{GenerateFileExtensionsOption, OptionalValidationLevel}; +use isograph_config::GenerateFileExtensionsOption; use std::{cmp::Ordering, path::PathBuf}; use common_lang_types::{ArtifactPathAndContent, SelectableFieldName}; @@ -13,29 +13,45 @@ use crate::generate_artifacts::ISO_TS; fn build_iso_overload_for_entrypoint( validated_client_field: &ValidatedClientField, file_extensions: GenerateFileExtensionsOption, -) -> (String, String) { - let mut s: String = "".to_string(); - let import = format!( - "import entrypoint_{} from '../__isograph/{}/{}/entrypoint{}';\n", - validated_client_field.type_and_field.underscore_separated(), - validated_client_field.type_and_field.type_name, - validated_client_field.type_and_field.field_name, - file_extensions.ts() - ); + no_babel_transform: bool, +) -> (Option, String) { let formatted_field = format!( "entrypoint {}.{}", validated_client_field.type_and_field.type_name, validated_client_field.type_and_field.field_name ); - s.push_str(&format!( - " + match no_babel_transform { + true => ( + None, + format!( + " +export function iso( + param: T & MatchesWhitespaceAndString<'{}', T> +): void;\n", + formatted_field + ), + ), + false => { + let mut s: String = "".to_string(); + let import = format!( + "import entrypoint_{} from '../__isograph/{}/{}/entrypoint{}';\n", + validated_client_field.type_and_field.underscore_separated(), + validated_client_field.type_and_field.type_name, + validated_client_field.type_and_field.field_name, + file_extensions.ts() + ); + + s.push_str(&format!( + " export function iso( param: T & MatchesWhitespaceAndString<'{}', T> ): typeof entrypoint_{};\n", - formatted_field, - validated_client_field.type_and_field.underscore_separated(), - )); - (import, s) + formatted_field, + validated_client_field.type_and_field.underscore_separated(), + )); + (Some(import), s) + } + } } fn build_iso_overload_for_client_defined_field( @@ -80,7 +96,7 @@ export function iso( pub(crate) fn build_iso_overload_artifact( schema: &ValidatedSchema, file_extensions: GenerateFileExtensionsOption, - on_missing_babel_transform: OptionalValidationLevel, + no_babel_transform: bool, ) -> ArtifactPathAndContent { let mut imports = "import type { IsographEntrypoint } from '@isograph/react';\n".to_string(); let mut content = String::from( @@ -144,9 +160,11 @@ type MatchesWhitespaceAndString< let entrypoint_overloads = sorted_entrypoints(schema) .into_iter() - .map(|field| build_iso_overload_for_entrypoint(field, file_extensions)); + .map(|field| build_iso_overload_for_entrypoint(field, file_extensions, no_babel_transform)); for (import, entrypoint_overload) in entrypoint_overloads { - imports.push_str(&import); + if let Some(import) = import { + imports.push_str(&import); + } content.push_str(&entrypoint_overload); } @@ -159,20 +177,14 @@ export function iso(_isographLiteralText: string): {\n", ); - content.push_str(match on_missing_babel_transform { - OptionalValidationLevel::Error => { + content.push_str(match no_babel_transform { + false => { " throw new Error('iso: Unexpected invocation at runtime. Either the Babel transform ' + 'was not set up, or it failed to identify this call site. Make sure it ' + - 'is being used verbatim as `iso`.');" - } - OptionalValidationLevel::Warn => { - - " console.warn('iso: Unexpected invocation at runtime. Either the Babel transform ' + - 'was not set up, or it failed to identify this call site. Make sure it ' + - 'is being used verbatim as `iso`.'); - return (clientFieldResolver: any) => clientFieldResolver;" + 'is being used verbatim as `iso`. If you cannot use the babel transform, ' + + 'set options.no_babel_transform to true in your Isograph config. ');" } - OptionalValidationLevel::Ignore => { + true => { " return (clientFieldResolver: any) => clientFieldResolver;" } }); diff --git a/crates/isograph_compiler/src/compiler_state.rs b/crates/isograph_compiler/src/compiler_state.rs index 1095faed8..d6affede5 100644 --- a/crates/isograph_compiler/src/compiler_state.rs +++ b/crates/isograph_compiler/src/compiler_state.rs @@ -1,9 +1,7 @@ use std::path::PathBuf; use graphql_artifact_generation::get_artifact_path_and_content; -use isograph_config::{ - create_config, CompilerConfig, GenerateFileExtensionsOption, OptionalValidationLevel, -}; +use isograph_config::{create_config, CompilerConfig, GenerateFileExtensionsOption}; use isograph_schema::{Schema, UnvalidatedSchema}; use crate::{ @@ -85,7 +83,7 @@ impl CompilerState { source_files, &self.config, self.config.options.generate_file_extensions, - self.config.options.on_missing_babel_transform, + self.config.options.no_babel_transform, )?; Ok(CompilationStats { client_field_count: stats.client_field_count, @@ -102,7 +100,7 @@ impl CompilerState { source_files, &self.config, self.config.options.generate_file_extensions, - self.config.options.on_missing_babel_transform, + self.config.options.no_babel_transform, )?; Ok(CompilationStats { client_field_count: stats.client_field_count, @@ -121,7 +119,7 @@ impl CompilerState { source_files, &self.config, self.config.options.generate_file_extensions, - self.config.options.on_missing_babel_transform, + self.config.options.no_babel_transform, )?; Ok(CompilationStats { client_field_count: stats.client_field_count, @@ -152,7 +150,7 @@ pub fn validate_and_create_artifacts_from_source_files( source_files: SourceFiles, config: &CompilerConfig, file_extensions: GenerateFileExtensionsOption, - on_missing_babel_transform: OptionalValidationLevel, + no_babel_transform: bool, ) -> Result { // Create schema let mut unvalidated_schema = UnvalidatedSchema::new(); @@ -169,7 +167,7 @@ pub fn validate_and_create_artifacts_from_source_files( &config.project_root, &config.artifact_directory, file_extensions, - on_missing_babel_transform, + no_babel_transform, ); let total_artifacts_written = write_artifacts_to_disk(artifacts, &config.artifact_directory)?; diff --git a/crates/isograph_config/src/compilation_options.rs b/crates/isograph_config/src/compilation_options.rs index 06e4c10ee..71ceb3470 100644 --- a/crates/isograph_config/src/compilation_options.rs +++ b/crates/isograph_config/src/compilation_options.rs @@ -28,7 +28,7 @@ pub struct CompilerConfig { #[derive(Default, Debug, Clone, Copy)] pub struct ConfigOptions { pub on_invalid_id_type: OptionalValidationLevel, - pub on_missing_babel_transform: OptionalValidationLevel, + pub no_babel_transform: bool, pub generate_file_extensions: GenerateFileExtensionsOption, } @@ -188,7 +188,7 @@ pub fn create_config(config_location: PathBuf) -> CompilerConfig { #[serde(default, deny_unknown_fields)] pub struct ConfigFileOptions { on_invalid_id_type: ConfigFileOptionalValidationLevel, - on_missing_babel_transform: ConfigFileOptionalValidationLevel, + no_babel_transform: bool, include_file_extensions_in_import_statements: bool, } @@ -212,9 +212,7 @@ impl Default for ConfigFileOptionalValidationLevel { fn create_options(options: ConfigFileOptions) -> ConfigOptions { ConfigOptions { on_invalid_id_type: create_optional_validation_level(options.on_invalid_id_type), - on_missing_babel_transform: create_optional_validation_level( - options.on_missing_babel_transform, - ), + no_babel_transform: options.no_babel_transform, generate_file_extensions: create_generate_file_extensions( options.include_file_extensions_in_import_statements, ), diff --git a/demos/github-demo/src/isograph-components/__isograph/iso.ts b/demos/github-demo/src/isograph-components/__isograph/iso.ts index b9cc634c2..85346833b 100644 --- a/demos/github-demo/src/isograph-components/__isograph/iso.ts +++ b/demos/github-demo/src/isograph-components/__isograph/iso.ts @@ -180,5 +180,6 @@ export function iso(_isographLiteralText: string): { throw new Error('iso: Unexpected invocation at runtime. Either the Babel transform ' + 'was not set up, or it failed to identify this call site. Make sure it ' + - 'is being used verbatim as `iso`.'); + 'is being used verbatim as `iso`. If you cannot use the babel transform, ' + + 'set options.no_babel_transform to true in your Isograph config. '); } \ No newline at end of file diff --git a/demos/pet-demo/src/components/__isograph/iso.ts b/demos/pet-demo/src/components/__isograph/iso.ts index d49365be6..4e32e31d0 100644 --- a/demos/pet-demo/src/components/__isograph/iso.ts +++ b/demos/pet-demo/src/components/__isograph/iso.ts @@ -245,5 +245,6 @@ export function iso(_isographLiteralText: string): { throw new Error('iso: Unexpected invocation at runtime. Either the Babel transform ' + 'was not set up, or it failed to identify this call site. Make sure it ' + - 'is being used verbatim as `iso`.'); + 'is being used verbatim as `iso`. If you cannot use the babel transform, ' + + 'set options.no_babel_transform to true in your Isograph config. '); } \ No newline at end of file diff --git a/demos/vite-demo/src/components/__isograph/iso.ts b/demos/vite-demo/src/components/__isograph/iso.ts index 8eac0efd9..4b2850233 100644 --- a/demos/vite-demo/src/components/__isograph/iso.ts +++ b/demos/vite-demo/src/components/__isograph/iso.ts @@ -70,5 +70,6 @@ export function iso(_isographLiteralText: string): { throw new Error('iso: Unexpected invocation at runtime. Either the Babel transform ' + 'was not set up, or it failed to identify this call site. Make sure it ' + - 'is being used verbatim as `iso`.'); + 'is being used verbatim as `iso`. If you cannot use the babel transform, ' + + 'set options.no_babel_transform to true in your Isograph config. '); } \ No newline at end of file diff --git a/libs/isograph-compiler/isograph-config-schema.json b/libs/isograph-compiler/isograph-config-schema.json index 8ab82d23b..69599ac44 100644 --- a/libs/isograph-compiler/isograph-config-schema.json +++ b/libs/isograph-compiler/isograph-config-schema.json @@ -80,10 +80,11 @@ "default": false, "type": "boolean" }, - "on_invalid_id_type": { - "$ref": "#/definitions/ConfigFileOptionalValidationLevel" + "no_babel_transform": { + "default": false, + "type": "boolean" }, - "on_missing_babel_transform": { + "on_invalid_id_type": { "$ref": "#/definitions/ConfigFileOptionalValidationLevel" } }, diff --git a/libs/isograph-react/src/tests/__isograph/iso.ts b/libs/isograph-react/src/tests/__isograph/iso.ts index 62eafeb95..2945e3361 100644 --- a/libs/isograph-react/src/tests/__isograph/iso.ts +++ b/libs/isograph-react/src/tests/__isograph/iso.ts @@ -95,5 +95,6 @@ export function iso(_isographLiteralText: string): { throw new Error('iso: Unexpected invocation at runtime. Either the Babel transform ' + 'was not set up, or it failed to identify this call site. Make sure it ' + - 'is being used verbatim as `iso`.'); + 'is being used verbatim as `iso`. If you cannot use the babel transform, ' + + 'set options.no_babel_transform to true in your Isograph config. '); } \ No newline at end of file