Open cicciodm opened 3 years ago
NICE! Exactly what I was looking for. It seems to work as expected. Any Idea why this is happening?
The name of the enum should be IBaseIfaceKind
We'd love to accept a PR if someone wants to work on this.
@ardatan Pr can be found here: https://github.com/dotansimha/graphql-code-generator/pull/6862
NICE! Exactly what I was looking for. It seems to work as expected. Any Idea why this is happening?
The name of the enum should be IBaseIfaceKind
The spaces in the interface name resulted from the change-case-all#capitalCase setting. @ardatan Do you know if this counts as bug or is it working as intended?
Is your feature request related to a problem? Please describe.
The c-sharp plugin currently does not provide an implementation of GQL Union types and Interfaces. I will refer to both of these as Composition Types.
Consider the following schema definition:
With the current implementation, these types are simply ignored. This makes the plugin not really usable for real-world scenarios, such as generating type for a complex API currently serving many real users.
Describe the solution you'd like On a personal fork of graphql-code-generator I have implemented a way to model Composition Types in C#.
The language has no concept of Union Types, so I took inspiration from Typescript to model it. Using the above example, this is how it works.
The Union Type is modelled with an
interface
named as the type itself, and anEnum
called{name}Kind
with values representing each concrete implementation of the Union Type.Each of the concrete type will implement the generic interface, including an autogenerated
getter
, which will return the correct value ofKind
.When faced with a
Veichle
, devs can then check on theVeichleKind
on their object, before safely casting the object to the appropriate type.Another block necessary to make this implementation work is the addition of a custom implementation for a Newtonsoft JSONConverter for correctly Deserializing fields that are of any Composition type (
UnionType
orInterface
), as simply callingserializer.Deserialize<GeneratedGQLQueryType>()
would otherwise fail withCannot instantiate item of type Veichle
The Custom Converters (one for fields of
CompositionType
type, and one for list ofComposition Type
objects) work by inspecting the__typename
of the object currently being deserialised. Based on the__typename
, the appropriate Generated Type will be found using introspection, and an appropriateToObject
method will be created, with the correct type parameter.All fields that require the Custom Converter to be parsed correctly, are automatically marked with the
[JsonConverter(typeof(UnionType[List]Converter))]
attribute.Describe alternatives you've considered No alternative considered as of now. I have tried to push the CustomConverter tag to the types to be parsed, as opposed to the fields of those types, but that approach was not successful.
Additional context A version of this implementation is currently being used in production, and has several hundred thousand hits per day, with near perfect success rate.
Link to the fork: https://github.com/cicciodm/graphql-code-generator/tree/c%23-union-types/packages/plugins/c-sharp/c-sharp/src