Skip to content

Commit

Permalink
fix: fix broken emission of __typename field type (#47)
Browse files Browse the repository at this point in the history
* test: add snapshot with current state

* fix: correct emission of __typename field's type
  • Loading branch information
uhyo authored Feb 14, 2024
1 parent aa10bf1 commit 545d24f
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ fn generate_selection_tree_type_impl<'src, S: Text<'src>>(
branch
.unaliased_fields
.iter()
.map(|field| field_to_type(context, field))
.map(|field| field_to_type(context, field, &branch.type_name))
.collect(),
);
let aliased_object = TSType::Object(
branch
.aliased_fields
.iter()
.map(|field| field_to_type(context, field))
.map(|field| field_to_type(context, field, &branch.type_name))
.collect(),
);
TSType::TypeFunc(
Expand All @@ -78,6 +78,7 @@ fn generate_selection_tree_type_impl<'src, S: Text<'src>>(
fn field_to_type<'src, S: Text<'src>>(
context: &GenerateSelectionTreeTypeContext,
field: &SelectionTreeField<S>,
parent_type_name: &str,
) -> ObjectField {
match field {
SelectionTreeField::Empty(empty) => ObjectField {
Expand All @@ -90,7 +91,7 @@ fn field_to_type<'src, S: Text<'src>>(
SelectionTreeField::Leaf(leaf) => {
let field_type = if leaf.name == "__typename" {
// special case for __typename
TSType::StringLiteral(leaf.name.to_string())
TSType::StringLiteral(parent_type_name.to_string())
} else {
map_to_tstype(&leaf.r#type, |ty| {
TSType::NamespaceMember3(
Expand Down
39 changes: 39 additions & 0 deletions crates/printer/src/operation_type_printer/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,45 @@ fn fragment_inline_spread() {
assert_snapshot!(printed);
}

#[test]
fn typename_field() {
let doc = parse_operation_document(
"
query {
me {
__typename
id name type age
}
}
",
)
.unwrap();
let printed = print_document_default(&doc);
assert_snapshot!(printed);
}

#[test]
fn typename_field_on_union() {
let doc = parse_operation_document(
"
query {
me {
posts {
__typename
id
... on Post {
title
}
}
}
}
",
)
.unwrap();
let printed = print_document_default(&doc);
assert_snapshot!(printed);
}

#[test]
fn query_variables() {
let doc = parse_operation_document(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
source: crates/printer/src/operation_type_printer/tests/mod.rs
expression: printed
---
import type { TypedDocumentNode } from "@graphql-typed-document-node/core";
import type * as Schema from "";

type Result = Schema.__SelectionSet<Schema.__OperationOutput.Query, {
me: Schema.__SelectionSet<Schema.__OperationOutput.User, {
__typename: "User";
id: Schema.__OperationOutput.ID;
name: Schema.__OperationOutput.String;
type: Schema.__OperationOutput.UserType;
age: Schema.__OperationOutput.Int | null;
}, {}>;
}, {}>;

type Variables = {};

declare const Query: TypedDocumentNode<Result, Variables>;

export { Query as default };


Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: crates/printer/src/operation_type_printer/tests/mod.rs
expression: printed
---
import type { TypedDocumentNode } from "@graphql-typed-document-node/core";
import type * as Schema from "";

type Result = Schema.__SelectionSet<Schema.__OperationOutput.Query, {
me: Schema.__SelectionSet<Schema.__OperationOutput.User, {
posts: (Schema.__SelectionSet<Schema.__OperationOutput.User, {
__typename: "User";
id: Schema.__OperationOutput.ID;
}, {}> | Schema.__SelectionSet<Schema.__OperationOutput.Bot, {
__typename: "Bot";
id: Schema.__OperationOutput.ID;
}, {}> | Schema.__SelectionSet<Schema.__OperationOutput.Post, {
__typename: "Post";
id: Schema.__OperationOutput.ID;
title: Schema.__OperationOutput.String;
}, {}> | Schema.__SelectionSet<Schema.__OperationOutput.Tweet, {
__typename: "Tweet";
id: Schema.__OperationOutput.ID;
}, {}>)[];
}, {}>;
}, {}>;

type Variables = {};

declare const Query: TypedDocumentNode<Result, Variables>;

export { Query as default };


0 comments on commit 545d24f

Please sign in to comment.