Skip to content

Commit f2ea14a

Browse files
authored
Merge pull request #108 from mathstuf/stutter-rename
api: remove `GraphQL` prefix from type names
2 parents 5b8e01e + 855f9c1 commit f2ea14a

File tree

8 files changed

+31
-29
lines changed

8 files changed

+31
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
4646

4747
- Most of the `graphql-query-derive` crate was factored out into a new `graphql-client-codegen` crate that should enable code generation through means other than custom derives (CLI, build scripts...). Thanks @h-michael for this important refactoring!
4848

49+
- Types have been renamed. Types no longer have a `GraphQL` prefix.
50+
4951
### Fixed
5052

5153
- Handle all Rust keywords as field names in codegen by appending `_` to the generated names, so a field called `type` in a GraphQL query will become a `type_` field in the generated struct. Thanks to @scrogson!

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ A typed GraphQL client library for Rust.
5252

5353
That module contains all the struct and enum definitions necessary to deserialize a response to that query.
5454

55-
The root type for the response is named `ResponseData`. The GraphQL response will take the form of a `GraphQLResponse<ResponseData>` (the [GraphQLResponse](https://docs.rs/graphql_client/latest/graphql_client/struct.GraphQLResponse.html) type is always the same).
55+
The root type for the response is named `ResponseData`. The GraphQL response will take the form of a `Response<ResponseData>` (the [Response](https://docs.rs/graphql_client/latest/graphql_client/struct.Response.html) type is always the same).
5656

5757
The module also contains a struct called `Variables` representing the variables expected by the query.
5858

@@ -64,7 +64,7 @@ A typed GraphQL client library for Rust.
6464
extern crate graphql_client;
6565
extern crate reqwest;
6666

67-
use graphql_client::{GraphQLQuery, GraphQLResponse};
67+
use graphql_client::{GraphQLQuery, Response};
6868

6969
fn perform_my_query(variables: &my_query::Variables) -> Result<(), failure::Error> {
7070

@@ -73,7 +73,7 @@ A typed GraphQL client library for Rust.
7373

7474
let client = reqwest::Client::new();
7575
let mut res = client.post("/graphql").json(&request_body).send()?;
76-
let response_body: GraphQLResponse<my_query::ResponseData> = res.json()?;
76+
let response_body: Response<my_query::ResponseData> = res.json()?;
7777
println!("{:#?}", response_body);
7878
Ok(())
7979
}

examples/call_from_js/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ fn cb() -> Closure<Fn(String)> {
9898

9999
let parent = document.body();
100100

101-
let json: GraphQLResponse<puppy_smiles::ResponseData> =
101+
let json: Response<puppy_smiles::ResponseData> =
102102
serde_json::from_str(&s).expect("failed to deserialize");
103103
let response = document.createElement("div");
104104
let mut inner_html = String::new();

examples/github/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn main() -> Result<(), failure::Error> {
7373
.json(&q)
7474
.send()?;
7575

76-
let response_body: GraphQLResponse<query1::ResponseData> = res.json()?;
76+
let response_body: Response<query1::ResponseData> = res.json()?;
7777
info!("{:?}", response_body);
7878

7979
if let Some(errors) = response_body.errors {

graphql_client_cli/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn introspect_schema(
7474
None => Box::new(::std::io::stdout()),
7575
};
7676

77-
let request_body: graphql_client::GraphQLQueryBody<()> = graphql_client::GraphQLQueryBody {
77+
let request_body: graphql_client::QueryBody<()> = graphql_client::QueryBody {
7878
variables: (),
7979
query: introspection_query::QUERY,
8080
operation_name: introspection_query::OPERATION_NAME,

graphql_client_codegen/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ pub fn generate_module_token_stream(
151151
type Variables = #module_name::Variables;
152152
type ResponseData = #module_name::ResponseData;
153153

154-
fn build_query(variables: Self::Variables) -> ::graphql_client::GraphQLQueryBody<Self::Variables> {
155-
::graphql_client::GraphQLQueryBody {
154+
fn build_query(variables: Self::Variables) -> ::graphql_client::QueryBody<Self::Variables> {
155+
::graphql_client::QueryBody {
156156
variables,
157157
query: #module_name::QUERY,
158158
operation_name: #module_name::OPERATION_NAME,

src/lib.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ pub trait GraphQLQuery<'de> {
7272
type ResponseData: serde::Deserialize<'de>;
7373

7474
/// Produce a GraphQL query struct that can be JSON serialized and sent to a GraphQL API.
75-
fn build_query(variables: Self::Variables) -> GraphQLQueryBody<Self::Variables>;
75+
fn build_query(variables: Self::Variables) -> QueryBody<Self::Variables>;
7676
}
7777

7878
/// The form in which queries are sent over HTTP in most implementations. This will be built using the [`GraphQLQuery`] trait normally.
7979
#[derive(Debug, Serialize, Deserialize)]
80-
pub struct GraphQLQueryBody<Variables>
80+
pub struct QueryBody<Variables>
8181
where
8282
Variables: serde::Serialize,
8383
{
@@ -90,7 +90,7 @@ where
9090
pub operation_name: &'static str,
9191
}
9292

93-
/// Represents a location inside a query string. Used in errors. See [`GraphQLError`].
93+
/// Represents a location inside a query string. Used in errors. See [`Error`].
9494
#[derive(Debug, Serialize, Deserialize, PartialEq)]
9595
pub struct Location {
9696
/// The line number in the query string where the error originated (starting from 1).
@@ -99,7 +99,7 @@ pub struct Location {
9999
pub column: i32,
100100
}
101101

102-
/// Part of a path in a query. It can be an object key or an array index. See [`GraphQLError`].
102+
/// Part of a path in a query. It can be an object key or an array index. See [`Error`].
103103
#[derive(Debug, Serialize, Deserialize, PartialEq)]
104104
#[serde(untagged)]
105105
pub enum PathFragment {
@@ -132,7 +132,7 @@ pub enum PathFragment {
132132
/// # fn main() -> Result<(), failure::Error> {
133133
/// use graphql_client::*;
134134
///
135-
/// let body: GraphQLResponse<ResponseData> = serde_json::from_value(json!({
135+
/// let body: Response<ResponseData> = serde_json::from_value(json!({
136136
/// "data": null,
137137
/// "errors": [
138138
/// {
@@ -146,10 +146,10 @@ pub enum PathFragment {
146146
/// ],
147147
/// }))?;
148148
///
149-
/// let expected: GraphQLResponse<ResponseData> = GraphQLResponse {
149+
/// let expected: Response<ResponseData> = Response {
150150
/// data: None,
151151
/// errors: Some(vec![
152-
/// GraphQLError {
152+
/// Error {
153153
/// message: "The server crashed. Sorry.".to_owned(),
154154
/// locations: Some(vec![
155155
/// Location {
@@ -160,7 +160,7 @@ pub enum PathFragment {
160160
/// path: None,
161161
/// extensions: None,
162162
/// },
163-
/// GraphQLError {
163+
/// Error {
164164
/// message: "Seismic activity detected".to_owned(),
165165
/// locations: None,
166166
/// path: Some(vec![
@@ -178,7 +178,7 @@ pub enum PathFragment {
178178
/// # }
179179
/// ```
180180
#[derive(Debug, Serialize, Deserialize, PartialEq)]
181-
pub struct GraphQLError {
181+
pub struct Error {
182182
/// The human-readable error message. This is the only required field.
183183
pub message: String,
184184
/// Which locations in the query the error applies to.
@@ -220,17 +220,17 @@ pub struct GraphQLError {
220220
/// # }
221221
/// #
222222
/// # fn main() -> Result<(), failure::Error> {
223-
/// use graphql_client::GraphQLResponse;
223+
/// use graphql_client::Response;
224224
///
225-
/// let body: GraphQLResponse<ResponseData> = serde_json::from_value(json!({
225+
/// let body: Response<ResponseData> = serde_json::from_value(json!({
226226
/// "data": {
227227
/// "users": [{"id": 13}],
228228
/// "dogs": [{"name": "Strelka"}],
229229
/// },
230230
/// "errors": [],
231231
/// }))?;
232232
///
233-
/// let expected: GraphQLResponse<ResponseData> = GraphQLResponse {
233+
/// let expected: Response<ResponseData> = Response {
234234
/// data: Some(ResponseData {
235235
/// users: vec![User { id: 13 }],
236236
/// dogs: vec![Dog { name: "Strelka".to_owned() }],
@@ -244,11 +244,11 @@ pub struct GraphQLError {
244244
/// # }
245245
/// ```
246246
#[derive(Debug, Serialize, Deserialize, PartialEq)]
247-
pub struct GraphQLResponse<Data> {
247+
pub struct Response<Data> {
248248
/// The absent, partial or complete response data.
249249
pub data: Option<Data>,
250250
/// The top-level errors returned by the server.
251-
pub errors: Option<Vec<GraphQLError>>,
251+
pub errors: Option<Vec<Error>>,
252252
}
253253

254254
#[cfg(test)]
@@ -261,11 +261,11 @@ mod tests {
261261
"message": "I accidentally your whole query"
262262
});
263263

264-
let deserialized_error: GraphQLError = serde_json::from_value(err).unwrap();
264+
let deserialized_error: Error = serde_json::from_value(err).unwrap();
265265

266266
assert_eq!(
267267
deserialized_error,
268-
GraphQLError {
268+
Error {
269269
message: "I accidentally your whole query".to_string(),
270270
locations: None,
271271
path: None,
@@ -282,11 +282,11 @@ mod tests {
282282
"path": ["home", "alone", 3, "rating"]
283283
});
284284

285-
let deserialized_error: GraphQLError = serde_json::from_value(err).unwrap();
285+
let deserialized_error: Error = serde_json::from_value(err).unwrap();
286286

287287
assert_eq!(
288288
deserialized_error,
289-
GraphQLError {
289+
Error {
290290
message: "I accidentally your whole query".to_string(),
291291
locations: Some(vec![
292292
Location {
@@ -321,7 +321,7 @@ mod tests {
321321
}
322322
});
323323

324-
let deserialized_error: GraphQLError = serde_json::from_value(err).unwrap();
324+
let deserialized_error: Error = serde_json::from_value(err).unwrap();
325325

326326
let mut expected_extensions = HashMap::new();
327327
expected_extensions.insert("code".to_owned(), json!("CAN_NOT_FETCH_BY_ID"));
@@ -330,7 +330,7 @@ mod tests {
330330

331331
assert_eq!(
332332
deserialized_error,
333-
GraphQLError {
333+
Error {
334334
message: "I accidentally your whole query".to_string(),
335335
locations: Some(vec![
336336
Location {

tests/introspection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const INTROSPECTION_RESPONSE: &'static str =
2323

2424
#[test]
2525
fn leading_underscores_are_preserved() {
26-
let deserialized: graphql_client::GraphQLResponse<introspection_query::ResponseData> =
26+
let deserialized: graphql_client::Response<introspection_query::ResponseData> =
2727
serde_json::from_str(INTROSPECTION_RESPONSE).unwrap();
2828
assert!(deserialized.data.is_some());
2929
assert!(deserialized.data.unwrap().schema.is_some());

0 commit comments

Comments
 (0)