use std::{marker::PhantomData, sync::Arc};
use gql::{GraphQLError, Name, Object, TypeRef, Value};
use crate::{
registry::TypeRegistry,
traits::{IntoValue, OutputType},
};
pub struct Raw<T> {
value: Value,
marker: PhantomData<fn() -> T>,
}
impl<T> Clone for Raw<T> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
marker: PhantomData,
}
}
}
impl<T> PartialEq for Raw<T> {
fn eq(&self, other: &Self) -> bool {
self.value == other.value
}
}
impl<T> Raw<T> {
#[must_use]
pub fn new(value: Value) -> Self {
Self {
value: strip_internal_keys(value),
marker: PhantomData,
}
}
#[must_use]
pub fn object(object: Object) -> Self {
Self::new(Value::Object(Arc::new(object)))
}
#[must_use]
pub fn null() -> Self {
Self::new(Value::Null)
}
#[must_use]
pub fn raw_value(&self) -> &Value {
&self.value
}
#[must_use]
pub fn into_raw(self) -> Value {
self.value
}
}
impl<T> Raw<Vec<T>> {
#[must_use]
pub fn list(items: impl IntoIterator<Item = Raw<T>>) -> Self {
Self::new(Value::List(
items.into_iter().map(|item| item.value).collect(),
))
}
}
impl<T> From<Value> for Raw<T> {
fn from(value: Value) -> Self {
Self::new(value)
}
}
impl<T> From<Object> for Raw<T> {
fn from(object: Object) -> Self {
Self::object(object)
}
}
impl<T> std::fmt::Debug for Raw<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Raw").field("value", &self.value).finish()
}
}
impl<T: OutputType> OutputType for Raw<T> {
fn output_type() -> TypeRef {
T::output_type()
}
fn is_optional() -> bool {
T::is_optional()
}
fn register_output(registry: &mut TypeRegistry) {
T::register_output(registry);
}
}
impl<T> IntoValue for Raw<T> {
fn into_value(self) -> Result<Value, GraphQLError> {
Ok(self.value)
}
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct RawObject {
object: Object,
}
impl RawObject {
#[must_use]
pub fn new() -> Self {
Self {
object: Object::new(),
}
}
#[must_use]
pub fn with_capacity(capacity: usize) -> Self {
Self {
object: Object::with_capacity(capacity),
}
}
#[must_use]
pub fn field(mut self, name: impl Into<Name>, value: impl Into<Value>) -> Self {
self.insert(name, value);
self
}
pub fn insert(&mut self, name: impl Into<Name>, value: impl Into<Value>) -> Option<Value> {
let name = name.into();
if is_internal_key(&name) {
return None;
}
self.object.insert(name, strip_internal_keys(value.into()))
}
#[must_use]
pub fn typename(mut self, name: impl Into<Name>) -> Self {
self.insert("__typename", Value::String(name.into()));
self
}
#[must_use]
pub fn without(mut self, name: &str) -> Self {
self.object.remove(name);
self
}
#[must_use]
pub fn get(&self, name: &str) -> Option<&Value> {
self.object.get(name)
}
#[must_use]
pub fn len(&self) -> usize {
self.object.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.object.is_empty()
}
#[must_use]
pub fn build(self) -> Object {
self.object
}
#[must_use]
pub fn into_value(self) -> Value {
Value::Object(Arc::new(self.object))
}
#[must_use]
pub fn into_raw<T>(self) -> Raw<T> {
Raw::object(self.object)
}
}
impl From<RawObject> for Value {
fn from(value: RawObject) -> Self {
value.into_value()
}
}
impl From<RawObject> for Object {
fn from(value: RawObject) -> Self {
value.object
}
}
const INTERNAL_PREFIX: &str = "__gql_";
fn is_internal_key(name: &str) -> bool {
name.starts_with(INTERNAL_PREFIX)
}
fn contains_internal_key(value: &Value) -> bool {
match value {
Value::Object(object) => {
object.keys().any(|key| is_internal_key(key))
|| object.values().any(contains_internal_key)
}
Value::List(items) => items.iter().any(contains_internal_key),
_ => false,
}
}
fn strip_internal_keys(value: Value) -> Value {
if !contains_internal_key(&value) {
return value;
}
match value {
Value::Object(object) => {
let mut clean = Object::with_capacity(object.len());
let object = Arc::try_unwrap(object).unwrap_or_else(|shared| (*shared).clone());
for (key, value) in object.into_fields() {
if !is_internal_key(&key) {
clean.push_unchecked(key, strip_internal_keys(value));
}
}
Value::Object(Arc::new(clean))
}
Value::List(items) => Value::List(items.into_iter().map(strip_internal_keys).collect()),
other => other,
}
}