Skip to content

Commit fb6bbe2

Browse files
committed
Document and explicitly test nested union field selections on interfaces
1 parent e4761a5 commit fb6bbe2

File tree

6 files changed

+27
-6
lines changed

6 files changed

+27
-6
lines changed

graphql_client/tests/interfaces.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const RESPONSE_FRAGMENT: &'static str =
9292
#[test]
9393
fn fragment_in_interface() {
9494
use interface_with_fragment_query::*;
95-
let response_data: interface_with_fragment_query::ResponseData =
95+
let response_data: ResponseData =
9696
serde_json::from_str(RESPONSE_FRAGMENT).expect("RESPONSE_FRAGMENT did not deserialize");
9797

9898
assert_eq!(
@@ -103,7 +103,10 @@ fn fragment_in_interface() {
103103
name: "Audrey Lorde".to_string(),
104104
public_status: PublicStatus {
105105
display_name: false,
106-
on: PublicStatusOn::Person,
106+
on: PublicStatusOn::Person(PublicStatusOnPerson {
107+
birthday: Some("1934-02-18".to_string()),
108+
age: Some(84),
109+
}),
107110
},
108111
on: InterfaceWithFragmentQueryEverythingOn::Person(
109112
InterfaceWithFragmentQueryEverythingOnPerson {
@@ -125,7 +128,9 @@ fn fragment_in_interface() {
125128
name: "Mozilla".to_string(),
126129
public_status: PublicStatus {
127130
display_name: false,
128-
on: PublicStatusOn::Organization
131+
on: PublicStatusOn::Organization(PublicStatusOnOrganization {
132+
industry: Industry::CAT_FOOD,
133+
})
129134
},
130135
on: InterfaceWithFragmentQueryEverythingOn::Organization,
131136
},

graphql_client/tests/interfaces/interface_schema.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ interface Named {
1010
type Person implements Named {
1111
name: String!
1212
birthday: String
13+
age: Int
1314
}
1415

1516
enum Industry {
17+
CAT_FOOD
1618
CHOCOLATE
1719
OTHER
1820
}

graphql_client/tests/interfaces/interface_with_fragment_query.graphql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
fragment PublicStatus on Named {
22
__typename
33
displayName
4+
... on Person {
5+
age
6+
birthday
7+
}
8+
... on Organization {
9+
industry
10+
}
411
}
512

613
query InterfaceWithFragmentQuery {

graphql_client/tests/interfaces/interface_with_fragment_response.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"__typename": "Person",
55
"name": "Audrey Lorde",
66
"displayName": false,
7-
"birthday": "1934-02-18"
7+
"birthday": "1934-02-18",
8+
"age": 84
89
},
910
{
1011
"__typename": "Dog",
@@ -15,7 +16,8 @@
1516
{
1617
"__typename": "Organization",
1718
"name": "Mozilla",
18-
"displayName": false
19+
"displayName": false,
20+
"industry": "CAT_FOOD"
1921
},
2022
{
2123
"__typename": "Dog",

graphql_client_codegen/src/interfaces.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use std::cell::Cell;
99
use std::collections::HashSet;
1010
use unions::union_variants;
1111

12-
/// Represents an Interface type extracted from the schema.
12+
/// A GraphQL interface (simplified schema representation).
13+
///
14+
/// In the generated code, fragments nesting is preserved, including for selection on union variants. See the tests in the graphql client crate for examples.
1315
#[derive(Debug, Clone, PartialEq)]
1416
pub struct GqlInterface<'schema> {
1517
/// The documentation for the interface. Extracted from the schema.

graphql_client_codegen/src/unions.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ use selection::Selection;
55
use std::cell::Cell;
66
use std::collections::BTreeSet;
77

8+
/// A GraphQL union (simplified schema representation).
9+
///
10+
/// For code generation purposes, unions will "flatten" fragment spreads, so there is only one enum for the selection. See the tests in the graphql_client crate for examples.
811
#[derive(Debug, Clone, PartialEq)]
912
pub(crate) struct GqlUnion<'schema> {
1013
pub name: &'schema str,

0 commit comments

Comments
 (0)