Skip to content

Commit 761bd46

Browse files
committed
GraphQLQueryBody: send operationName as well
When using a single query file for multiple actual queries, the server needs to know which one we're actually asking for.
1 parent a82359b commit 761bd46

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

graphql_client_cli/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ fn introspect_schema(
7878
let request_body: graphql_client::GraphQLQueryBody<()> = graphql_client::GraphQLQueryBody {
7979
variables: (),
8080
query: introspection_query::QUERY,
81+
operation_name: introspection_query::OPERATION_NAME,
8182
};
8283

8384
let headers = set_headers(authorization);

graphql_client_codegen/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ pub fn generate_module_token_stream(
122122
}
123123
};
124124

125-
let module_name = Ident::new(&input.ident.to_string().to_snake_case(), Span::call_site());
125+
let operation_string = input.ident.to_string();
126+
let module_name = Ident::new(&operation_string.to_snake_case(), Span::call_site());
126127
let struct_name = &input.ident;
127128
let schema_output = codegen::response_for_query(
128129
schema,
@@ -141,6 +142,7 @@ pub fn generate_module_token_stream(
141142
use serde;
142143

143144
pub const QUERY: &'static str = #query_string;
145+
pub const OPERATION_NAME: &'static str = #operation_string;
144146

145147
#schema_output
146148
}
@@ -153,6 +155,7 @@ pub fn generate_module_token_stream(
153155
::graphql_client::GraphQLQueryBody {
154156
variables,
155157
query: #module_name::QUERY,
158+
operation_name: #module_name::OPERATION_NAME,
156159
}
157160

158161
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use std::collections::HashMap;
4949
/// };
5050
///
5151
/// let expected_body = json!({
52+
/// "operationName": star_wars_query::OPERATION_NAME,
5253
/// "query": star_wars_query::QUERY,
5354
/// "variables": {
5455
/// "episodeForHero": "NEWHOPE"
@@ -84,6 +85,9 @@ where
8485
pub variables: Variables,
8586
/// The GraphQL query, as a string.
8687
pub query: &'static str,
88+
/// The GraphQL operation name, as a string.
89+
#[serde(rename = "operationName")]
90+
pub operation_name: &'static str,
8791
}
8892

8993
/// Represents a location inside a query string. Used in errors. See [`GraphQLError`].

0 commit comments

Comments
 (0)