graphql: Add schema AST helpers for resolving fields
@@ -6,6 +6,7 @@ use std::ops::Deref;
use std::str::FromStr;
use crate::execution::ObjectOrInterface;
+use crate::query::ast as qast;
use graph::prelude::ValueType;
pub(crate) enum FilterOp {
@@ -434,3 +435,25 @@ pub fn get_input_object_definitions(schema: &Document) -> Vec<InputObjectType> {
})
.collect()
}
+
+/// If the field has a `@derivedFrom(field: "foo")` directive, obtain the
+/// name of the field (e.g. `"foo"`)
+fn get_derived_from_directive<'a>(field_definition: &Field) -> Option<&Directive> {
+ field_definition
+ .directives
+ .iter()
+ .find(|directive| directive.name == Name::from("derivedFrom"))
+}
+
+pub fn get_derived_from_field<'a>(
+ object_type: impl Into<ObjectOrInterface<'a>>,
+ field_definition: &'a Field,
+) -> Option<&'a Field> {
+ get_derived_from_directive(field_definition)
+ .and_then(|directive| qast::get_argument_value(&directive.arguments, &Name::from("field")))
+ .and_then(|value| match value {
+ Value::String(s) => Some(s),
+ _ => None,
+ })
+ .and_then(|derived_from_field_name| get_field_type(object_type, derived_from_field_name))
+}
