thegraph: Implement conversion to GraphQL values for Entity and Value
-use std::collections::HashMap;
+use graphql_parser::query;
+use std::collections::{BTreeMap, HashMap};
use std::ops::{Deref, DerefMut};
/// An entity attribute name is represented as a string.
@@ -10,6 +11,14 @@ pub enum Value {
String(String),
}
+impl Into<query::Value> for Value {
+ fn into(self) -> query::Value {
+ match self {
+ Value::String(s) => query::Value::String(s.to_string()),
+ }
+ }
+}
+
/// An entity is represented as a map of attribute names to values.
#[derive(Clone, Debug)]
pub struct Entity(HashMap<Attribute, Value>);
@@ -34,3 +43,13 @@ impl DerefMut for Entity {
&mut self.0
}
}
+
+impl Into<query::Value> for Entity {
+ fn into(self) -> query::Value {
+ let mut fields = BTreeMap::new();
+ for (attr, value) in self.iter() {
+ fields.insert(attr.to_string(), value.clone().into());
+ }
+ query::Value::Object(fields)
+ }
+}
