Skip to content

Commit

Permalink
no_babel_transform option & change entrypoint iso overloads to return…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
PatrykWalach authored Dec 28, 2024
1 parent 1ee228e commit 72f6644
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 53 deletions.
6 changes: 3 additions & 3 deletions crates/graphql_artifact_generation/src/generate_artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<ArtifactPathAndContent> {
let mut encountered_client_field_map = BTreeMap::new();
let mut path_and_contents = vec![];
Expand Down Expand Up @@ -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
Expand Down
72 changes: 42 additions & 30 deletions crates/graphql_artifact_generation/src/iso_overload_file.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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>, 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<T>(
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<T>(
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(
Expand Down Expand Up @@ -80,7 +96,7 @@ export function iso<T>(
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(
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;"
}
});
Expand Down
14 changes: 6 additions & 8 deletions crates/isograph_compiler/src/compiler_state.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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<usize, BatchCompileError> {
// Create schema
let mut unvalidated_schema = UnvalidatedSchema::new();
Expand All @@ -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)?;
Expand Down
8 changes: 3 additions & 5 deletions crates/isograph_config/src/compilation_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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,
}

Expand All @@ -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,
),
Expand Down
3 changes: 2 additions & 1 deletion demos/github-demo/src/isograph-components/__isograph/iso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. ');
}
3 changes: 2 additions & 1 deletion demos/pet-demo/src/components/__isograph/iso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. ');
}
3 changes: 2 additions & 1 deletion demos/vite-demo/src/components/__isograph/iso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. ');
}
7 changes: 4 additions & 3 deletions libs/isograph-compiler/isograph-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
Expand Down
3 changes: 2 additions & 1 deletion libs/isograph-react/src/tests/__isograph/iso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. ');
}

0 comments on commit 72f6644

Please sign in to comment.