New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[typescript-operations] Arrays nested inside named fragments are not merged together #4212
Comments
Thanks @helios1138 , I'm trying to fix it now :) Btw, you should be able to workaround it by using |
@dotansimha thank you very much! Regarding
However, I have yet find the time to create a simple reproducible example for this error :) |
@helios1138 yeah you are right, it's @kamilkisiela tried to help me with a way to merge the fragment objects. It seems like |
I'm not sure about viability here, but would you accept an PR with an additional option for That would be a fix this issue, be a nice runtime improvement, and prevent the need to include fragment documents in queries. |
Would love to see a fix for this issue, as it potentially blocks us from adopting GraphQL Code Generator in one of our projects. (I will see if I can find time to look into it myself, but at least wanted to make a note of this.) |
adjust ts validation function remove old dependency for open
@kohlmannj @helios1138 Can you please give it a try to TypeScript 4? I checked your given example with something like that: fragment Object1 on Object {
items {
id
}
}
fragment Object2 on Object {
items {
name
}
}
I got 2 fragments generated: export type Object1Fragment = (
{ __typename?: 'Object' }
& { items: Array<(
{ __typename?: 'Item' }
& Pick<Item, 'id'>
)> }
);
export type Object2Fragment = (
{ __typename?: 'Object' }
& { items: Array<(
{ __typename?: 'Item' }
& Pick<Item, 'name'>
)> }
); Which eventually getting merged like that: export type CombinedObjectFragment = (
{ __typename?: 'Object' }
& Object1Fragment
& Object2Fragment
); I wrote a small piece of code that uses the query result and tries to access both fields: function test (t: TestQuery) {
for (const item of t.obj!.items) {
console.log(item.id, item.name, item.__typename);
}
} And it seems fine, no errors at all with TS v4, also the auto-complete seems to work nicely:
I created a test in the repo (which uses the latest TS 4) and the test passes compilation with no errors: #5665 Can you please confirm the above and let me know if it works correctly? |
The |
adjust ts validation function remove old dependency for open
Closed due to the lack of response. Feel free to create a new issue if the problem persists. |
Arrays nested inside named fragments are not merged together
for example, given the schema
and operations
in the generated code
you get an error
Which is incorrect since both
Object1
andObject2
fragments are on the same typeObject
and are merged together in the graphql, so the fielditem.name
is present at runtimeI'm guessing the problem stems from the fact that combined fragment types are defined dynamically as
and there are some typescripts-specific issues at play here.
I think it would be good to have an option to flatten the combined fragment types so that we still get
Object1Fragment
andObject2Fragment
generated separately, butCombinedObjectFragment
does not reference them dynamically, instead, it would be listing the fields inside explicitly.Something similar to how
preResolveTypes
eliminates dynamicPick
types, but for fragments as well.The text was updated successfully, but these errors were encountered: