Skip to content

Commit

Permalink
feat: merge after normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanmylee committed Dec 18, 2023
1 parent c9e3b56 commit c4ab793
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
70 changes: 69 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde_json::json;
mod builder;
mod graphql;
mod gson;
mod merge_utils;
mod omit;
mod parser_util;
mod resolve;
Expand Down Expand Up @@ -69,7 +70,74 @@ fn resolve(

#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {}
mod tests {
use super::*;

#[pg_test]
fn test_resolve() {
Spi::connect(|mut client| {
let _ = client.update(
"\
comment on schema public is '@graphql({\"inflect_names\": true})';
create table blog_post (
id int primary key,
a text,
b text,
c text,
d text,
e text,
f text
);
insert into blog_post values (1, 'a', 'b', 'c', 'd', 'e', 'f');
",
None,
None,
);
});

let result = resolve(
"\
query {
blogPostCollection {
edges {
cursor
node {
a
...c_query
... @include(if: true) {
e
}
}
}
}
blogPostCollection {
edges {
node {
b
...d_query
... @include(if: true) {
f
}
}
}
}
}
fragment c_query on BlogPost {
c
}
fragment d_query on BlogPost {
d
}
",
None,
None,
None,
);
println!("received the result: {result:#?}");
}
}

#[cfg(test)]
pub mod pg_test {
Expand Down
14 changes: 8 additions & 6 deletions src/merge_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use graphql_parser::query::*;
use itertools::Itertools;
use std::collections::HashMap;

use crate::parser_util::alias_or_name;
use crate::parser_util::{alias_or_name, normalize_selection_set};

pub fn merged_normalized_fields<'a, T, I>(fields: I) -> Result<Vec<Field<'a, T>>, String>
where
Expand All @@ -18,11 +18,10 @@ where
.push(field);
}

let merged_fields: Vec<_> = fields_by_name
let (merged_fields, errors): (Vec<_>, Vec<_>) = fields_by_name
.into_values()
.map(merged_matching_fields)
.collect();
let (merged_fields, errors): (Vec<_>, Vec<_>) = merged_fields.into_iter().partition_result();
.partition_result();

if !errors.is_empty() {
return Err(errors.join("\n"));
Expand Down Expand Up @@ -56,13 +55,16 @@ where
.into_iter()
.filter_map(|item| match item {
Selection::Field(field) => Some(field),
_ => None, // Fields should already be normalized so no other type of selections should exist.
_ => {
println!("received a non-normalized subfield");
None
},
});

let merged_subfields = merged_normalized_fields(subfields)?;
merged_field.selection_set.items = merged_subfields
.into_iter()
.map(|field| Selection::Field(field))
.map(Selection::Field)
.collect();

Ok(merged_field)
Expand Down
3 changes: 2 additions & 1 deletion src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ where
}
};

println!("selections {selections:#?}");

let selections = match merged_normalized_fields(selections) {
Ok(selections) => selections,
Err(err) => {
Expand Down Expand Up @@ -402,7 +404,6 @@ where
}
};


use pgrx::prelude::*;

let spi_result: Result<serde_json::Value, String> = Spi::connect(|mut conn| {
Expand Down

0 comments on commit c4ab793

Please sign in to comment.