-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
98 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use std::collections::hash_map::Entry; | ||
|
||
use common_lang_types::{Location, Span, WithLocation, WithSpan}; | ||
use intern::string_key::Intern; | ||
use isograph_lang_types::{ | ||
IsographSelectionVariant, ScalarFieldSelection, Selection, ServerFieldSelection, | ||
}; | ||
use isograph_schema::{ | ||
ClientField, ClientFieldVariant, FieldDefinitionLocation, ObjectTypeAndFieldNames, | ||
UnvalidatedClientField, UnvalidatedSchema, UnvalidatedSchemaObject, REFETCH_FIELD_NAME, | ||
}; | ||
|
||
use crate::batch_compile::BatchCompileError; | ||
|
||
pub fn add_refetch_fields_to_objects( | ||
schema: &mut UnvalidatedSchema, | ||
) -> Result<(), BatchCompileError> { | ||
'objects: for object in schema.server_field_data.server_objects.iter_mut() { | ||
if object.id_field.is_none() { | ||
continue 'objects; | ||
} | ||
|
||
if let Some(value) = add_refetch_field_to_object(object, &mut schema.client_fields) { | ||
return value; | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
fn add_refetch_field_to_object( | ||
object: &mut UnvalidatedSchemaObject, | ||
client_fields: &mut Vec<UnvalidatedClientField>, | ||
) -> Option<Result<(), BatchCompileError>> { | ||
match object | ||
.encountered_fields | ||
.entry((*REFETCH_FIELD_NAME).into()) | ||
{ | ||
Entry::Occupied(_) => return Some(Err(BatchCompileError::DuplicateRefetchField)), | ||
Entry::Vacant(vacant_entry) => { | ||
let next_client_field_id = client_fields.len().into(); | ||
|
||
vacant_entry.insert(FieldDefinitionLocation::Client(next_client_field_id)); | ||
|
||
let id_field_selection = WithSpan::new( | ||
Selection::ServerField(ServerFieldSelection::ScalarField(ScalarFieldSelection { | ||
name: WithLocation::new("id".intern().into(), Location::generated()), | ||
reader_alias: None, | ||
normalization_alias: None, | ||
associated_data: IsographSelectionVariant::Regular, | ||
unwraps: vec![], | ||
arguments: vec![], | ||
directives: vec![], | ||
})), | ||
Span::todo_generated(), | ||
); | ||
client_fields.push(ClientField { | ||
description: Some( | ||
format!("A refetch field for the {} type.", object.name) | ||
.intern() | ||
.into(), | ||
), | ||
name: (*REFETCH_FIELD_NAME).into(), | ||
id: next_client_field_id, | ||
selection_set_and_unwraps: Some((vec![id_field_selection], vec![])), | ||
variant: ClientFieldVariant::RefetchField, | ||
variable_definitions: vec![], | ||
type_and_field: ObjectTypeAndFieldNames { | ||
type_name: object.name, | ||
field_name: "__refetch".intern().into(), | ||
}, | ||
parent_object_id: object.id, | ||
}); | ||
|
||
object.client_field_ids.push(next_client_field_id); | ||
} | ||
} | ||
None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters