Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.

Union fields incorrectly generating type declarations #201

Open
maraisr opened this issue Aug 17, 2020 · 1 comment
Open

Union fields incorrectly generating type declarations #201

maraisr opened this issue Aug 17, 2020 · 1 comment

Comments

@maraisr
Copy link
Member

maraisr commented Aug 17, 2020

for the definition:

type PostNotFound implements Node {
	id: ID!;
}

type PostMoved implements Node {
	id: ID!;
	target: Post!;
}

type Post implements Node {
	id: ID!;
	body: String!;
	author: String!;
}

union BlogResult = Post | PostNotFound | PostMoved;

type Blog {
   node: BlogResult;
}

and the query:

query BlogPostQuery {
	blog {
		... on PostNotFound {
			__typename
		}
		... on Post {
			body
			author
		}
	}
}

Error:

The types generated doesn't allow me to discriminate unions:

export type BlogPostQuery = {
	readonly blog: {
		readonly __typename: "PostNotFound";
		readonly body?: string;
		readonly author?: string;
	}
}

To me that is wrong, because the typename PostNotFound doesnt have those fields on it.

Expected:

export type BlogPostQuery = {
	readonly blog: {
		readonly __typename: "PostNotFound";
	} | {
		readonly __typename: "Post";
		readonly body: string;
		readonly author: string;
	}
}

or would I need to ask for __typename on every union member, or on field for that matter.


Logging so I don't forget, will raise a fix PR soon

@damikun
Copy link

damikun commented Sep 5, 2020

@maraisr i think you can solve it using separate fragments for each union inside your querry. It will spilit it to fragmentRefs..

query BlogPostQuery {
	blog {
        __typename  // Use this to differ

         ... PostNotFound_fragment 
	 ... Post_fragment

	}
}

fragment PostNotFound_fragment on PostNotFound {
 ...
}

fragment Post_fragment on Post {
 ...
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants