Skip to content

Commit c89d8c3

Browse files
committed
graphql: Move ObjectCondition to graphql::schema::ast
1 parent 80b32ae commit c89d8c3

File tree

5 files changed

+54
-54
lines changed

5 files changed

+54
-54
lines changed

graphql/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub mod prelude {
3030
pub use super::introspection::{introspection_schema, IntrospectionResolver};
3131
pub use super::query::{execute_query, ext::BlockConstraint, QueryExecutionOptions};
3232
pub use super::schema::{api_schema, ast::is_list, ast::validate_entity, APISchemaError};
33-
pub use super::store::{build_query, StoreResolver};
33+
pub use super::store::StoreResolver;
3434
pub use super::subscription::SubscriptionExecutionOptions;
3535
pub use super::values::MaybeCoercible;
3636

graphql/src/schema/ast.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,48 @@ pub(crate) fn parse_field_as_filter(key: &str) -> (String, FilterOp) {
5151
(key.trim_end_matches(suffix).to_owned(), op)
5252
}
5353

54+
/// An `ObjectType` with `Hash` and `Eq` derived from the name.
55+
#[derive(Clone, Debug)]
56+
pub(crate) struct ObjectCondition<'a>(&'a s::ObjectType);
57+
58+
impl<'a> Ord for ObjectCondition<'a> {
59+
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
60+
self.0.name.cmp(&other.0.name)
61+
}
62+
}
63+
64+
impl<'a> PartialOrd for ObjectCondition<'a> {
65+
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
66+
Some(self.0.name.cmp(&other.0.name))
67+
}
68+
}
69+
70+
impl std::hash::Hash for ObjectCondition<'_> {
71+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
72+
self.0.name.hash(state)
73+
}
74+
}
75+
76+
impl PartialEq for ObjectCondition<'_> {
77+
fn eq(&self, other: &Self) -> bool {
78+
self.0.name.eq(&other.0.name)
79+
}
80+
}
81+
82+
impl Eq for ObjectCondition<'_> {}
83+
84+
impl<'a> From<&'a s::ObjectType> for ObjectCondition<'a> {
85+
fn from(object: &'a s::ObjectType) -> Self {
86+
ObjectCondition(object)
87+
}
88+
}
89+
90+
impl<'a> From<ObjectCondition<'a>> for ObjectOrInterface<'a> {
91+
fn from(cond: ObjectCondition<'a>) -> Self {
92+
ObjectOrInterface::Object(cond.0)
93+
}
94+
}
95+
5496
pub fn get_root_query_type_def(schema: &Document) -> Option<&TypeDefinition> {
5597
schema.definitions.iter().find_map(|d| match d {
5698
Definition::TypeDefinition(def @ TypeDefinition::Object(_)) => match def {

graphql/src/store/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ mod prefetch;
22
mod query;
33
mod resolver;
44

5-
pub use self::query::{build_query, parse_subgraph_id};
5+
pub use self::query::parse_subgraph_id;
66
pub use self::resolver::StoreResolver;

graphql/src/store/prefetch.rs

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ use graph::{
2727
use crate::execution::{ast as a, ExecutionContext, Resolver};
2828
use crate::runner::ResultSizeMetrics;
2929
use crate::schema::ast as sast;
30-
use crate::store::{build_query, StoreResolver};
30+
use crate::store::query::build_query;
31+
use crate::store::StoreResolver;
3132

3233
lazy_static! {
3334
static ref ARG_FIRST: String = String::from("first");
@@ -54,42 +55,6 @@ type GroupedFieldSet<'a> = IndexMap<&'a str, CollectedResponseKey<'a>>;
5455
/// attributes.
5556
type ComplementaryFields<'a> = BTreeMap<ObjectOrInterface<'a>, String>;
5657

57-
/// An `ObjectType` with `Hash` and `Eq` derived from the name.
58-
#[derive(Clone, Debug)]
59-
pub struct ObjectCondition<'a>(&'a s::ObjectType);
60-
61-
impl<'a> Ord for ObjectCondition<'a> {
62-
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
63-
self.0.name.cmp(&other.0.name)
64-
}
65-
}
66-
67-
impl<'a> PartialOrd for ObjectCondition<'a> {
68-
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
69-
Some(self.0.name.cmp(&other.0.name))
70-
}
71-
}
72-
73-
impl std::hash::Hash for ObjectCondition<'_> {
74-
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
75-
self.0.name.hash(state)
76-
}
77-
}
78-
79-
impl PartialEq for ObjectCondition<'_> {
80-
fn eq(&self, other: &Self) -> bool {
81-
self.0.name.eq(&other.0.name)
82-
}
83-
}
84-
85-
impl Eq for ObjectCondition<'_> {}
86-
87-
impl<'a> From<&'a s::ObjectType> for ObjectCondition<'a> {
88-
fn from(object: &'a s::ObjectType) -> Self {
89-
ObjectCondition(object)
90-
}
91-
}
92-
9358
/// Intermediate data structure to hold the results of prefetching entities
9459
/// and their nested associations. For each association of `entity`, `children`
9560
/// has an entry mapping the response key to the list of nodes.
@@ -717,7 +682,7 @@ fn execute_selection_set<'a>(
717682
struct CollectedResponseKey<'a> {
718683
iface_cond: Option<&'a s::InterfaceType>,
719684
iface_fields: Vec<&'a a::Field>,
720-
obj_types: IndexMap<ObjectCondition<'a>, Vec<&'a a::Field>>,
685+
obj_types: IndexMap<sast::ObjectCondition<'a>, Vec<&'a a::Field>>,
721686
collected_column_names: CollectedAttributeNames<'a>,
722687
}
723688

