Skip to main content

fraiseql_server/routes/graphql/
request.rs

1//! GraphQL request and response types.
2
3use axum::{
4    Json,
5    response::{IntoResponse, Response},
6};
7use serde::{Deserialize, Serialize};
8
9/// GraphQL request payload (for POST requests).
10#[derive(Debug, Deserialize)]
11#[serde(rename_all = "camelCase")]
12pub struct GraphQLRequest {
13    /// GraphQL query string (optional when using APQ with hash-only request).
14    #[serde(default)]
15    pub query: Option<String>,
16
17    /// Query variables (optional).
18    #[serde(default)]
19    pub variables: Option<serde_json::Value>,
20
21    /// Operation name (optional).
22    #[serde(default)]
23    pub operation_name: Option<String>,
24
25    /// Protocol extensions (APQ, tracing, etc.).
26    #[serde(default)]
27    pub extensions: Option<serde_json::Value>,
28
29    /// Trusted document identifier (GraphQL over HTTP spec).
30    #[serde(default, rename = "documentId")]
31    pub document_id: Option<String>,
32}
33
34/// GraphQL GET request parameters.
35///
36/// Per GraphQL over HTTP spec, GET requests encode parameters in the query string:
37/// - `query`: Required, the GraphQL query string
38/// - `variables`: Optional, JSON-encoded object
39/// - `operationName`: Optional, name of the operation to execute
40#[derive(Debug, Deserialize)]
41#[serde(rename_all = "camelCase")]
42pub struct GraphQLGetParams {
43    /// GraphQL query string (required).
44    pub query: String,
45
46    /// Query variables as JSON-encoded string (optional).
47    #[serde(default)]
48    pub variables: Option<String>,
49
50    /// Operation name (optional).
51    #[serde(default)]
52    pub operation_name: Option<String>,
53}
54
55/// GraphQL response payload.
56#[derive(Debug, Serialize)]
57pub struct GraphQLResponse {
58    /// Response data or errors.
59    #[serde(flatten)]
60    pub body: serde_json::Value,
61}
62
63impl IntoResponse for GraphQLResponse {
64    fn into_response(self) -> Response {
65        Json(self.body).into_response()
66    }
67}