-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Duplicated nested fragments on code generation #4684
Comments
@ardatan can you please take a look? :) |
It took a look, and it seems like it might be a bit tricky to solve, since we define fragments in each operation (and fragments). fragment A on User {
...Nested
}
fragment B on User {
...Nested
}
query user {
user {
...A
...B
...Nested
}
} Because even it we'll be able to avoid adding I think we might not be able to solve that without a runtime method to filter the result |
One option would be to spin a TypeScript process that takes the file as input and produces the already filtered fragment output. Would it be possible? |
Just for the reference: The |
We ended up with a 30MB GraphQL type definition file because of this. 🤣 |
@gilgardosh created a PR for fixing that during generation time: #6018 , by deduplicating the fragments and include them only at the level of the operation. |
This issue is now fixed. To get this fix, make sure to update to the latest plugins (this change affects many plugins, see the list here: #6150 ), and make sure to add to your config: The dedupe happens in generation time, by adding the fragments only once, to the operation, without using them on fragments. Thanks @gilgardosh for fixing it :) |
Any particular reason this is not the default behaviour? After moving to And even if some runtime stripped/ignore them, deduping seems to have no real drawbacks and just reduces data sent over the wire and/or processed? |
Just to add some clarity, this Before: // Inside graphql.config.js
plugins: [
'typescript',
'typescript-operations',
'typed-document-node'
] After: // Inside graphql.config.js
plugins: [
'typescript',
'typescript-operations',
{
'typed-document-node': {
dedupeFragments: true,
},
},
] |
As far as I can tell, enabling |
Describe the bug
When the
TypedDocumentNode
is generated by the tool, it splats (...
) all the fragment definitions, and then the nested fragments follow the same pattern, recursively, resulting in duplicated fragments being sent over the wire.A lot of GraphQL APIs have limits regarding how many fragments are allowed per query, so this is also a problem.
To Reproduce
Here's a reproducible example of what's happening. Clone it, install and run
yarn show-generated-content
and it will output the definitions included in the typed node.Expected behavior
Nested fragments are not duplicated on documents.
Environment:
Specified on the reproducible repo.
Additional context
graphql-tag
workarounds it by removing the duplicated definitions. I don't think it's straightforward to achieve the same behavior on this plugin since it relies on static code generation instead of dynamic, but I do believe it's possible to achieve the same result.In the meantime, I'm using a palliative solution that patches this plugin. Here's the diff:
Basically, this wraps the operations with this
dedupeFragments
function:Which does the job of analyzing the operations and ensures that no repeated fragments are sent.
What can be done to address that? Do we have some direction?
The text was updated successfully, but these errors were encountered: