use core::fmt;
use gql::{GraphQLError, Object, Value};
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value as JsonValue};
#[derive(Clone, Debug, PartialEq)]
pub struct UploadBinding {
pub path: String,
pub upload: gql::Upload,
}
impl UploadBinding {
#[must_use]
pub fn new(path: impl Into<String>, upload: gql::Upload) -> Self {
Self {
path: path.into(),
upload,
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GraphQLRequest {
#[serde(default, skip_serializing_if = "String::is_empty")]
pub query: String,
#[serde(
default,
alias = "doc_id",
alias = "documentId",
alias = "queryId",
skip_serializing_if = "Option::is_none"
)]
pub id: Option<String>,
#[serde(
rename = "operationName",
default,
skip_serializing_if = "Option::is_none"
)]
pub operation_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub variables: Option<Map<String, JsonValue>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extensions: Option<Map<String, JsonValue>>,
#[serde(skip)]
pub uploads: Vec<UploadBinding>,
}
impl GraphQLRequest {
#[must_use]
pub fn new(query: impl Into<String>) -> Self {
Self {
query: query.into(),
id: None,
operation_name: None,
variables: None,
extensions: None,
uploads: Vec::new(),
}
}
#[must_use]
pub fn persisted(id: impl Into<String>) -> Self {
Self {
query: String::new(),
id: Some(id.into()),
operation_name: None,
variables: None,
extensions: None,
uploads: Vec::new(),
}
}
#[must_use]
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
#[must_use]
pub fn prepared_id(&self) -> Option<&str> {
if let Some(id) = self.id.as_deref().filter(|id| !id.is_empty()) {
return Some(id);
}
let extensions = self.extensions.as_ref()?;
for key in ["id", "doc_id", "documentId", "queryId", "hash"] {
if let Some(id) = extensions
.get(key)
.and_then(JsonValue::as_str)
.filter(|id| !id.is_empty())
{
return Some(id);
}
}
let persisted = extensions.get("persistedQuery")?.as_object()?;
for key in ["sha256Hash", "hash", "id", "queryId", "documentId"] {
if let Some(id) = persisted
.get(key)
.and_then(JsonValue::as_str)
.filter(|id| !id.is_empty())
{
return Some(id);
}
}
None
}
pub fn from_json_slice(body: &[u8]) -> Result<Self, RequestError> {
serde_json::from_slice(body)
.map_err(|error| RequestError::new(format!("Invalid GraphQL request: {error}")))
}
pub fn from_json_str(body: &str) -> Result<Self, RequestError> {
serde_json::from_str(body)
.map_err(|error| RequestError::new(format!("Invalid GraphQL request: {error}")))
}
#[must_use]
pub fn operation_name(mut self, name: impl Into<String>) -> Self {
self.operation_name = Some(name.into());
self
}
#[must_use]
pub fn variables(mut self, variables: Map<String, JsonValue>) -> Self {
self.variables = Some(variables);
self
}
#[must_use]
pub fn with_upload(mut self, path: impl Into<String>, upload: gql::Upload) -> Self {
self.uploads.push(UploadBinding::new(path, upload));
self
}
pub fn variables_object(&self) -> Result<Option<Object>, RequestError> {
let mut object = self
.variables
.as_ref()
.map(variables_to_object)
.transpose()?;
if self.uploads.is_empty() {
return Ok(object);
}
let Some(variables) = object.as_mut() else {
return Err(RequestError::new(
"A multipart upload was mapped into `variables`, but the request has no variables.",
));
};
for binding in &self.uploads {
apply_upload(variables, &binding.path, &binding.upload)?;
}
Ok(object)
}
pub fn as_gql<'a>(&'a self, variables: Option<&'a Object>) -> gql::Request<'a> {
gql::Request {
query: &self.query,
operation_name: self.operation_name.as_deref(),
variables,
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum RequestPayload {
Single(GraphQLRequest),
Batch(Vec<GraphQLRequest>),
}
impl RequestPayload {
#[cfg_attr(
feature = "tracing",
tracing::instrument(
skip_all,
level = "debug",
name = "suku.parse",
fields(body_bytes = body.len(), max_batch_size)
)
)]
pub fn from_json_slice(body: &[u8], max_batch_size: usize) -> Result<Self, RequestError> {
let is_batch = body
.iter()
.copied()
.find(|byte| !byte.is_ascii_whitespace())
.is_some_and(|byte| byte == b'[');
if is_batch {
let batch: Vec<GraphQLRequest> = serde_json::from_slice(body)
.map_err(|error| RequestError::new(format!("Invalid batch request: {error}")))?;
if batch.is_empty() {
return Err(RequestError::new(
"A batch request must contain at least one operation.",
));
}
if max_batch_size != 0 && batch.len() > max_batch_size {
return Err(RequestError::new(format!(
"A batch request may contain at most {max_batch_size} operations."
)));
}
Ok(Self::Batch(batch))
} else {
let request: GraphQLRequest = serde_json::from_slice(body)
.map_err(|error| RequestError::new(format!("Invalid GraphQL request: {error}")))?;
Ok(Self::Single(request))
}
}
#[must_use]
pub fn requests(&self) -> &[GraphQLRequest] {
match self {
Self::Single(request) => core::slice::from_ref(request),
Self::Batch(requests) => requests,
}
}
#[must_use]
pub const fn is_batch(&self) -> bool {
matches!(self, Self::Batch(_))
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RequestError {
message: String,
}
impl RequestError {
#[must_use]
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
}
}
#[must_use]
pub fn message(&self) -> &str {
&self.message
}
#[must_use]
pub fn into_response(self) -> gql::Response {
gql::Response::error(GraphQLError::new(self.message))
}
}
impl fmt::Display for RequestError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for RequestError {}
impl From<GraphQLError> for RequestError {
fn from(value: GraphQLError) -> Self {
Self::new(value.message().to_owned())
}
}
enum Trail<'a> {
Key(&'a str),
Index(usize),
}
fn render_trail(prefix: &str, trail: &[Trail<'_>]) -> String {
use core::fmt::Write as _;
let mut path = String::with_capacity(prefix.len() + trail.len() * 4);
path.push_str(prefix);
for segment in trail {
match segment {
Trail::Key(key) => {
path.push('.');
path.push_str(key);
}
Trail::Index(index) => {
let _ = write!(path, "[{index}]");
}
}
}
path
}
fn variables_to_object(variables: &Map<String, JsonValue>) -> Result<Object, RequestError> {
let mut trail = Vec::new();
variables_to_object_at(variables, "", &mut trail)
}
fn variables_to_object_at<'a>(
variables: &'a Map<String, JsonValue>,
prefix: &str,
trail: &mut Vec<Trail<'a>>,
) -> Result<Object, RequestError> {
let mut object = Object::with_capacity(variables.len());
for (key, value) in variables {
trail.push(Trail::Key(key));
let converted = json_to_graphql_at(value, prefix, trail);
trail.pop();
object.push_unchecked(key.as_str(), converted?);
}
Ok(object)
}
fn apply_upload(
variables: &mut Object,
path: &str,
upload: &gql::Upload,
) -> Result<(), RequestError> {
let mut segments = path.split('.');
let Some("variables") = segments.next() else {
return Err(RequestError::new(format!(
"Upload path \"{path}\" must start with `variables`."
)));
};
let Some(name) = segments.next() else {
return Err(RequestError::new(format!(
"Upload path \"{path}\" does not name a variable."
)));
};
let value = variables
.get_mut(name)
.ok_or_else(|| missing_upload_target(path))?;
replace_upload_target(value, segments, path, upload)
}
fn replace_upload_target<'a>(
value: &mut Value,
mut segments: impl Iterator<Item = &'a str>,
path: &str,
upload: &gql::Upload,
) -> Result<(), RequestError> {
let Some(segment) = segments.next() else {
if !value.is_null() {
return Err(RequestError::new(format!(
"Upload path \"{path}\" points at a value that is not `null`; the multipart specification requires files to be replaced with `null`."
)));
}
*value = Value::from(upload.clone());
return Ok(());
};
if segment.is_empty() {
return Err(RequestError::new(format!(
"Upload path \"{path}\" contains an empty segment."
)));
}
match value {
Value::Object(object) => {
let child = Object::get_mut(std::sync::Arc::make_mut(object), segment)
.ok_or_else(|| missing_upload_target(path))?;
replace_upload_target(child, segments, path, upload)
}
Value::List(list) => {
let index: usize = segment.parse().map_err(|_| {
RequestError::new(format!(
"Upload path \"{path}\" uses \"{segment}\" where a list index was expected."
))
})?;
let len = list.len();
let child = list.get_mut(index).ok_or_else(|| {
RequestError::new(format!(
"Upload path \"{path}\" refers to list index {index}, but the list has {len} elements."
))
})?;
replace_upload_target(child, segments, path, upload)
}
_ => Err(missing_upload_target(path)),
}
}
fn missing_upload_target(path: &str) -> RequestError {
RequestError::new(format!(
"Upload path \"{path}\" does not identify a `null` inside the request variables."
))
}
pub fn json_to_graphql(value: &JsonValue, path: &str) -> Result<Value, RequestError> {
let mut trail = Vec::new();
json_to_graphql_at(value, path, &mut trail)
}
fn json_to_graphql_at<'a>(
value: &'a JsonValue,
prefix: &str,
trail: &mut Vec<Trail<'a>>,
) -> Result<Value, RequestError> {
Ok(match value {
JsonValue::Null => Value::Null,
JsonValue::Bool(value) => Value::Boolean(*value),
JsonValue::Number(number) => {
if let Some(number) = number.as_i64() {
Value::integer(i128::from(number))
} else if let Some(number) = number.as_u64() {
Value::integer(i128::from(number))
} else if let Some(number) = number.as_f64() {
if number.is_finite() && number.fract() == 0.0 {
let integer = number as i128;
if integer as f64 == number {
Value::integer(integer)
} else {
Value::Float(number)
}
} else {
Value::Float(number)
}
} else {
return Err(RequestError::new(format!(
"Unsupported number at `{}`.",
render_trail(prefix, trail)
)));
}
}
JsonValue::String(value) => Value::String(value.as_str().into()),
JsonValue::Array(values) => {
let mut list = Vec::with_capacity(values.len());
for (index, value) in values.iter().enumerate() {
trail.push(Trail::Index(index));
let converted = json_to_graphql_at(value, prefix, trail);
trail.pop();
list.push(converted?);
}
Value::List(list)
}
JsonValue::Object(values) => Value::Object(std::sync::Arc::new(variables_to_object_at(
values, prefix, trail,
)?)),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parses_operation_name_alias() {
let request: GraphQLRequest =
serde_json::from_str(r#"{"query":"{ hello }","operationName":"Greeting"}"#)
.expect("valid request");
assert_eq!(request.operation_name.as_deref(), Some("Greeting"));
}
#[test]
fn preserves_64_bit_integers_as_bigints() {
let request: GraphQLRequest =
serde_json::from_str(r#"{"query":"{ echo(n: 1) }","variables":{"n":10000000000}}"#)
.expect("valid request");
let object = request
.variables_object()
.expect("variables")
.expect("object");
assert_eq!(object.get("n"), Some(&Value::integer(10_000_000_000)));
}
#[test]
fn rejects_empty_batches() {
let error = RequestPayload::from_json_slice(b"[]", 10).expect_err("empty batch");
assert!(error.message().contains("at least one"));
}
}