1
+ //! Utilities for building HTTP endpoints in a library-agnostic manner
2
+
1
3
use serde:: ser;
2
4
use serde:: ser:: SerializeMap ;
3
5
4
6
use :: { GraphQLError , Value , Variables , GraphQLType , RootNode } ;
5
7
use ast:: InputValue ;
6
8
use executor:: ExecutionError ;
7
9
8
- /// The expected structure of the decoded JSON Document for either Post or Get requests.
10
+ /// The expected structure of the decoded JSON document for either POST or GET requests.
11
+ ///
12
+ /// For POST, you can use Serde to deserialize the incoming JSON data directly
13
+ /// into this struct - it derives Deserialize for exactly this reason.
14
+ ///
15
+ /// For GET, you will need to parse the query string and exctract "query",
16
+ /// "operationName", and "variables" manually.
9
17
#[ derive( Deserialize ) ]
10
18
pub struct GraphQLRequest {
11
19
query : String ,
@@ -27,6 +35,7 @@ impl GraphQLRequest {
27
35
} ) . unwrap_or_default ( )
28
36
}
29
37
38
+ /// Construct a new GraphQL request from parts
30
39
pub fn new ( query : String , operation_name : Option < String > , variables : Option < InputValue > ) -> GraphQLRequest {
31
40
GraphQLRequest {
32
41
query : query,
@@ -35,6 +44,10 @@ impl GraphQLRequest {
35
44
}
36
45
}
37
46
47
+ /// Execute a GraphQL request using the specified schema and context
48
+ ///
49
+ /// This is a simple wrapper around the `execute` function exposed at the
50
+ /// top level of this crate.
38
51
pub fn execute < ' a , CtxT , QueryT , MutationT > (
39
52
& ' a self ,
40
53
root_node : & RootNode < QueryT , MutationT > ,
@@ -54,10 +67,18 @@ impl GraphQLRequest {
54
67
}
55
68
}
56
69
57
-
70
+ /// Simple wrapper around the result from executing a GraphQL query
71
+ ///
72
+ /// This struct implements Serialize, so you can simply serialize this
73
+ /// to JSON and send it over the wire. Use the `is_ok` method to determine
74
+ /// whether to send a 200 or 400 HTTP status code.
58
75
pub struct GraphQLResponse < ' a > ( Result < ( Value , Vec < ExecutionError > ) , GraphQLError < ' a > > ) ;
59
76
60
77
impl < ' a > GraphQLResponse < ' a > {
78
+ /// Was the request successful or not?
79
+ ///
80
+ /// Note that there still might be errors in the response even though it's
81
+ /// considered OK. This is by design in GraphQL.
61
82
pub fn is_ok ( & self ) -> bool {
62
83
self . 0 . is_ok ( )
63
84
}
0 commit comments