Skip to content

Commit 1cc394d

Browse files
committed
draft @include for IsOneOf for introspection with cli option
1 parent 5394dcd commit 1cc394d

File tree

4 files changed

+54
-9
lines changed

4 files changed

+54
-9
lines changed

graphql_client_cli/src/graphql/introspection_query.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
query IntrospectionQuery {
1+
query IntrospectionQuery($isOneOf: Boolean!) {
22
__schema {
33
queryType {
44
name
@@ -27,7 +27,7 @@ fragment FullType on __Type {
2727
kind
2828
name
2929
description
30-
isOneOf
30+
isOneOf @include(if: $isOneOf)
3131
fields(includeDeprecated: true) {
3232
name
3333
description

graphql_client_cli/src/graphql/introspection_schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,4 @@ enum __DirectiveLocation {
104104
ENUM_VALUE
105105
INPUT_OBJECT
106106
INPUT_FIELD_DEFINITION
107-
}
107+
}

graphql_client_cli/src/introspect_schema.rs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
use crate::error::Error;
22
use crate::CliResult;
3-
use graphql_client::GraphQLQuery;
3+
use graphql_client::{GraphQLQuery, QueryBody};
44
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE};
5+
use serde_json::json;
6+
use std::fmt;
57
use std::path::PathBuf;
68
use std::str::FromStr;
79

10+
use clap::ValueEnum;
11+
use introspection_query::Variables;
12+
813
#[derive(GraphQLQuery)]
914
#[graphql(
1015
schema_path = "src/graphql/introspection_schema.graphql",
@@ -21,6 +26,7 @@ pub fn introspect_schema(
2126
authorization: Option<String>,
2227
headers: Vec<Header>,
2328
no_ssl: bool,
29+
options: Option<Vec<IntrospectionOptions>>,
2430
) -> CliResult<()> {
2531
use std::io::Write;
2632

@@ -29,11 +35,8 @@ pub fn introspect_schema(
2935
None => Box::new(std::io::stdout()),
3036
};
3137

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));
3740

3841
let client = reqwest::blocking::Client::builder()
3942
.danger_accept_invalid_certs(no_ssl)
@@ -126,6 +129,41 @@ impl FromStr for Header {
126129
}
127130
}
128131

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+
129167
#[cfg(test)]
130168
mod tests {
131169
use super::*;

graphql_client_cli/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod introspect_schema;
55
use clap::Parser;
66
use env_logger::fmt::{Color, Style, StyledValue};
77
use error::Error;
8+
use introspect_schema::IntrospectionOptions;
89
use log::Level;
910
use std::path::PathBuf;
1011
use Cli::Generate;
@@ -34,6 +35,10 @@ enum Cli {
3435
/// Default value is false.
3536
#[clap(long = "no-ssl")]
3637
no_ssl: bool,
38+
/// Introspection Options
39+
/// is-one-of will include IsOneOf in the introspection query.
40+
#[clap(long = "options", action(clap::ArgAction::Append))]
41+
options: Option<Vec<IntrospectionOptions>>,
3742
},
3843
#[clap(name = "generate")]
3944
Generate {
@@ -93,12 +98,14 @@ fn main() -> CliResult<()> {
9398
authorization,
9499
headers,
95100
no_ssl,
101+
options,
96102
} => introspect_schema::introspect_schema(
97103
&schema_location,
98104
output,
99105
authorization,
100106
headers,
101107
no_ssl,
108+
options,
102109
),
103110
Generate {
104111
variables_derives,

0 commit comments

Comments
 (0)