Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,20 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## Unreleased

- Support for `deprecated` directive on InputValue fields (#553)
- Support for custom variable and response types (#536)
- Allow using `#[derive(GraphQLQuery)]` without depending on `serde` directly (#487)
- CLI option for extern enums (#520)
- Support deserializing IDs from integers or strings (#476)
- Introspection schema now includes `oneOf` and `specifiedByUrl` (#501)
- Update `reqwest` to 0.12 (#499)
- Fix required ID deserialization (#523)
- Fix `skip_serializing_none` for root level variables (#485)
- Use consistent reference to `graphql_client` crate in codegen (#484)
- Fix multiple operations example in README (#497)

## 0.14.0 - 2024-03-26

- Add support for GraphQL’s `extend type` directive
Expand Down
8 changes: 5 additions & 3 deletions graphql_client_cli/src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ pub(crate) fn generate_code(params: CliCodegenParams) -> CliResult<()> {

options.set_custom_scalars_module(custom_scalars_module);
}

if let Some(custom_variable_types) = custom_variable_types {
options.set_custom_variable_types(custom_variable_types.split(",").map(String::from).collect());
options.set_custom_variable_types(
custom_variable_types.split(",").map(String::from).collect(),
);
}

if let Some(custom_response_type) = custom_response_type {
options.set_custom_response_type(custom_response_type);
}
Expand Down
4 changes: 2 additions & 2 deletions graphql_client_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ fn main() -> CliResult<()> {
selected_operation,
custom_scalars_module,
fragments_other_variant,
external_enums,
custom_variable_types,
external_enums,
custom_variable_types,
custom_response_type,
} => generate::generate_code(generate::CliCodegenParams {
query_path,
Expand Down
18 changes: 13 additions & 5 deletions graphql_client_codegen/src/codegen/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,19 @@ pub(super) fn generate_input_object_definitions(
all_used_types
.inputs(query.schema)
.map(|(input_id, input)| {
let custom_variable_type = query.query.variables.iter()
let custom_variable_type = query
.query
.variables
.iter()
.enumerate()
.find(|(_, v) | v.r#type.id.as_input_id().is_some_and(|i| i == input_id))
.map(|(index, _)| custom_variable_types.get(index))
.flatten();
.find(|(_, v)| {
v.r#type
.id
.as_input_id()
.map(|i| i == input_id)
.unwrap_or_default()
})
.and_then(|(index, _)| custom_variable_types.get(index));
if let Some(custom_type) = custom_variable_type {
generate_type_def(input, options, custom_type)
} else if input.is_one_of {
Expand All @@ -38,7 +46,7 @@ pub(super) fn generate_input_object_definitions(
fn generate_type_def(
input: &StoredInputType,
options: &GraphQLClientCodegenOptions,
custom_type: &String,
custom_type: &str,
) -> TokenStream {
let custom_type = syn::parse_str::<syn::Path>(custom_type).unwrap();
let normalized_name = options.normalization().input_name(input.name.as_str());
Expand Down
34 changes: 24 additions & 10 deletions graphql_client_codegen/src/codegen/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use crate::{
},
schema::{Schema, TypeId},
type_qualifiers::GraphqlTypeQualifier,
GraphQLClientCodegenOptions,
GeneralError,
GeneralError, GraphQLClientCodegenOptions,
};
use heck::*;
use proc_macro2::{Ident, Span, TokenStream};
Expand Down Expand Up @@ -43,12 +42,27 @@ pub(crate) fn render_response_data_fields<'a>(
if let Some(custom_response_type) = options.custom_response_type() {
if operation.selection_set.len() == 1 {
let selection_id = operation.selection_set[0];
let selection_field = query.query.get_selection(selection_id).as_selected_field()
.ok_or_else(|| GeneralError(format!("Custom response type {custom_response_type} will only work on fields")))?;
calculate_custom_response_type_selection(&mut expanded_selection, response_data_type_id, custom_response_type, selection_id, selection_field);
let selection_field = query
.query
.get_selection(selection_id)
.as_selected_field()
.ok_or_else(|| {
GeneralError(format!(
"Custom response type {custom_response_type} will only work on fields"
))
})?;
calculate_custom_response_type_selection(
&mut expanded_selection,
response_data_type_id,
custom_response_type,
selection_id,
selection_field,
);
return Ok(expanded_selection);
} else {
return Err(GeneralError(format!("Custom response type {custom_response_type} requires single selection field")));
return Err(GeneralError(format!(
"Custom response type {custom_response_type} requires single selection field"
)));
}
}

Expand All @@ -66,10 +80,10 @@ pub(crate) fn render_response_data_fields<'a>(
fn calculate_custom_response_type_selection<'a>(
context: &mut ExpandedSelection<'a>,
struct_id: ResponseTypeId,
custom_response_type: &'a String,
custom_response_type: &'a str,
selection_id: SelectionId,
field: &'a SelectedField)
{
field: &'a SelectedField,
) {
let (graphql_name, rust_name) = context.field_name(field);
let struct_name_string = full_path_prefix(selection_id, context.query);
let field = context.query.schema.get_field(field.field_id);
Expand All @@ -88,7 +102,7 @@ fn calculate_custom_response_type_selection<'a>(
name: struct_name_string.into(),
});
context.push_type_alias(TypeAlias {
name: custom_response_type.as_str(),
name: custom_response_type,
struct_id,
boxed: false,
});
Expand Down
6 changes: 1 addition & 5 deletions graphql_client_codegen/src/query/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ pub(super) fn validate_type_conditions(
TypeId::Union(union_id) => {
let union = query.schema.get_union(union_id);

if !union
.variants
.iter()
.any(|variant| *variant == selected_type)
{
if !union.variants.contains(&selected_type) {
return Err(QueryValidationError::new(format!(
"The spread {}... on {} is not valid.",
union.name,
Expand Down
9 changes: 1 addition & 8 deletions graphql_client_codegen/src/schema/json_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,7 @@ fn ingest_enum(schema: &mut Schema, enm: &mut FullType) {
.as_mut()
.expect("enm.enum_values.as_mut()")
.iter_mut()
.map(|v| {
std::mem::take(
v.name
.as_mut()
.take()
.expect("variant.name.as_mut().take()"),
)
})
.map(|v| std::mem::take(v.name.as_mut().expect("variant.name.as_mut().take()")))
.collect();

let enm = super::StoredEnum { name, variants };
Expand Down
3 changes: 2 additions & 1 deletion graphql_client_codegen/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ fn blended_custom_types_works() {
match r {
Ok(_) => {
// Variables and returns should be replaced with custom types
assert!(generated_code.contains("pub type SearchQuerySearch = external_crate :: Transaction"));
assert!(generated_code
.contains("pub type SearchQuerySearch = external_crate :: Transaction"));
assert!(generated_code.contains("pub type extern_ = external_crate :: ID"));
}
Err(e) => {
Expand Down
2 changes: 1 addition & 1 deletion graphql_query_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn build_graphql_client_derive_options(
if let Some(custom_variable_types) = custom_variable_types {
options.set_custom_variable_types(custom_variable_types);
}

if let Some(custom_response_type) = custom_response_type {
options.set_custom_response_type(custom_response_type);
}
Expand Down