1
1
use crate :: error:: Error ;
2
2
use crate :: CliResult ;
3
- use graphql_client:: GraphQLQuery ;
3
+ use graphql_client:: { GraphQLQuery , QueryBody } ;
4
4
use reqwest:: header:: { HeaderMap , HeaderValue , ACCEPT , CONTENT_TYPE } ;
5
+ use serde_json:: json;
6
+ use std:: fmt;
5
7
use std:: path:: PathBuf ;
6
8
use std:: str:: FromStr ;
7
9
10
+ use clap:: ValueEnum ;
11
+ use introspection_query:: Variables ;
12
+
8
13
#[ derive( GraphQLQuery ) ]
9
14
#[ graphql(
10
15
schema_path = "src/graphql/introspection_schema.graphql" ,
@@ -21,6 +26,7 @@ pub fn introspect_schema(
21
26
authorization : Option < String > ,
22
27
headers : Vec < Header > ,
23
28
no_ssl : bool ,
29
+ options : Option < Vec < IntrospectionOptions > > ,
24
30
) -> CliResult < ( ) > {
25
31
use std:: io:: Write ;
26
32
@@ -29,11 +35,8 @@ pub fn introspect_schema(
29
35
None => Box :: new ( std:: io:: stdout ( ) ) ,
30
36
} ;
31
37
32
- let request_body: graphql_client:: QueryBody < ( ) > = graphql_client:: QueryBody {
33
- variables : ( ) ,
34
- query : introspection_query:: QUERY ,
35
- operation_name : introspection_query:: OPERATION_NAME ,
36
- } ;
38
+ let request_body: QueryBody < introspection_query:: Variables > =
39
+ IntrospectionQuery :: build_query ( construct_options ( options) ) ;
37
40
38
41
let client = reqwest:: blocking:: Client :: builder ( )
39
42
. danger_accept_invalid_certs ( no_ssl)
@@ -126,6 +129,41 @@ impl FromStr for Header {
126
129
}
127
130
}
128
131
132
+ #[ derive( ValueEnum , Clone , Debug , PartialEq ) ]
133
+ pub enum IntrospectionOptions {
134
+ /// Enable the @oneOf directive in the introspection query.
135
+ IsOneOf ,
136
+ /// SpecifiedBy is is used within the type system definition language to
137
+ /// provide a scalar specification URL for specifying the behavior of custom scalar types.
138
+ SpecifiedBy ,
139
+ }
140
+
141
+ fn construct_options ( options : Option < Vec < IntrospectionOptions > > ) -> introspection_query:: Variables {
142
+ match options {
143
+ Some ( opts) => introspection_query:: Variables {
144
+ is_one_of : opts. contains ( & IntrospectionOptions :: IsOneOf ) ,
145
+ } ,
146
+ None => introspection_query:: Variables { is_one_of : false } ,
147
+ }
148
+ }
149
+
150
+ impl FromStr for IntrospectionOptions {
151
+ type Err = String ;
152
+
153
+ fn from_str ( input : & str ) -> Result < Self , Self :: Err > {
154
+ match input. to_lowercase ( ) . as_str ( ) {
155
+ "isoneof" => Ok ( IntrospectionOptions :: IsOneOf ) ,
156
+ _ => Err ( format ! ( "unknown option {:?}" , input) ) ,
157
+ }
158
+ }
159
+ }
160
+
161
+ impl fmt:: Debug for Variables {
162
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
163
+ write ! ( f, "{}" , json!( self ) )
164
+ }
165
+ }
166
+
129
167
#[ cfg( test) ]
130
168
mod tests {
131
169
use super :: * ;
0 commit comments