use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct GraphQLErrorLocation {
pub line: u32,
pub column: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphQLError {
pub message: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub locations: Option<Vec<GraphQLErrorLocation>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub path: Option<Vec<serde_json::Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub extensions: Option<HashMap<String, serde_json::Value>>,
}
impl GraphQLError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
locations: None,
path: None,
extensions: None,
}
}
#[must_use]
pub fn with_code(message: impl Into<String>, code: impl Into<String>) -> Self {
let mut extensions = HashMap::new();
extensions.insert("code".to_string(), serde_json::json!(code.into()));
Self {
message: message.into(),
locations: None,
path: None,
extensions: Some(extensions),
}
}
#[must_use]
pub fn with_location(mut self, line: u32, column: u32) -> Self {
let loc = GraphQLErrorLocation { line, column };
self.locations.get_or_insert_with(Vec::new).push(loc);
self
}
#[must_use]
pub fn with_path(mut self, path: Vec<serde_json::Value>) -> Self {
self.path = Some(path);
self
}
}