Derive Serialize for Request and Deserialize for Response.

This commit is contained in:
Sunli 2020-10-23 08:26:48 +08:00
parent 464644e9df
commit 3bd3de3d09
3 changed files with 14 additions and 13 deletions

View File

@ -2,13 +2,13 @@ use std::collections::BTreeMap;
use std::fmt::{self, Debug, Display, Formatter};
use std::marker::PhantomData;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use crate::{parser, InputValueType, Pos, Value};
/// Extensions to the error.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Default)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(transparent)]
pub struct ErrorExtensionValues(BTreeMap<String, Value>);
@ -20,18 +20,18 @@ impl ErrorExtensionValues {
}
/// An error in a GraphQL server.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ServerError {
/// An explanatory message of the error.
pub message: String,
/// Where the error occurred.
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub locations: Vec<Pos>,
/// If the error occurred in a resolver, the path to the error.
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub path: Vec<PathSegment>,
/// Extensions to the error.
#[serde(skip_serializing_if = "error_extensions_is_empty")]
#[serde(skip_serializing_if = "error_extensions_is_empty", default)]
pub extensions: Option<ErrorExtensionValues>,
}
@ -96,7 +96,7 @@ impl From<parser::Error> for ServerError {
///
/// This is like [`QueryPathSegment`](enum.QueryPathSegment.html), but owned and used as a part of
/// errors instead of during execution.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PathSegment {
/// A field in an object.

View File

@ -2,7 +2,7 @@ use std::any::Any;
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};
use serde::{Deserialize, Deserializer};
use serde::{Deserialize, Deserializer, Serialize};
use crate::{Data, ParseRequestError, UploadValue, Value, Variables};
@ -10,7 +10,7 @@ use crate::{Data, ParseRequestError, UploadValue, Value, Variables};
///
/// This can be deserialized from a structure of the query string, the operation name and the
/// variables. The names are all in `camelCase` (e.g. `operationName`).
#[derive(Deserialize)]
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Request {
/// The query source of the request.

View File

@ -1,15 +1,16 @@
use serde::Serialize;
use serde::{Deserialize, Serialize};
use crate::{CacheControl, Result, ServerError, Value};
/// Query response
#[derive(Debug, Default, Serialize)]
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct Response {
/// Data of query result
#[serde(default)]
pub data: Value,
/// Extensions result
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(skip_serializing_if = "Option::is_none", default)]
pub extensions: Option<Value>,
/// Cache control value
@ -17,7 +18,7 @@ pub struct Response {
pub cache_control: CacheControl,
/// Errors
#[serde(skip_serializing_if = "Vec::is_empty")]
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub errors: Vec<ServerError>,
}