with scalars

This commit is contained in:
2023-01-29 13:29:11 +01:00
parent 3263f1d589
commit 0d6e6e57ae
6 changed files with 141 additions and 7 deletions

View File

@@ -1,9 +1,12 @@
use std::sync::Arc;
use graphql_introspection_query::introspection_response::{
FullType, IntrospectionResponse, Schema,
};
use itertools::Itertools;
use crate::{
handlers::{DynHandler, Handlers},
handlers::{scalar::Scalar, DynHandler, Handlers},
predicates::is_custom_scalar_type,
};
@@ -14,7 +17,9 @@ pub struct CodeGeneration {
impl CodeGeneration {
pub fn new() -> Self {
Self { handlers: vec![] }
Self {
handlers: vec![Arc::new(Scalar {})],
}
}
pub fn generate(&self, schema: &IntrospectionResponse) -> eyre::Result<String> {
@@ -34,8 +39,34 @@ impl CodeGeneration {
let types = get_types(schema)?;
//let remaining: Vec<Option<String>> = types.into_iter().map(type_name).collect();
//
for (handler, types) in self.group_by_handlers(&types) {
for t in types {
if let Some(_) = self.type_name(&t) {
let rendered = handler.render(&t)?;
output.push_str(&rendered)
}
}
}
todo!()
Ok(output)
}
pub fn group_by_handlers(&self, types: &Vec<&FullType>) -> Vec<(DynHandler, Vec<FullType>)> {
let mut group = vec![];
for handler in self.handlers.iter() {
let mut group_types: Vec<FullType> = vec![];
for t in types.iter() {
if handler.predicate(*t) {
group_types.push(t.clone().clone());
}
}
group.push((handler.clone(), group_types))
}
group
}
pub fn type_name(&self, t: &FullType) -> Option<String> {

View File

@@ -1,3 +1,5 @@
pub mod scalar;
use std::sync::Arc;
use genco::prelude::rust::Tokens;
@@ -25,7 +27,7 @@ pub trait Handler {
let name = t.name.as_ref().ok_or(eyre::anyhow!("name not found"))?;
Ok(quote! {
pub $name {} {
pub struct $name {} {
// TODO: Add fields
}
})
@@ -35,7 +37,7 @@ pub trait Handler {
let name = t.name.as_ref().ok_or(eyre::anyhow!("name not found"))?;
Ok(quote! {
pub $name {} {
impl $name {} {
// TODO: Add fields
}
})
@@ -48,6 +50,7 @@ pub type Handlers = Vec<DynHandler>;
#[cfg(test)]
mod tests {
use graphql_introspection_query::introspection_response::FullType;
use pretty_assertions::assert_eq;
use super::Handler;
@@ -73,8 +76,8 @@ mod tests {
assert_eq!(
res,
"
pub SomeName {} { }
pub SomeName {} { }
pub struct SomeName {} { }
impl SomeName {} { }
"
.to_string()
);

View File

@@ -0,0 +1,14 @@
use genco::Tokens;
use graphql_introspection_query::introspection_response::FullType;
use crate::predicates::is_custom_scalar_type;
use super::Handler;
pub struct Scalar;
impl Handler for Scalar {
fn predicate(&self, t: &FullType) -> bool {
is_custom_scalar_type(t)
}
}

View File

@@ -9,6 +9,8 @@ mod tests {
use crate::codegen::CodeGeneration;
use pretty_assertions::assert_eq;
#[test]
fn can_generate_from_schema() {
let schema: IntrospectionResponse = serde_json::from_str(INTROSPECTION_QUERY).unwrap();