Skip to content

Commit

Permalink
Merge pull request #25 from semiotic-ai/gustavo/sql-16-return-sqloutp…
Browse files Browse the repository at this point in the history
…ut-as-uniontype

feat: use union for sql output
  • Loading branch information
gusinacio authored Mar 1, 2024
2 parents 78f5edd + c0de994 commit 87f1c49
Show file tree
Hide file tree
Showing 20 changed files with 437 additions and 263 deletions.
47 changes: 43 additions & 4 deletions graph/src/data/graphql/ext.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::ObjectOrInterface;
use super::QueryableType;
use crate::prelude::s::{
Definition, Directive, Document, EnumType, Field, InterfaceType, ObjectType, Type,
TypeDefinition, Value,
TypeDefinition, UnionType, Value,
};
use crate::prelude::ENV_VARS;
use crate::schema::{META_FIELD_TYPE, SCHEMA_TYPE_NAME, SQL_FIELD_TYPE};
Expand Down Expand Up @@ -56,11 +56,33 @@ impl ObjectTypeExt for InterfaceType {
}
}

impl ObjectTypeExt for UnionType {
fn field(&self, _name: &str) -> Option<&Field> {
None
}

fn is_meta(&self) -> bool {
false
}

fn is_sql(&self) -> bool {
self.name == SQL_FIELD_TYPE
}

fn is_immutable(&self) -> bool {
false
}
}

pub trait DocumentExt {
fn get_object_type_definitions(&self) -> Vec<&ObjectType>;

fn get_interface_type_definitions(&self) -> Vec<&InterfaceType>;

fn get_union_definitions(&self) -> Vec<&UnionType>;

fn get_union_definition(&self, name: &str) -> Option<&UnionType>;

fn get_object_type_definition(&self, name: &str) -> Option<&ObjectType>;

fn get_object_and_interface_type_fields(&self) -> HashMap<&str, &Vec<Field>>;
Expand All @@ -75,7 +97,7 @@ pub trait DocumentExt {

fn get_root_subscription_type(&self) -> Option<&ObjectType>;

fn object_or_interface(&self, name: &str) -> Option<ObjectOrInterface<'_>>;
fn object_or_interface(&self, name: &str) -> Option<QueryableType<'_>>;

fn get_named_type(&self, name: &str) -> Option<&TypeDefinition>;

Expand Down Expand Up @@ -141,6 +163,22 @@ impl DocumentExt for Document {
.collect()
}

fn get_union_definitions(&self) -> Vec<&UnionType> {
self.definitions
.iter()
.filter_map(|d| match d {
Definition::TypeDefinition(TypeDefinition::Union(t)) => Some(t),
_ => None,
})
.collect()
}

fn get_union_definition(&self, name: &str) -> Option<&UnionType> {
self.get_union_definitions()
.into_iter()
.find(|object_type| object_type.name.eq(name))
}

fn find_interface(&self, name: &str) -> Option<&InterfaceType> {
self.definitions.iter().find_map(|d| match d {
Definition::TypeDefinition(TypeDefinition::Interface(t)) if t.name == name => Some(t),
Expand Down Expand Up @@ -195,10 +233,11 @@ impl DocumentExt for Document {
.next()
}

fn object_or_interface(&self, name: &str) -> Option<ObjectOrInterface<'_>> {
fn object_or_interface(&self, name: &str) -> Option<QueryableType<'_>> {
match self.get_named_type(name) {
Some(TypeDefinition::Object(t)) => Some(t.into()),
Some(TypeDefinition::Interface(t)) => Some(t.into()),
Some(TypeDefinition::Union(u)) => Some(u.into()),
_ => None,
}
}
Expand Down
4 changes: 2 additions & 2 deletions graph/src/data/graphql/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ pub mod shape_hash;

pub mod load_manager;

pub mod object_or_interface;
pub use object_or_interface::ObjectOrInterface;
pub mod queryable_type;
pub use queryable_type::QueryableType;

pub mod object_macro;
pub use crate::object;
Expand Down
144 changes: 0 additions & 144 deletions graph/src/data/graphql/object_or_interface.rs

This file was deleted.

Loading

0 comments on commit 87f1c49

Please sign in to comment.