Skip to content

Commit

Permalink
extract component_reader stuff into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
rbalicki2 committed May 11, 2024
1 parent 90ed933 commit b4c307c
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 166 deletions.
83 changes: 2 additions & 81 deletions crates/graphql_artifact_generation/src/artifact_file_contents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use isograph_schema::ObjectTypeAndFieldNames;
use lazy_static::lazy_static;

use crate::generate_artifacts::{
ClientFieldFunctionImportStatement, ClientFieldOutputType, ComponentReaderArtifactInfo,
JavaScriptImports, RefetchReaderArtifactInfo,
ClientFieldFunctionImportStatement, ClientFieldOutputType, JavaScriptImports,
RefetchReaderArtifactInfo,
};

lazy_static! {
Expand All @@ -20,85 +20,6 @@ lazy_static! {
pub static ref ISO_TS: ArtifactFileType = "iso".intern().into();
}

impl<'schema> ComponentReaderArtifactInfo<'schema> {
pub(crate) fn file_contents(self, relative_directory: &PathBuf) -> Vec<PathAndContent> {
let ComponentReaderArtifactInfo {
function_import_statement,
client_field_parameter_type,
client_field_output_type,
reader_ast,
nested_client_field_artifact_imports,
parent_type,
client_field_name,
..
} = self;

let (client_field_import_statement, client_field_type_import_statement) =
nested_client_field_names_to_import_statement(
nested_client_field_artifact_imports,
parent_type.name,
);

let output_type_text = get_output_type_text(
&function_import_statement,
parent_type.name,
client_field_name,
client_field_output_type,
);

let parent_name = parent_type.name;
let reader_param_type = format!("{parent_name}__{client_field_name}__param");
let component_name = format!("{}.{}", parent_name, client_field_name);

let reader_content = format!(
"import type {{ComponentReaderArtifact, ExtractSecondParam, ReaderAst, RefetchQueryNormalizationArtifact}} from '@isograph/react';\n\
import {{ {reader_param_type} }} from './param_type';\n\
{function_import_statement}\n\
{client_field_import_statement}\n\
const readerAst: ReaderAst<{reader_param_type}> = {reader_ast};\n\n\
const artifact: ComponentReaderArtifact<\n\
{}{reader_param_type},\n\
{}ExtractSecondParam<typeof resolver>\n\
> = {{\n\
{}kind: \"ComponentReaderArtifact\",\n\
{}componentName: \"{component_name}\",\n\
{}resolver,\n\
{}readerAst,\n\
}};\n\n\
export default artifact;\n",
" ", " ", " ", " ", " ", " ",
);

let param_type_content = format!(
"{client_field_type_import_statement}\n\
export type {reader_param_type} = {client_field_parameter_type};\n",
);

let output_type_content = format!(
"import type {{ExtractSecondParam, RefetchQueryNormalizationArtifact}} from '@isograph/react';\n\
{output_type_text}\n",
);

vec![
PathAndContent {
relative_directory: relative_directory.clone(),
file_name_prefix: *READER,
file_content: reader_content,
},
PathAndContent {
relative_directory: relative_directory.clone(),
file_name_prefix: *READER_PARAM_TYPE,
file_content: param_type_content,
},
PathAndContent {
relative_directory: relative_directory.clone(),
file_name_prefix: *READER_OUTPUT_TYPE,
file_content: output_type_content,
},
]
}
}

impl<'schema> RefetchReaderArtifactInfo<'schema> {
pub(crate) fn file_contents(self, relative_directory: &PathBuf) -> Vec<PathAndContent> {
let RefetchReaderArtifactInfo {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
use std::{collections::HashMap, path::PathBuf};

use common_lang_types::{ConstExportName, FilePath, PathAndContent, SelectableFieldName};
use isograph_schema::{
create_merged_selection_set, SchemaObject, ValidatedClientField, ValidatedSchema,
};

use crate::{
artifact_file_contents::{
get_output_type_text, nested_client_field_names_to_import_statement, READER,
READER_OUTPUT_TYPE, READER_PARAM_TYPE,
},
eager_reader_artifact_info::generate_client_field_parameter_type,
generate_artifacts::{
generate_function_import_statement_for_eager_or_component, generate_output_type,
generate_path, ClientFieldFunctionImportStatement, ClientFieldOutputType,
ClientFieldParameterType, NestedClientFieldImports, ReaderAst,
},
reader_ast::generate_reader_ast,
};

pub(crate) fn generate_component_reader_artifact<'schema>(
schema: &'schema ValidatedSchema,
client_field: &ValidatedClientField,
project_root: &PathBuf,
artifact_directory: &PathBuf,
component_name_and_path: (ConstExportName, FilePath),
) -> ComponentReaderArtifactInfo<'schema> {
if let Some((selection_set, _)) = &client_field.selection_set_and_unwraps {
let parent_type = schema
.server_field_data
.object(client_field.parent_object_id);
let mut nested_client_field_artifact_imports = HashMap::new();

let (_merged_selection_set, root_refetched_paths) = create_merged_selection_set(
schema,
schema
.server_field_data
.object(client_field.parent_object_id)
.into(),
selection_set,
None,
None,
client_field,
);

let reader_ast = generate_reader_ast(
schema,
selection_set,
0,
&mut nested_client_field_artifact_imports,
&root_refetched_paths,
);

let client_field_parameter_type = generate_client_field_parameter_type(
schema,
&selection_set,
parent_type.into(),
&mut nested_client_field_artifact_imports,
0,
);
let client_field_output_type = generate_output_type(client_field);
let function_import_statement = generate_function_import_statement_for_eager_or_component(
project_root,
artifact_directory,
component_name_and_path,
);
ComponentReaderArtifactInfo {
parent_type: parent_type.into(),
client_field_name: client_field.name,
reader_ast,
nested_client_field_artifact_imports,
function_import_statement,
client_field_output_type,
client_field_parameter_type,
}
} else {
panic!("Unsupported: client fields not on query with no selection set")
}
}

#[derive(Debug)]
pub(crate) struct ComponentReaderArtifactInfo<'schema> {
pub parent_type: &'schema SchemaObject,
pub(crate) client_field_name: SelectableFieldName,
pub nested_client_field_artifact_imports: NestedClientFieldImports,
pub client_field_output_type: ClientFieldOutputType,
pub reader_ast: ReaderAst,
pub client_field_parameter_type: ClientFieldParameterType,
pub function_import_statement: ClientFieldFunctionImportStatement,
}

impl<'schema> ComponentReaderArtifactInfo<'schema> {
pub fn path_and_content(self) -> Vec<PathAndContent> {
let ComponentReaderArtifactInfo {
parent_type,
client_field_name,
..
} = &self;

let relative_directory = generate_path(parent_type.name, *client_field_name);

self.file_contents(&relative_directory)
}

pub(crate) fn file_contents(self, relative_directory: &PathBuf) -> Vec<PathAndContent> {
let ComponentReaderArtifactInfo {
function_import_statement,
client_field_parameter_type,
client_field_output_type,
reader_ast,
nested_client_field_artifact_imports,
parent_type,
client_field_name,
..
} = self;

let (client_field_import_statement, client_field_type_import_statement) =
nested_client_field_names_to_import_statement(
nested_client_field_artifact_imports,
parent_type.name,
);

let output_type_text = get_output_type_text(
&function_import_statement,
parent_type.name,
client_field_name,
client_field_output_type,
);

let parent_name = parent_type.name;
let reader_param_type = format!("{parent_name}__{client_field_name}__param");
let component_name = format!("{}.{}", parent_name, client_field_name);

let reader_content = format!(
"import type {{ComponentReaderArtifact, ExtractSecondParam, ReaderAst, RefetchQueryNormalizationArtifact}} from '@isograph/react';\n\
import {{ {reader_param_type} }} from './param_type';\n\
{function_import_statement}\n\
{client_field_import_statement}\n\
const readerAst: ReaderAst<{reader_param_type}> = {reader_ast};\n\n\
const artifact: ComponentReaderArtifact<\n\
{}{reader_param_type},\n\
{}ExtractSecondParam<typeof resolver>\n\
> = {{\n\
{}kind: \"ComponentReaderArtifact\",\n\
{}componentName: \"{component_name}\",\n\
{}resolver,\n\
{}readerAst,\n\
}};\n\n\
export default artifact;\n",
" ", " ", " ", " ", " ", " ",
);

let param_type_content = format!(
"{client_field_type_import_statement}\n\
export type {reader_param_type} = {client_field_parameter_type};\n",
);

let output_type_content = format!(
"import type {{ExtractSecondParam, RefetchQueryNormalizationArtifact}} from '@isograph/react';\n\
{output_type_text}\n",
);

vec![
PathAndContent {
relative_directory: relative_directory.clone(),
file_name_prefix: *READER,
file_content: reader_content,
},
PathAndContent {
relative_directory: relative_directory.clone(),
file_name_prefix: *READER_PARAM_TYPE,
file_content: param_type_content,
},
PathAndContent {
relative_directory: relative_directory.clone(),
file_name_prefix: *READER_OUTPUT_TYPE,
file_content: output_type_content,
},
]
}
}
88 changes: 3 additions & 85 deletions crates/graphql_artifact_generation/src/generate_artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use isograph_schema::{
};

use crate::{
component_reader_artifact_info::{
generate_component_reader_artifact, ComponentReaderArtifactInfo,
},
eager_reader_artifact_info::{
generate_client_field_parameter_type, generate_eager_reader_artifact,
EagerReaderArtifactInfo,
Expand Down Expand Up @@ -169,66 +172,6 @@ fn get_artifact_infos<'schema>(
artifact_infos
}

fn generate_component_reader_artifact<'schema>(
schema: &'schema ValidatedSchema,
client_field: &ValidatedClientField,
project_root: &PathBuf,
artifact_directory: &PathBuf,
component_name_and_path: (ConstExportName, FilePath),
) -> ComponentReaderArtifactInfo<'schema> {
if let Some((selection_set, _)) = &client_field.selection_set_and_unwraps {
let parent_type = schema
.server_field_data
.object(client_field.parent_object_id);
let mut nested_client_field_artifact_imports = HashMap::new();

let (_merged_selection_set, root_refetched_paths) = create_merged_selection_set(
schema,
schema
.server_field_data
.object(client_field.parent_object_id)
.into(),
selection_set,
None,
None,
client_field,
);

let reader_ast = generate_reader_ast(
schema,
selection_set,
0,
&mut nested_client_field_artifact_imports,
&root_refetched_paths,
);

let client_field_parameter_type = generate_client_field_parameter_type(
schema,
&selection_set,
parent_type.into(),
&mut nested_client_field_artifact_imports,
0,
);
let client_field_output_type = generate_output_type(client_field);
let function_import_statement = generate_function_import_statement_for_eager_or_component(
project_root,
artifact_directory,
component_name_and_path,
);
ComponentReaderArtifactInfo {
parent_type: parent_type.into(),
client_field_name: client_field.name,
reader_ast,
nested_client_field_artifact_imports,
function_import_statement,
client_field_output_type,
client_field_parameter_type,
}
} else {
panic!("Unsupported: client fields not on query with no selection set")
}
}

