@@ -43,14 +43,16 @@ where
4343 pub query : & ' static str ,
4444}
4545
46- /// Represents a location inside a query string. Used in errors.
46+ /// Represents a location inside a query string. Used in errors. See [`GraphQLError`].
4747#[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
4848pub struct Location {
49- line : i32 ,
50- column : i32 ,
49+ /// The line number in the query string where the error originated (starting from 1).
50+ pub line : i32 ,
51+ /// The column number in the query string where the error originated (starting from 1).
52+ pub column : i32 ,
5153}
5254
53- /// Part of a path in a query. It can be an object key or an array index.
55+ /// Part of a path in a query. It can be an object key or an array index. See [`GraphQLError`].
5456#[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
5557#[ serde( untagged) ]
5658pub enum PathFragment {
@@ -65,6 +67,69 @@ pub enum PathFragment {
6567/// This tries to be as close to the spec as possible.
6668///
6769/// [Spec](https://github.com/facebook/graphql/blob/master/spec/Section%207%20--%20Response.md)
70+ ///
71+ ///
72+ /// ```
73+ /// # extern crate failure;
74+ /// # #[macro_use]
75+ /// # extern crate serde_json;
76+ /// # extern crate graphql_client;
77+ /// # #[macro_use]
78+ /// # extern crate serde_derive;
79+ /// #
80+ /// # #[derive(Debug, Deserialize, PartialEq)]
81+ /// # struct ResponseData {
82+ /// # something: i32
83+ /// # }
84+ /// #
85+ /// # fn main() -> Result<(), failure::Error> {
86+ /// use graphql_client::*;
87+ ///
88+ /// let body: GraphQLResponse<ResponseData> = serde_json::from_value(json!({
89+ /// "data": null,
90+ /// "errors": [
91+ /// {
92+ /// "message": "The server crashed. Sorry.",
93+ /// "locations": [{ "line": 1, "column": 1 }]
94+ /// },
95+ /// {
96+ /// "message": "Seismic activity detected",
97+ /// "path": ["undeground", 20]
98+ /// },
99+ /// ],
100+ /// }))?;
101+ ///
102+ /// let expected: GraphQLResponse<ResponseData> = GraphQLResponse {
103+ /// data: None,
104+ /// errors: Some(vec![
105+ /// GraphQLError {
106+ /// message: "The server crashed. Sorry.".to_owned(),
107+ /// locations: Some(vec![
108+ /// Location {
109+ /// line: 1,
110+ /// column: 1,
111+ /// }
112+ /// ]),
113+ /// path: None,
114+ /// extensions: None,
115+ /// },
116+ /// GraphQLError {
117+ /// message: "Seismic activity detected".to_owned(),
118+ /// locations: None,
119+ /// path: Some(vec![
120+ /// PathFragment::Key("undeground".into()),
121+ /// PathFragment::Index(20),
122+ /// ]),
123+ /// extensions: None,
124+ /// },
125+ /// ]),
126+ /// };
127+ ///
128+ /// assert_eq!(body, expected);
129+ ///
130+ /// # Ok(())
131+ /// # }
132+ /// ```
68133#[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
69134pub struct GraphQLError {
70135 /// The human-readable error message. This is the only required field.
@@ -82,7 +147,56 @@ pub struct GraphQLError {
82147/// This will generally be used with the `ResponseData` struct from a derived module.
83148///
84149/// [Spec](https://github.com/facebook/graphql/blob/master/spec/Section%207%20--%20Response.md)
85- #[ derive( Debug , Serialize , Deserialize ) ]
150+ ///
151+ /// ```
152+ /// # extern crate failure;
153+ /// # #[macro_use]
154+ /// # extern crate serde_json;
155+ /// # extern crate graphql_client;
156+ /// # #[macro_use]
157+ /// # extern crate serde_derive;
158+ /// #
159+ /// # #[derive(Debug, Deserialize, PartialEq)]
160+ /// # struct User {
161+ /// # id: i32,
162+ /// # }
163+ /// #
164+ /// # #[derive(Debug, Deserialize, PartialEq)]
165+ /// # struct Dog {
166+ /// # name: String
167+ /// # }
168+ /// #
169+ /// # #[derive(Debug, Deserialize, PartialEq)]
170+ /// # struct ResponseData {
171+ /// # users: Vec<User>,
172+ /// # dogs: Vec<Dog>,
173+ /// # }
174+ /// #
175+ /// # fn main() -> Result<(), failure::Error> {
176+ /// use graphql_client::GraphQLResponse;
177+ ///
178+ /// let body: GraphQLResponse<ResponseData> = serde_json::from_value(json!({
179+ /// "data": {
180+ /// "users": [{"id": 13}],
181+ /// "dogs": [{"name": "Strelka"}],
182+ /// },
183+ /// "errors": [],
184+ /// }))?;
185+ ///
186+ /// let expected: GraphQLResponse<ResponseData> = GraphQLResponse {
187+ /// data: Some(ResponseData {
188+ /// users: vec![User { id: 13 }],
189+ /// dogs: vec![Dog { name: "Strelka".to_owned() }],
190+ /// }),
191+ /// errors: Some(vec![]),
192+ /// };
193+ ///
194+ /// assert_eq!(body, expected);
195+ ///
196+ /// # Ok(())
197+ /// # }
198+ /// ```
199+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
86200pub struct GraphQLResponse < Data > {
87201 /// The absent, partial or complete response data.
88202 pub data : Option < Data > ,
0 commit comments