Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add (entries: true) argument support #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ mod filters {
use serde_json::{ Value };
use serde_json::map::Map;
use graphql_parser::query::*;
use graphql_parser::query::Value as GValue;

#[derive(Clone, Debug)]
pub enum Filters {
Field(String),
Object(String, Vec<Filters>)
Object(String, Vec<Filters>),
Entries(String, Vec<Filters>),
}

// TODO: Fail when requested fields do not exist
Expand All @@ -99,6 +101,23 @@ mod filters {
map.insert(field, filter_value(fields, value.clone()));
}
}
Filters::Entries(field, fields) => {
if let Some(value) = object.get(&field) {
if let Value::Object(ref items) = *value {
let array = filter_array(fields,
items.iter().map(|(k, v)| {
Value::Object(vec![
("key".into(), Value::String(k.clone())),
("value".into(), v.clone()),
].into_iter().collect())
}).collect());
map.insert(field, Value::Array(array));
} else {
map.insert(field,
filter_value(fields, value.clone()));
}
}
}
}
}

Expand All @@ -122,8 +141,30 @@ mod filters {
selection.items.iter()
.filter_map(|selection| {
if let Selection::Field(field) = selection.clone() {
if field.selection_set.items.len() > 0 {
Some(Filters::Object(field.name, get_filters(field.selection_set)))
let subfilters = get_filters(field.selection_set);
if field.arguments.len() > 0 {
for argument in &field.arguments {
match &argument.0[..] {
"entries" => {
match argument.1 {
GValue::Boolean(true) => {
return Some(Filters::Entries(
field.name,
subfilters));
}
GValue::Boolean(false) => {}
_ => {
panic!("invalid argument {:?}",
argument);
}
}
}
_ => panic!("invalid argument {:?}", argument),
}
}
}
if subfilters.len() > 0 {
Some(Filters::Object(field.name, subfilters))
} else {
Some(Filters::Field(field.name))
}
Expand Down Expand Up @@ -187,7 +228,7 @@ mod filters {
pub fn filter_stream<'de, R>(query: String, reader: R, func: fn(Value)) where R: Read<'de> {
// Parse query string to AST
match parse_query(&query) {
Err(error) => panic!(error),
Err(error) => panic!("Bad query: {}", error),
Ok(ast) => {
// Convert AST to selection tree
let selection = filters::get_selection(ast);
Expand Down Expand Up @@ -356,4 +397,19 @@ mod tests {

assert_eq!(super::filter_value(query, data).to_string(), expect);
}

#[test]
fn dictionary() {
let query = String::from("{ dict(entries: true) { value { name } } }");
let data = json!({
"dict": {
"item1": {"name": "item one"},
"item2": {"name": "item two"},
},
});

let expect = r#"{"dict":[{"value":{"name":"item one"}},{"value":{"name":"item two"}}]}"#;

assert_eq!(super::filter_value(query, data).to_string(), expect);
}
}