diff --git a/src/core/jit/graphql_error.rs b/src/core/jit/graphql_error.rs index b6be9f6834..6d2e0a7132 100644 --- a/src/core/jit/graphql_error.rs +++ b/src/core/jit/graphql_error.rs @@ -15,7 +15,7 @@ pub struct GraphQLError { pub locations: Vec, /// If the error occurred in a resolver, the path to the error. #[serde(skip_serializing_if = "Vec::is_empty", default)] - pub path: Vec, + pub path: Vec>, /// Extensions to the error. #[serde(skip_serializing_if = "error_extensions_is_empty", default)] pub extensions: Option, @@ -75,7 +75,7 @@ impl GraphQLError { #[doc(hidden)] #[must_use] - pub fn with_path(self, path: Vec) -> Self { + pub fn with_path(self, path: Vec>) -> Self { Self { path, ..self } } } diff --git a/src/core/jit/model.rs b/src/core/jit/model.rs index 8ccff728ff..13dfcbb7d3 100644 --- a/src/core/jit/model.rs +++ b/src/core/jit/model.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::collections::HashMap; use std::fmt::{Debug, Formatter}; use std::sync::Arc; @@ -505,36 +506,27 @@ impl From for async_graphql::Pos { #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] #[serde(untagged)] -pub enum PathSegment { +pub enum PathSegment<'a> { /// A field in an object. - Field(String), + Field(Cow<'a, String>), /// An index in a list. Index(usize), } -impl From for PathSegment { +impl From for PathSegment<'static> { fn from(value: async_graphql::PathSegment) -> Self { match value { - async_graphql::PathSegment::Field(field) => PathSegment::Field(field), + async_graphql::PathSegment::Field(field) => PathSegment::Field(Cow::Owned(field)), async_graphql::PathSegment::Index(index) => PathSegment::Index(index), } } } -impl From for async_graphql::PathSegment { - fn from(val: PathSegment) -> Self { - match val { - PathSegment::Field(field) => async_graphql::PathSegment::Field(field), - PathSegment::Index(index) => async_graphql::PathSegment::Index(index), - } - } -} - #[derive(Debug, Serialize, Clone)] pub struct Positioned { pub value: Value, pub pos: Pos, - pub path: Vec, + pub path: Vec>, } impl Positioned { @@ -547,7 +539,7 @@ impl Positioned where Value: Clone, { - pub fn with_path(&mut self, path: Vec) -> Self { + pub fn with_path(&mut self, path: Vec>) -> Self { Self { value: self.value.clone(), pos: self.pos, path } } } diff --git a/src/core/jit/synth/synth.rs b/src/core/jit/synth/synth.rs index 06960aa91c..53b2234918 100644 --- a/src/core/jit/synth/synth.rs +++ b/src/core/jit/synth/synth.rs @@ -59,10 +59,10 @@ where node: &'a Field, value: Option<&'a Value>, data_path: &DataPath, - path: &mut Vec, + path: &mut Vec>, root_name: Option<&'a str>, ) -> Result> { - path.push(PathSegment::Field(node.output_name.clone())); + path.push(PathSegment::Field(Cow::Borrowed(&node.output_name))); let result = match self.store.get(&node.id) { Some(value) => { @@ -120,7 +120,7 @@ where node: &'a Field, value: &'a Value, data_path: &DataPath, - path: &mut Vec, + path: &mut Vec>, ) -> Result> { // skip the field if field is not included in schema if !self.include(node) { @@ -222,7 +222,16 @@ where node: &'a Field, path: &[PathSegment], ) -> Positioned { - Positioned::new(error, node.pos).with_path(path.to_vec()) + Positioned::new(error, node.pos).with_path( + path.iter() + .map(|x| match x { + PathSegment::Field(cow) => { + PathSegment::Field(Cow::Owned(cow.clone().into_owned())) + } + PathSegment::Index(i) => PathSegment::Index(*i), + }) + .collect(), + ) } }