Hotchocolate: Improved union types for PCF

Created on 19 Feb 2020  路  12Comments  路  Source: ChilliCream/hotchocolate

Is your feature request related to a problem? Please describe.

Currently there are 2 ways to make union types:
1. type descriptors
2. marker interfaces

The first is not compatible with pure-code-first schema because you can't use the GraphQL type as return value.

The second option works for pure-code-first, but has the problem that you don't define the union as a set of types but you define the types as part of different unions. Therefore, for example, if you have an AppError type that is part of 20+ unions you'll have AppError implementing 20+ interfaces. Even though the number of interfaces a type is practically limitless, this is suboptimal because in order to figure out what's the set of types for a union you have to scan all other types (both while reading and probably at the HC implementation level).

Describe the solution you'd like

I propose a custom library type (similarly to Optional) named Union that has overloads up to 16 types (12 would also be acceptable I guess but I wouldn't go lower than 12).

So, for example you might have Union<A>, Union<A, B>, Union<A, B, C>, ...

This would just be a "marker" abstract class that defines implicit and explicit conversions from the types defined as type arguments.

The implementation would be similar to https://github.com/mcintyre321/OneOf and https://github.com/DiscU/DiscU but could be even simpler (just define explicit and implicit conversions and nothing else).

You'd then define a union type via something like:

public class CreateUserResult : Union<User, ValidationError, AuthorizationError> {}

and then, thanks to the implicit/explicit conversions, you would be able to use it as a return type and return User, ValidationError or AuthorizationError.

馃尪 hot chocolate 馃攳 investigate

Most helpful comment

I think your proposal would be good to discuss in our community standup. I will put it on the agenda.

All 12 comments

I think your proposal would be good to discuss in our community standup. I will put it on the agenda.

@michaelstaib Thanks

@michaelstaib Hi Michael, was this discussed by any chance? I've seen it's on the 11.x.x milestone. Does that mean that you decided in favour?

No, the milestone means we are looking into it after 11.0.0. We are at the moment laser focused on the execution engine and strawberry shake

Also, Microsoft is working on unions for c# so we might even wait a little to see if they are coming in c# 9 or 10

@michaelstaib TBH I wouldn't hold my breath for discriminated unions entering C# any time soon. Other features such as "records" have been in the pipeline for much longer and were promised since C# 6 but at C# 8 they are nowhere to be found.

If discriminated unions were ever to make it into C# we could support them as well.
But even then GraphQL unions are not discriminated unions. They are more like a union of possible types, but they are not "tagged" in the same way a discriminated union (like in F#) does.

I think unions are one of the fundamental types in GraphQL that should be easy to use. For example representing errors in the scema require unions to represent them correctly.

If I were to work on a PR for it would there be a chance for it entering v11?

If discriminated unions were ever to make it into C# we could support them as well.

I do have other information on that.

But we can still explore this type. Do you want to do a PR?

I do have other information on that.

You do?

But we can still explore this type. Do you want to do a PR?

Sure, I can give it a try.

Lets discuss details on slack ... we also can do a zoom call if you are stuck.

By the way, with reference to your previous comments: https://github.com/dotnet/roslyn/blob/master/docs/Language%20Feature%20Status.md

Maybe records will make it into C# 9 this time (although I'm still very skeptical), but discriminated unions are nowhere to be seen.

discriminated are coming ... they are on the roadmap to version 10. I was under NDA previous to build so I did not want to speak more about it. If you watch mats talk again you will see him mentioning them although just with one sentence. They are planned for c# 6 which puts them into fall next year.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sergeyshaykhullin picture sergeyshaykhullin  路  3Comments

RohrerF picture RohrerF  路  3Comments

PascalSenn picture PascalSenn  路  5Comments

nigel-sampson picture nigel-sampson  路  5Comments

jbray1982 picture jbray1982  路  5Comments