Skip to content

Implement type-refining fragments (fixes #154) #181

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
Nov 3, 2018
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
34 changes: 34 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,40 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Added

- The CLI can now optionally format the generated code with rustfmt (enable the `rustfmt` feature).
- When deriving, the generated module now has the same visibility (private, `pub`, `pub(crate)` or `crate`) as the struct under derive.
- Codegen now supports type-refining fragments, i.e. fragments on interfaces or unions that only apply to one of the variants. Example:

```graphql
type Pie {
diameter: Integer
name: String
}

type Sandwich {
length: Float
ingredients: [String]
}

union Food = Sandwich | Pie

type Query {
lunch: Food
}

fragment PieName on Pie {
name
}

query Test {
lunch {
...PieName
...on Sandwich {
length
}
}
}

```

### Changed

Expand Down
38 changes: 19 additions & 19 deletions graphql_client/tests/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const RESPONSE: &'static str = include_str!("interfaces/interface_response.json"
#[graphql(
query_path = "tests/interfaces/interface_query.graphql",
schema_path = "tests/interfaces/interface_schema.graphql",
response_derives = "Debug, PartialEq",
response_derives = "Debug, PartialEq"
)]
pub struct InterfaceQuery;

Expand Down Expand Up @@ -50,15 +50,13 @@ fn interface_deserialization() {
};

assert_eq!(response_data, expected);

assert_eq!(response_data.everything.map(|names| names.len()), Some(4));
}

#[derive(GraphQLQuery)]
#[graphql(
query_path = "tests/interfaces/interface_not_on_everything_query.graphql",
schema_path = "tests/interfaces/interface_schema.graphql",
response_derives = "Debug",
response_derives = "Debug"
)]
pub struct InterfaceNotOnEverythingQuery;

Expand All @@ -84,7 +82,7 @@ fn interface_not_on_everything_deserialization() {
#[graphql(
query_path = "tests/interfaces/interface_with_fragment_query.graphql",
schema_path = "tests/interfaces/interface_schema.graphql",
response_derives = "Debug,PartialEq",
response_derives = "Debug,PartialEq"
)]
pub struct InterfaceWithFragmentQuery;

Expand All @@ -101,35 +99,37 @@ fn fragment_in_interface() {
response_data,
ResponseData {
everything: Some(vec![
RustMyQueryEverything {
RustInterfaceWithFragmentQueryEverything {
name: "Audrey Lorde".to_string(),
public_status: PublicStatus {
display_name: false,
},
on: RustMyQueryEverythingOn::Person(RustMyQueryEverythingOnPerson {
birthday: Some("1934-02-18".to_string()),
})
on: RustInterfaceWithFragmentQueryEverythingOn::Person(
RustInterfaceWithFragmentQueryEverythingOnPerson {
birthday: Some("1934-02-18".to_string()),
}
)
},
RustMyQueryEverything {
RustInterfaceWithFragmentQueryEverything {
name: "Laïka".to_string(),
public_status: PublicStatus { display_name: true },
on: RustMyQueryEverythingOn::Dog(RustMyQueryEverythingOnDog {
is_good_dog: true,
})
on: RustInterfaceWithFragmentQueryEverythingOn::Dog(
RustInterfaceWithFragmentQueryEverythingOnDog { is_good_dog: true }
)
},
RustMyQueryEverything {
RustInterfaceWithFragmentQueryEverything {
name: "Mozilla".to_string(),
public_status: PublicStatus {
display_name: false
},
on: RustMyQueryEverythingOn::Organization,
on: RustInterfaceWithFragmentQueryEverythingOn::Organization,
},
RustMyQueryEverything {
RustInterfaceWithFragmentQueryEverything {
name: "Norbert".to_string(),
public_status: PublicStatus { display_name: true },
on: RustMyQueryEverythingOn::Dog(RustMyQueryEverythingOnDog {
is_good_dog: true
}),
on: RustInterfaceWithFragmentQueryEverythingOn::Dog(
RustInterfaceWithFragmentQueryEverythingOnDog { is_good_dog: true }
),
},
])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ fragment PublicStatus on Named {
displayName
}

query MyQuery {
query InterfaceWithFragmentQuery {
everything {
name
__typename
name
...PublicStatus
... on Dog {
isGoodDog
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fragment Birthday on Person {
birthday
}

query MyQuery {
everything {
__typename
name
... on Dog {
isGoodDog
}
...Birthday
... on Organization {
industry
}
}
}
87 changes: 87 additions & 0 deletions graphql_client/tests/type_refining_fragments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#[macro_use]
extern crate graphql_client;
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;

#[derive(GraphQLQuery)]
#[graphql(
query_path = "tests/interfaces/interface_with_type_refining_fragment_query.graphql",
schema_path = "tests/interfaces/interface_schema.graphql",
response_derives = "Debug, PartialEq"
)]
pub struct QueryOnInterface;

#[derive(GraphQLQuery)]
#[graphql(
query_path = "tests/unions/type_refining_fragment_on_union_query.graphql",
schema_path = "tests/unions/union_schema.graphql",
response_derives = "PartialEq, Debug"
)]
pub struct QueryOnUnion;

#[test]
fn type_refining_fragment_on_union() {
const RESPONSE: &'static str = include_str!("unions/union_query_response.json");

let response_data: query_on_union::ResponseData = serde_json::from_str(RESPONSE).unwrap();

let expected = query_on_union::ResponseData {
names: Some(vec![
query_on_union::RustMyQueryNames::Person(query_on_union::RustMyQueryNamesOnPerson {
first_name: "Audrey".to_string(),
last_name: Some("Lorde".to_string()),
}),
query_on_union::RustMyQueryNames::Dog(query_on_union::RustMyQueryNamesOnDog {
name: "Laïka".to_string(),
}),
query_on_union::RustMyQueryNames::Organization(
query_on_union::RustMyQueryNamesOnOrganization {
title: "Mozilla".to_string(),
},
),
query_on_union::RustMyQueryNames::Dog(query_on_union::RustMyQueryNamesOnDog {
name: "Norbert".to_string(),
}),
]),
};

assert_eq!(response_data, expected);
}

#[test]
fn type_refining_fragment_on_interface() {
use query_on_interface::*;

const RESPONSE: &'static str = include_str!("interfaces/interface_response.json");

let response_data: query_on_interface::ResponseData = serde_json::from_str(RESPONSE).unwrap();

let expected = ResponseData {
everything: Some(vec![
RustMyQueryEverything {
name: "Audrey Lorde".to_string(),
on: RustMyQueryEverythingOn::Person(RustMyQueryEverythingOnPerson {
birthday: Some("1934-02-18".to_string()),
}),
},
RustMyQueryEverything {
name: "Laïka".to_string(),
on: RustMyQueryEverythingOn::Dog(RustMyQueryEverythingOnDog { is_good_dog: true }),
},
RustMyQueryEverything {
name: "Mozilla".to_string(),
on: RustMyQueryEverythingOn::Organization(RustMyQueryEverythingOnOrganization {
industry: Industry::OTHER,
}),
},
RustMyQueryEverything {
name: "Norbert".to_string(),
on: RustMyQueryEverythingOn::Dog(RustMyQueryEverythingOnDog { is_good_dog: true }),
},
]),
};

assert_eq!(response_data, expected);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fragment DogName on Dog {
name
}

query MyQuery {
names {
__typename
...DogName
... on Person {
firstName
lastName
}
... on Organization {
title
}
}
}
Loading