Skip to content
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
26 changes: 26 additions & 0 deletions graphql_client/tests/typename_only_selection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use graphql_client::*;

const RESPONSE: &str =
include_str!("typename_only_selection/typename_only_on_union_member_response.json");

#[derive(GraphQLQuery)]
#[graphql(
query_path = "tests/typename_only_selection/query.graphql",
schema_path = "tests/typename_only_selection/schema.graphql",
response_derives = "Debug, PartialEq, Eq"
)]
pub struct TypenameOnlyOnUnionMember;

#[test]
fn typename_only_on_union_member_deserialization() {
let response_data: typename_only_on_union_member::ResponseData =
serde_json::from_str(RESPONSE).unwrap();

let expected = typename_only_on_union_member::ResponseData {
do_thing: typename_only_on_union_member::TypenameOnlyOnUnionMemberDoThing::DoThingSuccess(
typename_only_on_union_member::TypenameOnlyOnUnionMemberDoThingOnDoThingSuccess {},
),
};

assert_eq!(response_data, expected);
}
8 changes: 8 additions & 0 deletions graphql_client/tests/typename_only_selection/query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mutation TypenameOnlyOnUnionMember {
doThing {
__typename
... on DoThingSuccess {
__typename
}
}
}
22 changes: 22 additions & 0 deletions graphql_client/tests/typename_only_selection/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
schema {
query: Query
mutation: Mutation
}

type Query {
things: [String!]!
}

type Mutation {
doThing: DoThingResult!
}

union DoThingResult = DoThingSuccess | DoThingFailure

type DoThingSuccess {
thingId: ID!
}

type DoThingFailure {
reason: String!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"doThing": {
"__typename": "DoThingSuccess"
}
}
5 changes: 3 additions & 2 deletions graphql_client_codegen/src/codegen/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,9 @@ impl<'a> ExpandedSelection<'a> {
.collect();

// If we only have an `on` field, turn the struct into the enum
// of the variants.
if fields.peek().is_none() {
// of the variants. With no variants either, this would produce an
// enum with no variants, which can never deserialize.
if fields.peek().is_none() && !on_variants.is_empty() {
let item = quote! {
#response_derives
#[serde(tag = "__typename")]
Expand Down
Loading