edgedb-js icon indicating copy to clipboard operation
edgedb-js copied to clipboard

Better return types for `.__type__.name` and polymorphic queries if possible

Open jaclarke opened this issue 2 years ago • 5 comments

Describe the proposed feature A clear and concise description of the syntax, what you want to happen, and why it's needed.

Part 1. __type__.name

Currently the return type of .__type__.name is just string. We know the name of the object type that's being selected, so it should be possible to infer this as the correct string literal (or union of string literals if it's a parent type).

Part 2. Polymorphic queries

Currently a query like this:

e.select(e.Content, () => ({
  title: true,
  ...e.is(e.Movie, { release_year: true }),
  ...e.is(e.TVShow, { num_seasons: true }),
}));

gets a return type like:

{
  title: string;
  release_year: number | null;
  num_seasons: number | null;
}[]

While this is technically correct, it would be more useful if we could return a discriminated union. I think it might be possible for the querybuilder to automatically insert the typename in the shape, and return a type like so:

({
  title: string;
} & ({
  __typename: 'default::Movie';
  release_year: number;
} | {
  __typename: 'default::Show';
  num_seasons: number;
}))[]

jaclarke avatar Sep 28 '22 19:09 jaclarke

This would be fantastic! Being able to return discriminated unions here would be very beneficial for applications using something like tRPC, which lets you then have discriminated unions on your frontend.

TechSupportJosh avatar Sep 28 '22 20:09 TechSupportJosh

Invaluable! Eliminates redundant checking just to satisfy the parser!

datner avatar Oct 03 '22 22:10 datner

This would be incredibly useful! Right now the returned types for a polymorphic link are quite difficult to work with. Any chance this could get attention again?

pk-nb avatar Jun 04 '23 22:06 pk-nb

@pk-nb

No timeline on this yet, but it's on my radar to try to bring the linked PR current against master. There are some potential interactions between this and some other issues with polymorphic types that I'll want to be careful with, so it might be a bit more involved from a QA perspective.

scotttrinh avatar Jun 07 '23 18:06 scotttrinh

Ping

janwirth avatar Jul 30 '24 14:07 janwirth