Skip to content

Commit a6a8972

Browse files
authored
Merge pull request #390 from dingxiangfei2009/annotate-variable-fields
Annotate variable struct fields too
2 parents 9685a9d + 8b9f605 commit a6a8972

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

graphql_client/tests/input_object_variables.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,32 @@ fn indirectly_recursive_input_objects_can_be_constructed() {
117117
})),
118118
};
119119
}
120+
121+
#[derive(GraphQLQuery)]
122+
#[graphql(
123+
query_path = "tests/input_object_variables/input_object_variables_query.graphql",
124+
schema_path = "tests/input_object_variables/input_object_variables_schema.graphql",
125+
variables_derives = "Default",
126+
response_derives = "Debug, PartialEq"
127+
)]
128+
pub struct RustNameQuery;
129+
130+
#[test]
131+
fn rust_name_correctly_mapped() {
132+
use rust_name_query::*;
133+
let value = serde_json::to_value(&Variables {
134+
extern_: Some("hello".to_owned()),
135+
msg: <_>::default(),
136+
})
137+
.unwrap();
138+
assert_eq!(
139+
value
140+
.as_object()
141+
.unwrap()
142+
.get("extern")
143+
.unwrap()
144+
.as_str()
145+
.unwrap(),
146+
"hello"
147+
);
148+
}

graphql_client/tests/input_object_variables/input_object_variables_query.graphql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@ query IndirectlyRecursiveInputQuery($input: IndirectlyRecursiveInput!) {
1515
query InputCaseTestsQuery($input: CaseTestInput!) {
1616
testQueryCase(caseTestInput: $input)
1717
}
18+
19+
query RustNameQuery($msg: Message, $extern: String) {
20+
echo(message: $msg, extern: $extern) {
21+
result
22+
}
23+
}

graphql_client/tests/input_object_variables/input_object_variables_schema.graphql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type CaseTestResult {
4949
}
5050

5151
type InputObjectVariablesQuery {
52-
echo(message: Message!, options: Options = { pgpSignature: true }): EchoResult
52+
echo(message: Message!, options: Options = { pgpSignature: true }, extern: String = ""): EchoResult
5353
testQueryCase(caseTestInput: CaseTestInput!): CaseTestResult
5454
saveRecursiveInput(recursiveInput: RecursiveInput!): Category
5555
}

graphql_client_codegen/src/codegen.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,9 @@ fn generate_variable_struct_field(
133133
query: &BoundQuery<'_>,
134134
) -> TokenStream {
135135
let snake_case_name = variable.name.to_snake_case();
136-
let ident = Ident::new(
137-
&shared::keyword_replace(&snake_case_name),
138-
Span::call_site(),
139-
);
140-
let annotation = shared::field_rename_annotation(&variable.name, &snake_case_name);
136+
let safe_name = shared::keyword_replace(&snake_case_name);
137+
let ident = Ident::new(&safe_name, Span::call_site());
138+
let annotation = shared::field_rename_annotation(&variable.name, &safe_name);
141139
let r#type = render_variable_field_type(variable, options, query);
142140

143141
quote::quote!(#annotation pub #ident : #r#type)

0 commit comments

Comments
 (0)