@@ -745,10 +710,7 @@ impl<'a> CollectedResponseKey<'a> {
745710
self.iface_fields.push(field);
746711
}
747712
ObjectOrInterface::Object(o) => {
748-
self.obj_types
749-
.entry(ObjectCondition(o))
750-
.or_default()
751-
.push(field);
713+
self.obj_types.entry(o.into()).or_default().push(field);
752714
}
753715
}
754716
}
@@ -776,11 +738,7 @@ impl<'a> IntoIterator for CollectedResponseKey<'a> {
776738
self.iface_cond
777739
.map(|cond| (ObjectOrInterface::Interface(cond), iface_fields))
778740
.into_iter()
779-
.chain(
780-
self.obj_types
781-
.into_iter()
782-
.map(|(c, f)| (ObjectOrInterface::Object(c.0), f)),
783-
),
741+
.chain(self.obj_types.into_iter().map(|(c, f)| (c.into(), f))),
784742
)
785743
}
786744
}
@@ -1057,7 +1015,7 @@ fn fetch(
10571015

10581016
/// Represents a finished column collection operation, mapping each object type to the final set of
10591017
/// selected SQL columns.
1060-
type AttributeNamesByObjectType<'a> = BTreeMap<ObjectCondition<'a>, AttributeNames>;
1018+
type AttributeNamesByObjectType<'a> = BTreeMap<sast::ObjectCondition<'a>, AttributeNames>;
10611019

10621020
#[derive(Debug, Default, Clone)]
10631021
struct CollectedAttributeNames<'a>(HashMap<ObjectOrInterface<'a>, AttributeNames>);
@@ -1110,12 +1068,12 @@ impl<'a> CollectedAttributeNames<'a> {
11101068

11111069
/// Helper function for handling insertion on the `AttributeNamesByObjectType` struct.
11121070
fn upsert(
1113-
map: &mut BTreeMap<ObjectCondition<'a>, AttributeNames>,
1071+
map: &mut BTreeMap<sast::ObjectCondition<'a>, AttributeNames>,
11141072
object: &'a s::ObjectType,
11151073
column_names: AttributeNames,
11161074
) {
11171075
use std::collections::btree_map::Entry;
1118-
let key = ObjectCondition(object);
1076+
let key = object.into();
11191077
let column_names = filter_derived_fields(column_names, object);
11201078
match map.entry(key) {
11211079
Entry::Occupied(mut entry) => {

graphql/src/store/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use graph::{components::store::EntityType, data::graphql::ObjectOrInterface};
77

88
use crate::execution::ast as a;
99
use crate::schema::ast as sast;
10-
use crate::store::prefetch::ObjectCondition;
10+
use crate::schema::ast::ObjectCondition;
1111

1212
#[derive(Debug)]
1313
enum OrderDirection {
@@ -18,7 +18,7 @@ enum OrderDirection {
1818
/// Builds a EntityQuery from GraphQL arguments.
1919
///
2020
/// Panics if `entity` is not present in `schema`.
21-
pub fn build_query<'a>(
21+
pub(crate) fn build_query<'a>(
2222
entity: impl Into<ObjectOrInterface<'a>>,
2323
block: BlockNumber,
2424
arguments: &HashMap<&str, r::Value>,

0 commit comments

Comments
 (0)