Skip to content

Commit 855f9c1

Browse files
committed
api: remove GraphQL prefix from type names
The `GraphQLQuery` trait is left the same to ease searching the code and to avoid clashing with other custom derive crates (e.g., a SQL crate). Fixes #101.
1 parent a82359b commit 855f9c1

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
@@ -82,7 +82,7 @@ fn main() -> Result<(), failure::Error> {
8282
))).json(&q)
8383
.send()?;
8484

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

8888
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
@@ -75,7 +75,7 @@ fn introspect_schema(
7575
None => Box::new(::std::io::stdout()),
7676
};
7777

78-
let request_body: graphql_client::GraphQLQueryBody<()> = graphql_client::GraphQLQueryBody {
78+
let request_body: graphql_client::QueryBody<()> = graphql_client::QueryBody {
7979
variables: (),
8080
query: introspection_query::QUERY,
8181
};

graphql_client_codegen/src/lib.rs

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

152-
fn build_query(variables: Self::Variables) -> ::graphql_client::GraphQLQueryBody<Self::Variables> {
153-
::graphql_client::GraphQLQueryBody {
152+
fn build_query(variables: Self::Variables) -> ::graphql_client::QueryBody<Self::Variables> {
153+
::graphql_client::QueryBody {
154154
variables,
155155
query: #module_name::QUERY,
156156
}

src/lib.rs

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

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

7777
/// The form in which queries are sent over HTTP in most implementations. This will be built using the [`GraphQLQuery`] trait normally.
7878
#[derive(Debug, Serialize, Deserialize)]
79-
pub struct GraphQLQueryBody<Variables>
79+
pub struct QueryBody<Variables>
8080
where
8181
Variables: serde::Serialize,
8282
{
@@ -86,7 +86,7 @@ where
8686
pub query: &'static str,
8787
}
8888

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

98-
/// Part of a path in a query. It can be an object key or an array index. See [`GraphQLError`].
98+
/// Part of a path in a query. It can be an object key or an array index. See [`Error`].
9999
#[derive(Debug, Serialize, Deserialize, PartialEq)]
100100
#[serde(untagged)]
101101
pub enum PathFragment {
@@ -128,7 +128,7 @@ pub enum PathFragment {
128128
/// # fn main() -> Result<(), failure::Error> {
129129
/// use graphql_client::*;
130130
///
131-
/// let body: GraphQLResponse<ResponseData> = serde_json::from_value(json!({
131+
/// let body: Response<ResponseData> = serde_json::from_value(json!({
132132
/// "data": null,
133133
/// "errors": [
134134
/// {
@@ -142,10 +142,10 @@ pub enum PathFragment {
142142
/// ],
143143
/// }))?;
144144
///
145-
/// let expected: GraphQLResponse<ResponseData> = GraphQLResponse {
145+
/// let expected: Response<ResponseData> = Response {
146146
/// data: None,
147147
/// errors: Some(vec![
148-
/// GraphQLError {
148+
/// Error {
149149
/// message: "The server crashed. Sorry.".to_owned(),
150150
/// locations: Some(vec![
151151
/// Location {
@@ -156,7 +156,7 @@ pub enum PathFragment {
156156
/// path: None,
157157
/// extensions: None,
158158
/// },
159-
/// GraphQLError {
159+
/// Error {
160160
/// message: "Seismic activity detected".to_owned(),
161161
/// locations: None,
162162
/// path: Some(vec![
@@ -174,7 +174,7 @@ pub enum PathFragment {
174174
/// # }
175175
/// ```
176176
#[derive(Debug, Serialize, Deserialize, PartialEq)]
177-
pub struct GraphQLError {
177+
pub struct Error {
178178
/// The human-readable error message. This is the only required field.
179179
pub message: String,
180180
/// Which locations in the query the error applies to.
@@ -216,17 +216,17 @@ pub struct GraphQLError {
216216
/// # }
217217
/// #
218218
/// # fn main() -> Result<(), failure::Error> {
219-
/// use graphql_client::GraphQLResponse;
219+
/// use graphql_client::Response;
220220
///
221-
/// let body: GraphQLResponse<ResponseData> = serde_json::from_value(json!({
221+
/// let body: Response<ResponseData> = serde_json::from_value(json!({
222222
/// "data": {
223223
/// "users": [{"id": 13}],
224224
/// "dogs": [{"name": "Strelka"}],
225225
/// },
226226
/// "errors": [],
227227
/// }))?;
228228
///
229-
/// let expected: GraphQLResponse<ResponseData> = GraphQLResponse {
229+
/// let expected: Response<ResponseData> = Response {
230230
/// data: Some(ResponseData {
231231
/// users: vec![User { id: 13 }],
232232
/// dogs: vec![Dog { name: "Strelka".to_owned() }],
@@ -240,11 +240,11 @@ pub struct GraphQLError {
240240
/// # }
241241
/// ```
242242
#[derive(Debug, Serialize, Deserialize, PartialEq)]
243-
pub struct GraphQLResponse<Data> {
243+
pub struct Response<Data> {
244244
/// The absent, partial or complete response data.
245245
pub data: Option<Data>,
246246
/// The top-level errors returned by the server.
247-
pub errors: Option<Vec<GraphQLError>>,
247+
pub errors: Option<Vec<Error>>,
248248
}
249249

250250
#[cfg(test)]
@@ -257,11 +257,11 @@ mod tests {
257257
"message": "I accidentally your whole query"
258258
});
259259

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

262262
assert_eq!(
263263
deserialized_error,
264-
GraphQLError {
264+
Error {
265265
message: "I accidentally your whole query".to_string(),
266266
locations: None,
267267
path: None,
@@ -278,11 +278,11 @@ mod tests {
278278
"path": ["home", "alone", 3, "rating"]
279279
});
280280

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

283283
assert_eq!(
284284
deserialized_error,
285-
GraphQLError {
285+
Error {
286286
message: "I accidentally your whole query".to_string(),
287287
locations: Some(vec![
288288
Location {
@@ -317,7 +317,7 @@ mod tests {
317317
}
318318
});
319319

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

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

327327
assert_eq!(
328328
deserialized_error,
329-
GraphQLError {
329+
Error {
330330
message: "I accidentally your whole query".to_string(),
331331
locations: Some(vec![
332332
Location {

tests/introspection.rs

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

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

0 commit comments

Comments
 (0)