Skip to content

Commit

Permalink
feat: Include Spanning in Arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
audunhalland committed Nov 3, 2023
1 parent d0f50e7 commit ead9d90
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 10 deletions.
6 changes: 3 additions & 3 deletions juniper/src/schema/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
schema::model::SchemaType,
types::base::TypeKind,
value::{DefaultScalarValue, ParseScalarValue},
FieldError,
FieldError, Spanning,
};

/// Whether an item is deprecated, with context.
Expand Down Expand Up @@ -202,7 +202,7 @@ pub struct Argument<'a, S> {
#[doc(hidden)]
pub arg_type: Type<'a>,
#[doc(hidden)]
pub default_value: Option<InputValue<S>>,
pub default_value: Option<Spanning<InputValue<S>>>,
}

impl<'a, S> Argument<'a, S> {
Expand Down Expand Up @@ -739,7 +739,7 @@ impl<'a, S> Argument<'a, S> {
/// Overwrites any previously set default value.
#[must_use]
pub fn default_value(mut self, val: InputValue<S>) -> Self {
self.default_value = Some(val);
self.default_value = Some(Spanning::unlocated(val));
self
}
}
Expand Down
2 changes: 1 addition & 1 deletion juniper/src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl<'a, S: ScalarValue + 'a> Argument<'a, S> {

#[graphql(name = "defaultValue")]
fn default_value_(&self) -> Option<String> {
self.default_value.as_ref().map(ToString::to_string)
self.default_value.as_ref().map(|v| v.item.to_string())
}
}

Expand Down
2 changes: 1 addition & 1 deletion juniper/src/schema/translate/graphql_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl GraphQLParserTranslator {
default_value: input
.default_value
.as_ref()
.map(|x| GraphQLParserTranslator::translate_value(x)),
.map(|x| GraphQLParserTranslator::translate_value(&x.item)),
directives: vec![],
}
}
Expand Down
7 changes: 6 additions & 1 deletion juniper/src/types/async_await.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,12 @@ where
m.item
.iter()
.filter_map(|(k, v)| {
v.item.clone().into_const(exec_vars).map(|v| (k.item, v))
let value = v.item.clone().into_const(exec_vars)?;
Some((k.item, Spanning {
item: value,
start: v.start,
end: v.end,
}))
})
.collect()
}),
Expand Down
19 changes: 16 additions & 3 deletions juniper/src/types/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ pub enum TypeKind {
/// Field argument container
#[derive(Debug)]
pub struct Arguments<'a, S = DefaultScalarValue> {
args: Option<IndexMap<&'a str, InputValue<S>>>,
args: Option<IndexMap<&'a str, Spanning<InputValue<S>>>>,
}

impl<'a, S> Arguments<'a, S> {
#[doc(hidden)]
pub fn new(
mut args: Option<IndexMap<&'a str, InputValue<S>>>,
mut args: Option<IndexMap<&'a str, Spanning<InputValue<S>>>>,
meta_args: &'a Option<Vec<Argument<S>>>,
) -> Self
where
Expand Down Expand Up @@ -117,10 +117,18 @@ impl<'a, S> Arguments<'a, S> {
self.args
.as_ref()
.and_then(|args| args.get(name))
.map(|spanning| &spanning.item)
.map(InputValue::convert)
.transpose()
.map_err(IntoFieldError::into_field_error)
}

/// Gets a direct reference to the spanned argument input value
pub fn get_input_value(&self, name: &str) -> Option<&Spanning<InputValue<S>>> {
self.args
.as_ref()
.and_then(|args| args.get(name))
}
}

/// Primary trait used to resolve GraphQL values.
Expand Down Expand Up @@ -473,7 +481,12 @@ where
m.item
.iter()
.filter_map(|(k, v)| {
v.item.clone().into_const(exec_vars).map(|v| (k.item, v))
let value = v.item.clone().into_const(exec_vars)?;
Some((k.item, Spanning {
item: value,
start: v.start,
end: v.end,
}))
})
.collect()
}),
Expand Down
7 changes: 6 additions & 1 deletion juniper/src/types/subscriptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,12 @@ where
m.item
.iter()
.filter_map(|(k, v)| {
v.item.clone().into_const(exec_vars).map(|v| (k.item, v))
let value = v.item.clone().into_const(exec_vars)?;
Some((k.item, Spanning {
item: value,
start: v.start,
end: v.end,
}))
})
.collect()
}),
Expand Down

0 comments on commit ead9d90

Please sign in to comment.