feat: fragmentifyDocument utility
This PR introduces a utility function fragmentifyDocument that significantly optimize GraphQL ASTs by programmatically converting InlineFragments to FragmentDefinitions and replace these inlineFragments with FragmentSpreads. This transformation aims to enhance the efficiency of operation parsing, validation and execution, especially for large and complex queries.
GraphQL operations can become quite large and complex, leading to increased parsing time and execution latency. This is particularly evident with extensive use of InlineFragments, which, while flexible and powerful, can add significant overhead to query processing. By extracting these inline fragments into separate FragmentDefinitions and referencing them with FragmentSpreads, we can significantly reduce the complexity and size of the initial query sent to the server.
The optimization would consist of:
- Traverse the AST.
- Search for InlineFragment nodes in SelectionSet nodes.
- Create a FragmentDefinition of the SelectionSet of that InlineFragments.
- Append that FragmentDefinition to the document AST.
- replace the InlineFragment in the SelectionSet with a FragmentSpread.
This optimization is scoped for operations that heavily rely in polymorphism. It doesn't need a graphQL schema at all, only needs the AST.
I have a test suite pending to add, (already have the tests), but we (ExpediaGroup) tried this in production giving us a 100x smaller operations.
This PR was originally raised against Apollo Federation, however, this utility function applies to ASTs and to any GraphQL implementation specification, so graphql-js sound like the best place to have this.
- :x: The email address for the commit (a583cd381ca83b2f27f21ed0753345c2262f200b, 3b0c035c69c32e84cb7130f6c1e5b687690f9a23) is not linked to the GitHub account, preventing the EasyCLA check. Consult this Help Article and GitHub Help to resolve. (To view the commit's email address, add .patch at the end of this PR page's URL.) For further assistance with EasyCLA, please submit a support request ticket.
Deploy Preview for compassionate-pike-271cb3 ready!
| Name | Link |
|---|---|
| Latest commit | 3b0c035c69c32e84cb7130f6c1e5b687690f9a23 |
| Latest deploy log | https://app.netlify.com/sites/compassionate-pike-271cb3/deploys/65e93dd0f8882b0008799657 |
| Deploy Preview | https://deploy-preview-4025--compassionate-pike-271cb3.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
@samuelAndalon this looks extremely useful! I think it might fit very well into graphql-tools as that is more of a general toolbox, but it could make sense within the reference implementation if the WG ever formalizes something in the spec with regard to canonical documents of some kind.
What do you think?