fn generate_mutation_reader_artifact<'schema>(
schema: &'schema ValidatedSchema,
client_field: &ValidatedClientField,
Expand Down Expand Up @@ -353,31 +296,6 @@ derive_display!(ConvertFunction);
pub(crate) struct RefetchQueryArtifactImport(pub String);
derive_display!(RefetchQueryArtifactImport);

#[derive(Debug)]
pub(crate) struct ComponentReaderArtifactInfo<'schema> {
pub parent_type: &'schema SchemaObject,
pub(crate) client_field_name: SelectableFieldName,
pub nested_client_field_artifact_imports: NestedClientFieldImports,
pub client_field_output_type: ClientFieldOutputType,
pub reader_ast: ReaderAst,
pub client_field_parameter_type: ClientFieldParameterType,
pub function_import_statement: ClientFieldFunctionImportStatement,
}

impl<'schema> ComponentReaderArtifactInfo<'schema> {
pub fn path_and_content(self) -> Vec<PathAndContent> {
let ComponentReaderArtifactInfo {
parent_type,
client_field_name,
..
} = &self;

let relative_directory = generate_path(parent_type.name, *client_field_name);

self.file_contents(&relative_directory)
}
}

#[derive(Debug)]
pub(crate) struct RefetchReaderArtifactInfo<'schema> {
pub parent_type: &'schema SchemaObject,
Expand Down
1 change: 1 addition & 0 deletions crates/graphql_artifact_generation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod artifact_file_contents;
mod component_reader_artifact_info;
mod eager_reader_artifact_info;
mod entrypoint_artifact_info;
mod generate_artifacts;
Expand Down

0 comments on commit b4c307c

Please sign in to comment.