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

View File

@ -2,7 +2,7 @@ use std::any::Any;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter}; use std::fmt::{self, Debug, Formatter};
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer, Serialize};
use crate::{Data, ParseRequestError, UploadValue, Value, Variables}; 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 /// 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`). /// variables. The names are all in `camelCase` (e.g. `operationName`).
#[derive(Deserialize)] #[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Request { pub struct Request {
/// The query source of the 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}; use crate::{CacheControl, Result, ServerError, Value};
/// Query response /// Query response
#[derive(Debug, Default, Serialize)] #[derive(Debug, Default, Serialize, Deserialize)]
pub struct Response { pub struct Response {
/// Data of query result /// Data of query result
#[serde(default)]
pub data: Value, pub data: Value,
/// Extensions result /// Extensions result
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none", default)]
pub extensions: Option<Value>, pub extensions: Option<Value>,
/// Cache control value /// Cache control value
@ -17,7 +18,7 @@ pub struct Response {
pub cache_control: CacheControl, pub cache_control: CacheControl,
/// Errors /// Errors
#[serde(skip_serializing_if = "Vec::is_empty")] #[serde(skip_serializing_if = "Vec::is_empty", default)]
pub errors: Vec<ServerError>, pub errors: Vec<ServerError>,
} }