graphql_client_codegen/
codegen_options.rs

1use crate::deprecation::DeprecationStrategy;
2use crate::normalization::Normalization;
3use proc_macro2::Ident;
4use std::path::{Path, PathBuf};
5use syn::{self, Visibility};
6
7/// Which context is this code generation effort taking place.
8#[derive(Debug)]
9pub enum CodegenMode {
10    /// The graphql-client CLI.
11    Cli,
12    /// The derive macro defined in graphql_query_derive.
13    Derive,
14}
15
16/// Used to configure code generation.
17pub struct GraphQLClientCodegenOptions {
18    /// Which context is this code generation effort taking place.
19    pub mode: CodegenMode,
20    /// Name of the operation we want to generate code for. If it does not match, we use all queries.
21    pub operation_name: Option<String>,
22    /// The name of implementation target struct.
23    pub struct_name: Option<String>,
24    /// The struct for which we derive GraphQLQuery.
25    struct_ident: Option<Ident>,
26    /// Comma-separated list of additional traits we want to derive for variables.
27    variables_derives: Option<String>,
28    /// Comma-separated list of additional traits we want to derive for responses.
29    response_derives: Option<String>,
30    /// The deprecation strategy to adopt.
31    deprecation_strategy: Option<DeprecationStrategy>,
32    /// Target module visibility.
33    module_visibility: Option<Visibility>,
34    /// A path to a file to include in the module to force Cargo to take into account changes in
35    /// the query files when recompiling.
36    query_file: Option<PathBuf>,
37    /// A path to a file to include in the module to force Cargo to take into account changes in
38    /// the schema files when recompiling.
39    schema_file: Option<PathBuf>,
40    /// Normalization pattern for query types and names.
41    normalization: Normalization,
42    /// Custom scalar definitions module path
43    custom_scalars_module: Option<syn::Path>,
44    /// List of externally defined enum types. Type names must match those used in the schema exactly.
45    extern_enums: Vec<String>,
46    /// Flag to trigger generation of Other variant for fragments Enum
47    fragments_other_variant: bool,
48    /// Skip Serialization of None values.
49    skip_serializing_none: bool,
50}
51
52impl GraphQLClientCodegenOptions {
53    /// Creates an empty options object with default params. It probably wants to be configured.
54    pub fn new(mode: CodegenMode) -> GraphQLClientCodegenOptions {
55        GraphQLClientCodegenOptions {
56            mode,
57            variables_derives: Default::default(),
58            response_derives: Default::default(),
59            deprecation_strategy: Default::default(),
60            module_visibility: Default::default(),
61            operation_name: Default::default(),
62            struct_ident: Default::default(),
63            struct_name: Default::default(),
64            query_file: Default::default(),
65            schema_file: Default::default(),
66            normalization: Normalization::None,
67            custom_scalars_module: Default::default(),
68            extern_enums: Default::default(),
69            fragments_other_variant: Default::default(),
70            skip_serializing_none: Default::default(),
71        }
72    }
73
74    /// The visibility (public/private) to apply to the target module.
75    pub(crate) fn module_visibility(&self) -> &Visibility {
76        self.module_visibility
77            .as_ref()
78            .unwrap_or(&Visibility::Inherited)
79    }
80
81    /// The deprecation strategy to adopt.
82    pub(crate) fn deprecation_strategy(&self) -> DeprecationStrategy {
83        self.deprecation_strategy.clone().unwrap_or_default()
84    }
85
86    /// A path to a file to include in the module to force Cargo to take into account changes in
87    /// the query files when recompiling.
88    pub fn set_query_file(&mut self, path: PathBuf) {
89        self.query_file = Some(path);
90    }
91
92    /// Comma-separated list of additional traits we want to derive for variables.
93    pub fn variables_derives(&self) -> Option<&str> {
94        self.variables_derives.as_deref()
95    }
96
97    /// Comma-separated list of additional traits we want to derive for variables.
98    pub fn set_variables_derives(&mut self, variables_derives: String) {
99        self.variables_derives = Some(variables_derives);
100    }
101
102    /// All the variable derives to be rendered.
103    pub fn all_variable_derives(&self) -> impl Iterator<Item = &str> {
104        let additional = self
105            .variables_derives
106            .as_deref()
107            .into_iter()
108            .flat_map(|s| s.split(','))
109            .map(|s| s.trim());
110
111        std::iter::once("Serialize").chain(additional)
112    }
113
114    /// Traits we want to derive for responses.
115    pub fn all_response_derives(&self) -> impl Iterator<Item = &str> {
116        let base_derives = std::iter::once("Deserialize");
117
118        base_derives.chain(
119            self.additional_response_derives()
120                .filter(|additional| additional != &"Deserialize"),
121        )
122    }
123
124    /// Additional traits we want to derive for responses.
125    pub fn additional_response_derives(&self) -> impl Iterator<Item = &str> {
126        self.response_derives
127            .as_deref()
128            .into_iter()
129            .flat_map(|s| s.split(','))
130            .map(|s| s.trim())
131    }
132
133    /// Comma-separated list of additional traits we want to derive for responses.
134    pub fn set_response_derives(&mut self, response_derives: String) {
135        self.response_derives = Some(response_derives);
136    }
137
138    /// The deprecation strategy to adopt.
139    pub fn set_deprecation_strategy(&mut self, deprecation_strategy: DeprecationStrategy) {
140        self.deprecation_strategy = Some(deprecation_strategy);
141    }
142
143    /// Target module visibility.
144    pub fn set_module_visibility(&mut self, visibility: Visibility) {
145        self.module_visibility = Some(visibility);
146    }
147
148    /// The name of implementation target struct.
149    pub fn set_struct_name(&mut self, struct_name: String) {
150        self.struct_name = Some(struct_name);
151    }
152
153    /// Name of the operation we want to generate code for. If none is selected, it means all
154    /// operations.
155    pub fn set_operation_name(&mut self, operation_name: String) {
156        self.operation_name = Some(operation_name);
157    }
158
159    /// A path to a file to include in the module to force Cargo to take into account changes in
160    /// the schema files when recompiling.
161    pub fn schema_file(&self) -> Option<&Path> {
162        self.schema_file.as_deref()
163    }
164
165    /// A path to a file to include in the module to force Cargo to take into account changes in
166    /// the query files when recompiling.
167    pub fn query_file(&self) -> Option<&Path> {
168        self.query_file.as_deref()
169    }
170
171    /// The identifier to use when referring to the struct implementing GraphQLQuery, if any.
172    pub fn set_struct_ident(&mut self, ident: Ident) {
173        self.struct_ident = Some(ident);
174    }
175
176    /// The identifier to use when referring to the struct implementing GraphQLQuery, if any.
177    pub fn struct_ident(&self) -> Option<&proc_macro2::Ident> {
178        self.struct_ident.as_ref()
179    }
180
181    /// Set the normalization mode for the generated code.
182    pub fn set_normalization(&mut self, norm: Normalization) {
183        self.normalization = norm;
184    }
185
186    /// The normalization mode for the generated code.
187    pub fn normalization(&self) -> &Normalization {
188        &self.normalization
189    }
190
191    /// Get the custom scalar definitions module
192    pub fn custom_scalars_module(&self) -> Option<&syn::Path> {
193        self.custom_scalars_module.as_ref()
194    }
195
196    /// Set the custom scalar definitions module
197    pub fn set_custom_scalars_module(&mut self, module: syn::Path) {
198        self.custom_scalars_module = Some(module)
199    }
200
201    /// Get the externally defined enums type names
202    pub fn extern_enums(&self) -> &[String] {
203        &self.extern_enums
204    }
205
206    /// Set the externally defined enums type names
207    pub fn set_extern_enums(&mut self, enums: Vec<String>) {
208        self.extern_enums = enums;
209    }
210
211    /// Set the graphql client codegen options's fragments other variant.
212    pub fn set_fragments_other_variant(&mut self, fragments_other_variant: bool) {
213        self.fragments_other_variant = fragments_other_variant;
214    }
215
216    /// Get a reference to the graphql client codegen options's fragments other variant.
217    pub fn fragments_other_variant(&self) -> &bool {
218        &self.fragments_other_variant
219    }
220
221    /// Set the graphql client codegen option's skip none value.
222    pub fn set_skip_serializing_none(&mut self, skip_serializing_none: bool) {
223        self.skip_serializing_none = skip_serializing_none
224    }
225
226    /// Get a reference to the graphql client codegen option's skip none value.
227    pub fn skip_serializing_none(&self) -> &bool {
228        &self.skip_serializing_none
229    }
230}