-
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.
feat: implement infrastructure for separation of scalar types (#40)
* chore: define ScalarTypeConfig * chore: pass ScalarTypeConfig * refactor: introduce TypeTarget * test: fix ScalarTypeConfig related test * refactor: remove impl TypePrinter for TypeSystemDocument * refactor: introduce TypeTarget in SchemaTypePrinterContext * feat: output four namespaces * feat: do not emit unneeded types * feat: use OperationOutput / OperationInput from operation_type_printer * feat: implement server-side separation
- Loading branch information
Showing
56 changed files
with
1,419 additions
and
545 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,60 @@ | ||
use serde::Deserialize; | ||
|
||
use crate::TypeTarget; | ||
|
||
/// Representation of a scalar type's TypeScript type | ||
/// as defined in the config file. | ||
#[derive(Debug, Clone, PartialEq, Eq)] | ||
pub enum ScalarTypeConfig { | ||
/// Single specification for use in all situations. | ||
Single(String), | ||
} | ||
|
||
impl ScalarTypeConfig { | ||
/// Get the TypeScript type for given target. | ||
pub fn get_type(&self, _target: TypeTarget) -> &str { | ||
match self { | ||
ScalarTypeConfig::Single(type_name) => type_name, | ||
} | ||
} | ||
/// Get the TypeScript type as a resolver output type. | ||
pub fn as_resolver_output_type(&self) -> &str { | ||
match self { | ||
ScalarTypeConfig::Single(type_name) => type_name, | ||
} | ||
} | ||
/// Get the TypeScript type as a resolver input type. | ||
pub fn as_resolver_input_type(&self) -> &str { | ||
match self { | ||
ScalarTypeConfig::Single(type_name) => type_name, | ||
} | ||
} | ||
/// Get the TypeScript type as an operation output type. | ||
pub fn as_operation_output_type(&self) -> &str { | ||
match self { | ||
ScalarTypeConfig::Single(type_name) => type_name, | ||
} | ||
} | ||
/// Get the TypeScript type as an operation input type. | ||
pub fn as_operation_input_type(&self) -> &str { | ||
match self { | ||
ScalarTypeConfig::Single(type_name) => type_name, | ||
} | ||
} | ||
/// Returns an Iterator over all type names used in this config. | ||
pub fn type_names(&self) -> impl Iterator<Item = &str> { | ||
match self { | ||
ScalarTypeConfig::Single(type_name) => std::iter::once(type_name.as_str()), | ||
} | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for ScalarTypeConfig { | ||
fn deserialize<D>(deserializer: D) -> Result<ScalarTypeConfig, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let s = String::deserialize(deserializer)?; | ||
Ok(ScalarTypeConfig::Single(s)) | ||
} | ||
} |
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,40 @@ | ||
use std::fmt::Display; | ||
|
||
#[derive(Copy, Clone)] | ||
pub enum TypeTarget { | ||
OperationInput, | ||
OperationOutput, | ||
ResolverInput, | ||
ResolverOutput, | ||
} | ||
|
||
impl TypeTarget { | ||
/// Returns the string representation of self. | ||
/// String representation is prefixed with `__` for use in type definitions. | ||
pub fn as_str(&self) -> &'static str { | ||
match self { | ||
TypeTarget::OperationInput => "__OperationInput", | ||
TypeTarget::OperationOutput => "__OperationOutput", | ||
TypeTarget::ResolverInput => "__ResolverInput", | ||
TypeTarget::ResolverOutput => "__ResolverOutput", | ||
} | ||
} | ||
|
||
/// Returns whether self is an output target. | ||
pub fn is_output(&self) -> bool { | ||
matches!( | ||
self, | ||
TypeTarget::OperationOutput | TypeTarget::ResolverOutput | ||
) | ||
} | ||
/// Returns whether self is an input target. | ||
pub fn is_input(&self) -> bool { | ||
!self.is_output() | ||
} | ||
} | ||
|
||
impl Display for TypeTarget { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.as_str()) | ||
} | ||
} |
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
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
Oops, something went wrong.