Skip to content

Commit b5befea

Browse files
authored
Merge pull request #381 from o0Ignition0o/igni/input_variable_case
Allow to parse camel case input variables from .graphql files and generate snake_case variables in Rust
2 parents 046d7af + 003c623 commit b5befea

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

graphql_client/tests/input_object_variables.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ fn recursive_input_objects_can_be_constructed() {
7474
};
7575
}
7676

77+
#[derive(GraphQLQuery)]
78+
#[graphql(
79+
query_path = "tests/input_object_variables/input_object_variables_query.graphql",
80+
schema_path = "tests/input_object_variables/input_object_variables_schema.graphql",
81+
response_derives = "Debug, PartialEq"
82+
)]
83+
pub struct InputCaseTestsQuery;
84+
85+
#[test]
86+
fn input_objects_are_all_snake_case() {
87+
use input_case_tests_query::*;
88+
89+
let _ = CaseTestInput {
90+
field_with_snake_case: "hello from".to_string(),
91+
other_field_with_camel_case: "the other side".to_string(),
92+
};
93+
}
94+
7795
#[derive(GraphQLQuery)]
7896
#[graphql(
7997
query_path = "tests/input_object_variables/input_object_variables_query.graphql",

graphql_client/tests/input_object_variables/input_object_variables_query.graphql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ query RecursiveInputQuery($input: RecursiveInput!) {
1111
query IndirectlyRecursiveInputQuery($input: IndirectlyRecursiveInput!) {
1212
saveRecursiveInput(recursiveInput: $input)
1313
}
14+
15+
query InputCaseTestsQuery($input: CaseTestInput!) {
16+
testQueryCase(caseTestInput: $input)
17+
}

graphql_client/tests/input_object_variables/input_object_variables_schema.graphql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,18 @@ input IndirectlyRecursiveInputTailPart {
3939
recursed_field: IndirectlyRecursiveInput
4040
}
4141

42+
input CaseTestInput {
43+
field_with_snake_case: String!
44+
otherFieldWithCamelCase: String!
45+
}
46+
47+
type CaseTestResult {
48+
result: String!
49+
}
50+
4251
type InputObjectVariablesQuery {
4352
echo(message: Message!, options: Options = { pgpSignature: true }): EchoResult
53+
testQueryCase(caseTestInput: CaseTestInput!): CaseTestResult
4454
saveRecursiveInput(recursiveInput: RecursiveInput!): Category
4555
}
4656

graphql_client_codegen/src/codegen/inputs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
query::{BoundQuery, UsedTypes},
55
schema::input_is_recursive_without_indirection,
66
};
7+
use heck::SnakeCase;
78
use proc_macro2::{Ident, Span, TokenStream};
89
use quote::quote;
910

@@ -21,7 +22,7 @@ pub(super) fn generate_input_object_definitions(
2122
let struct_name = Ident::new(safe_name.as_ref(), Span::call_site());
2223

2324
let fields = input.fields.iter().map(|(field_name, field_type)| {
24-
let safe_field_name = keyword_replace(field_name);
25+
let safe_field_name = keyword_replace(field_name.to_snake_case());
2526
let annotation = field_rename_annotation(field_name, safe_field_name.as_ref());
2627
let name_ident = Ident::new(safe_field_name.as_ref(), Span::call_site());
2728
let normalized_field_type_name = options

0 commit comments

Comments
 (0)