Open mat-sop opened 8 months ago
I started to look into this but this was a bit tricker than I expected, mostly because the information about fragments isn't shared between generators. When the PackageGenerator
is created the fragments are both passed to the FragmentGenerator
and the PackageGenerator
itself.
With the fix in #224 these fragments got passed further to the field parsing but the field and schema state is in the local context for the result fields so I don't really se an obvious way to leverage this when generating the fragment string. And further mutating the fragment definitions dictionary might have unintentional side effects and becomes very order dependent.
I think one approach to not spread this into separate generators would be to somehow pre-process this kind of data modifying the fragment definitions already when creating the PackageGenerator
. Right now we don't have any context around different fields but as a test I tried to just recursively ensure __typename
is selected on every selections that's not spreads. This would ensure the fragment string generated in each client method select __typename
as well and that every result class will have the __typename
field.
Have you had the time to look into what would be required to resolve this issue? Is there a downside to always selecting __typename
or would that be a viable solution forward? Any pointers on how I could add __typename
only to the abstract fields (without adding it twice)?
Also I know that sometimes having to answer people on questions like this just takes more time and energy so feel free to not elaborate on any reply here. I mostly just wanted to try to contribute here since this is blocking us but I don't want to introduce a big refactor if you had something else in mind.
Reported in #253
When the result field's type is an abstract type - union or interface, we add
__typename
to the query string and corresponding generated model. When this happens as part of the fragment we only add to the model, not to the string - because of this the generated client can't properly create an instance of the model.Example schema to reproduce the issue:
Example query: