Skip to content

Commit

Permalink
continue cleanup of compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
rbalicki2 committed May 10, 2024
1 parent 68be141 commit 5cc113c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 172 deletions.
3 changes: 1 addition & 2 deletions crates/isograph_cli/src/batch_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ pub(crate) fn handle_compile_command(
.map(|(fetchable_object_id, _)| *fetchable_object_id)
.collect::<Vec<_>>();
for fetchable_object_id in fetchable_types.into_iter() {
schema
.add_exposed_fields_to_parent_object_types(fetchable_object_id, config.options)?;
schema.add_exposed_fields_to_parent_object_types(fetchable_object_id)?;
}

let canonicalized_root_path = get_canonicalized_root_path(config)?;
Expand Down
145 changes: 5 additions & 140 deletions crates/isograph_schema/src/argument_map.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
use std::collections::HashMap;

use common_lang_types::{
DescriptionValue, InputValueName, IsographObjectTypeName, Location, SelectableFieldName, Span,
StringLiteralValue, WithLocation, WithSpan,
};
use graphql_lang_types::{
ConstantValue, GraphQLDirective, GraphQLFieldDefinition, GraphQLInputValueDefinition,
GraphQLTypeAnnotation,
InputValueName, IsographObjectTypeName, Location, SelectableFieldName, StringLiteralValue,
WithLocation,
};
use graphql_lang_types::{GraphQLInputValueDefinition, GraphQLTypeAnnotation};
use intern::{string_key::Intern, Lookup};
use isograph_lang_types::{SelectableServerFieldId, ServerFieldId, ServerObjectId};
use isograph_lang_types::{SelectableServerFieldId, ServerFieldId};

use crate::{
FieldMapItem, IsographObjectTypeDefinition, ProcessObjectTypeDefinitionOutcome,
ProcessTypeDefinitionError, ProcessTypeDefinitionResult, ProcessedFieldMapItem,
FieldMapItem, ProcessTypeDefinitionError, ProcessTypeDefinitionResult, ProcessedFieldMapItem,
UnvalidatedSchema,
};
use isograph_config::ConfigOptions;

pub(crate) struct ArgumentMap {
arguments: Vec<WithLocation<PotentiallyModifiedArgument>>,
Expand Down Expand Up @@ -145,46 +140,6 @@ impl ArgumentMap {

Ok(processed_field_map_item)
}

pub(crate) fn into_arguments(
self,
schema: &mut UnvalidatedSchema,
options: ConfigOptions,
) -> Vec<WithLocation<GraphQLInputValueDefinition>> {
self.arguments
.into_iter()
.map(|with_location| {
with_location.map(|potentially_modified_argument| {
match potentially_modified_argument {
PotentiallyModifiedArgument::Unmodified(unmodified) => unmodified,
PotentiallyModifiedArgument::Modified(modified) => {
let ModifiedArgument {
description,
name,
object,
default_value,
directives,
} = modified;

GraphQLInputValueDefinition {
description,
name,
type_: object.map(|modified_object| {
modified_object
.create_and_get_name(schema, options)
.lookup()
.intern()
.into()
}),
default_value,
directives,
}
}
}
})
})
.collect()
}
}

enum PotentiallyModifiedArgument {
Expand All @@ -198,92 +153,9 @@ enum PotentiallyModifiedArgument {
/// only deleted.
#[derive(Debug)]
pub(crate) struct ModifiedObject {
object_id: ServerObjectId,
field_map: HashMap<SelectableFieldName, PotentiallyModifiedField>,
}

impl ModifiedObject {
fn create_and_get_name(
self,
schema: &mut UnvalidatedSchema,
options: ConfigOptions,
) -> IsographObjectTypeName {
let original_object = schema.server_field_data.object(self.object_id);

let fields = original_object
.server_field_ids
.iter()
.flat_map(|field_id| {
let field = schema.server_field(*field_id);

// HACK alert
if field.name.item == "__typename".intern().into() {
return None;
}

let potentially_modified_field = self.field_map.get(&field.name.item)?;

let new_field = match potentially_modified_field {
PotentiallyModifiedField::Unmodified(_) => WithLocation::new(
GraphQLFieldDefinition {
description: field.description.map(|description| {
WithSpan::new(description, Span::todo_generated())
}),
name: field.name,
type_: field.associated_data.clone(),
arguments: field.arguments.clone(),
directives: vec![],
},
Location::generated(),
),
PotentiallyModifiedField::Modified(_) => todo!("modified"),
};

Some(new_field)
})
.collect();

let item = IsographObjectTypeDefinition {
// TODO it looks like we throw away span info for descriptions, which makes sense?
description: original_object
.description
.clone()
.map(|description| WithSpan::new(description, Span::todo_generated())),
// TODO confirm that names don't have to be unique
name: WithLocation::new(
format!("{}__generated", original_object.name.lookup())
.intern()
.into(),
Location::generated(),
),
// Very unclear what to do here
interfaces: vec![],
directives: vec![],
fields,
};

let ProcessObjectTypeDefinitionOutcome { object_id, .. } = schema
.process_object_type_definition(
item,
// Obviously, this is a smell
&mut HashMap::new(),
&mut HashMap::new(),
true,
options,
)
// This is not (yet) true. If you reference a non-existent type in
// a @exposeField directive, the compiler panics here. The solution is to
// process these directives after the definitions have been validated,
// but before resolvers hvae been validated.
.expect(
"Expected object creation to work. This is \
indicative of a bug in Isograph.",
);

schema.server_field_data.object(object_id).name
}
}

#[derive(Debug)]
pub(crate) enum PotentiallyModifiedField {
Unmodified(ServerFieldId),
Expand Down Expand Up @@ -313,11 +185,8 @@ pub(crate) struct ModifiedField {

#[derive(Debug)]
struct ModifiedArgument {
description: Option<WithSpan<DescriptionValue>>,
name: WithLocation<InputValueName>,
object: GraphQLTypeAnnotation<ModifiedObject>,
default_value: Option<WithLocation<ConstantValue>>,
directives: Vec<GraphQLDirective<ConstantValue>>,
}

impl ModifiedArgument {
Expand Down Expand Up @@ -348,7 +217,6 @@ impl ModifiedArgument {
let object = schema.server_field_data.object(object_id);

ModifiedObject {
object_id,
field_map: object
.server_field_ids
.iter()
Expand All @@ -371,9 +239,6 @@ impl ModifiedArgument {
// TODO We can probably avoid cloning here
Self {
name: unmodified.name,
description: unmodified.description,
default_value: unmodified.default_value.clone(),
directives: unmodified.directives.clone(),
object,
}
}
Expand Down
3 changes: 0 additions & 3 deletions crates/isograph_schema/src/create_merged_selection_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ pub fn create_merged_selection_set(
server_schema_mutation_field_name,
aliased_exposed_field_name: mutation_primary_field_name,
mutation_field_arguments,
filtered_mutation_field_arguments: _,
mutation_primary_field_return_type_object_id,
field_map: _,
expose_field_fetchable_field_parent_id,
Expand Down Expand Up @@ -740,7 +739,6 @@ fn merge_scalar_client_field(
aliased_exposed_field_name: mutation_primary_field_name,
fetchable_type_original_field_name: server_schema_mutation_field_name,
mutation_field_arguments,
filtered_mutation_field_arguments,
mutation_field_name: _,
mutation_primary_field_return_type_object_id,
field_map,
Expand All @@ -758,7 +756,6 @@ fn merge_scalar_client_field(
fetchable_type_original_field_name: *server_schema_mutation_field_name,
aliased_exposed_field_name: *mutation_primary_field_name,
mutation_field_arguments: mutation_field_arguments.clone(),
filtered_mutation_field_arguments: filtered_mutation_field_arguments.clone(),
mutation_primary_field_return_type_object_id:
*mutation_primary_field_return_type_object_id,
field_map: field_map.clone(),
Expand Down
38 changes: 12 additions & 26 deletions crates/isograph_schema/src/expose_field_directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use graphql_lang_types::{
GraphQLInputValueDefinition,
};
use intern::{string_key::Intern, Lookup};
use isograph_config::ConfigOptions;
use isograph_lang_types::{
ClientFieldId, IsographSelectionVariant, ScalarFieldSelection, SelectableServerFieldId,
Selection, ServerFieldId, ServerFieldSelection, ServerObjectId,
Expand Down Expand Up @@ -77,7 +76,6 @@ impl UnvalidatedSchema {
pub fn add_exposed_fields_to_parent_object_types(
&mut self,
parent_object_id: ServerObjectId,
options: ConfigOptions,
) -> ProcessTypeDefinitionResult<()> {
// TODO don't clone if possible
let parent_object = self.server_field_data.object(parent_object_id);
Expand All @@ -99,7 +97,6 @@ impl UnvalidatedSchema {
expose_field_directive,
parent_object_name,
parent_object_id,
options,
)?;
}

Expand All @@ -111,7 +108,6 @@ impl UnvalidatedSchema {
expose_field_directive: &ExposeFieldDirective,
parent_object_name: IsographObjectTypeName,
parent_object_id: ServerObjectId,
options: ConfigOptions,
) -> Result<(), WithLocation<ProcessTypeDefinitionError>> {
let ExposeFieldDirective {
expose_as,
Expand All @@ -136,18 +132,16 @@ impl UnvalidatedSchema {
.map(|x| *x);

if let Some(SelectableServerFieldId::Object(mutation_field_object_id)) = payload_id {
let (mutation_field_args_without_id, processed_field_map_items) =
skip_arguments_contained_in_field_map(
self,
mutation_field_arguments.clone(),
// TODO make this a no-op
mutation_field_payload_type_name.lookup().intern().into(),
parent_object_name,
mutation_field_name,
// TODO don't clone
field_map.clone(),
options,
)?;
let processed_field_map_items = skip_arguments_contained_in_field_map(
self,
mutation_field_arguments.clone(),
// TODO make this a no-op
mutation_field_payload_type_name.lookup().intern().into(),
parent_object_name,
mutation_field_name,
// TODO don't clone
field_map.clone(),
)?;

// payload object is the object type of the mutation field, e.g. SetBestFriendResponse
let payload_object = self.server_field_data.object(mutation_field_object_id);
Expand Down Expand Up @@ -230,7 +224,6 @@ impl UnvalidatedSchema {
fetchable_type_original_field_name,
aliased_exposed_field_name: path_selectable_field_name,
mutation_field_arguments: mutation_field_arguments.to_vec(),
filtered_mutation_field_arguments: mutation_field_args_without_id.to_vec(),
mutation_primary_field_return_type_object_id:
maybe_abstract_parent_object_id,
field_map: field_map.to_vec(),
Expand Down Expand Up @@ -345,11 +338,7 @@ fn skip_arguments_contained_in_field_map(
mutation_object_name: IsographObjectTypeName,
mutation_field_name: SelectableFieldName,
field_map_items: Vec<FieldMapItem>,
options: ConfigOptions,
) -> ProcessTypeDefinitionResult<(
Vec<WithLocation<GraphQLInputValueDefinition>>,
Vec<ProcessedFieldMapItem>,
)> {
) -> ProcessTypeDefinitionResult<Vec<ProcessedFieldMapItem>> {
let mut processed_field_map_items = Vec::with_capacity(field_map_items.len());
// TODO
// We need to create entirely new arguments, which are the existing arguments minus
Expand All @@ -366,8 +355,5 @@ fn skip_arguments_contained_in_field_map(
)?);
}

Ok((
argument_map.into_arguments(schema, options),
processed_field_map_items,
))
Ok(processed_field_map_items)
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ pub struct ImperativelyLoadedFieldVariant {
pub aliased_exposed_field_name: SelectableFieldName,
pub mutation_primary_field_return_type_object_id: ServerObjectId,
pub mutation_field_arguments: Vec<WithLocation<GraphQLInputValueDefinition>>,
pub filtered_mutation_field_arguments: Vec<WithLocation<GraphQLInputValueDefinition>>,
pub field_map: Vec<FieldMapItem>,
pub expose_field_fetchable_field_parent_id: ServerObjectId,
}
Expand Down

0 comments on commit 5cc113c

Please sign in to comment.