1+ //! Utilities for building HTTP endpoints in a library-agnostic manner
2+
13use serde:: ser;
24use serde:: ser:: SerializeMap ;
35
46use :: { GraphQLError , Value , Variables , GraphQLType , RootNode } ;
57use ast:: InputValue ;
68use executor:: ExecutionError ;
79
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.
917#[ derive( Deserialize ) ]
1018pub struct GraphQLRequest {
1119 query : String ,
@@ -27,6 +35,7 @@ impl GraphQLRequest {
2735 } ) . unwrap_or_default ( )
2836 }
2937
38+ /// Construct a new GraphQL request from parts
3039 pub fn new ( query : String , operation_name : Option < String > , variables : Option < InputValue > ) -> GraphQLRequest {
3140 GraphQLRequest {
3241 query : query,
@@ -35,6 +44,10 @@ impl GraphQLRequest {
3544 }
3645 }
3746
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.
3851 pub fn execute < ' a , CtxT , QueryT , MutationT > (
3952 & ' a self ,
4053 root_node : & RootNode < QueryT , MutationT > ,
@@ -54,10 +67,18 @@ impl GraphQLRequest {
5467 }
5568}
5669
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.
5875pub struct GraphQLResponse < ' a > ( Result < ( Value , Vec < ExecutionError > ) , GraphQLError < ' a > > ) ;
5976
6077impl < ' 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.
6182 pub fn is_ok ( & self ) -> bool {
6283 self . 0 . is_ok ( )
6384 }
0 commit comments