Skip to content

Annotate variable struct fields too #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 16, 2021
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
29 changes: 29 additions & 0 deletions graphql_client/tests/input_object_variables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,32 @@ fn indirectly_recursive_input_objects_can_be_constructed() {
})),
};
}

#[derive(GraphQLQuery)]
#[graphql(
query_path = "tests/input_object_variables/input_object_variables_query.graphql",
schema_path = "tests/input_object_variables/input_object_variables_schema.graphql",
variables_derives = "Default",
response_derives = "Debug, PartialEq"
)]
pub struct RustNameQuery;

#[test]
fn rust_name_correctly_mapped() {
use rust_name_query::*;
let value = serde_json::to_value(&Variables {
extern_: Some("hello".to_owned()),
msg: <_>::default(),
})
.unwrap();
assert_eq!(
value
.as_object()
.unwrap()
.get("extern")
Copy link
Contributor Author

@dingxiangfei2009 dingxiangfei2009 Oct 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An assertion is made here so that the original field extern of the query variable is sent to the GraphQL server, instead of extern_ which mostly likely gets ignored.

.unwrap()
.as_str()
.unwrap(),
"hello"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ query IndirectlyRecursiveInputQuery($input: IndirectlyRecursiveInput!) {
query InputCaseTestsQuery($input: CaseTestInput!) {
testQueryCase(caseTestInput: $input)
}

query RustNameQuery($msg: Message, $extern: String) {
echo(message: $msg, extern: $extern) {
result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type CaseTestResult {
}

type InputObjectVariablesQuery {
echo(message: Message!, options: Options = { pgpSignature: true }): EchoResult
echo(message: Message!, options: Options = { pgpSignature: true }, extern: String = ""): EchoResult
testQueryCase(caseTestInput: CaseTestInput!): CaseTestResult
saveRecursiveInput(recursiveInput: RecursiveInput!): Category
}
Expand Down
8 changes: 3 additions & 5 deletions graphql_client_codegen/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,9 @@ fn generate_variable_struct_field(
query: &BoundQuery<'_>,
) -> TokenStream {
let snake_case_name = variable.name.to_snake_case();
let ident = Ident::new(
&shared::keyword_replace(&snake_case_name),
Span::call_site(),
);
let annotation = shared::field_rename_annotation(&variable.name, &snake_case_name);
let safe_name = shared::keyword_replace(&snake_case_name);
let ident = Ident::new(&safe_name, Span::call_site());
let annotation = shared::field_rename_annotation(&variable.name, &safe_name);
let r#type = render_variable_field_type(variable, options, query);

quote::quote!(#annotation pub #ident : #r#type)
Expand Down