Add mergeAST utility for merging multiple AST documents#4582
Add mergeAST utility for merging multiple AST documents#4582veeceey wants to merge 2 commits intographql:16.x.xfrom
Conversation
|
Someone is attempting to deploy a commit to the The GraphQL Foundation Team on Vercel. A member of the Team first needs to authorize it. |
Implements a `mergeAST` function that merges multiple DocumentNodes by combining selection sets of operations with matching names and types, recursively deduplicating fields with the same response name and arguments, and deduplicating fragment definitions. This addresses the long-standing need (issue graphql#1428) for a way to dynamically merge GraphQL queries, such as when resolvers need to ensure additional fields are present in requests to backend services. Closes graphql#1428
1697428 to
a29673b
Compare
|
Thanks for the pointer @yaacovCR. I went through benjie's feedback on #4359 -- the DOS concern with mergeAST on unvalidated input is a fair point. I'll look into adding depth/complexity guards so it's safer to use on arbitrary documents. If the preference is still to keep this as a separate package first, I'm open to that too. Let me know how you'd like to proceed. |
Adds a maxDepth option (default: 20) to mergeAST that limits recursion depth when merging nested selection sets. This addresses the concern raised about mergeAST being a potential denial-of-service vector when used on unvalidated or adversarial input. The depth is tracked through the recursive mergeSelectionSets path and an error is thrown if the limit is exceeded. The default of 20 is high enough for any realistic query while still protecting against malicious nesting.
|
Went through benjie's feedback on #4359 about the DOS concern -- totally valid point. I've added a depth guard to the recursive merge path:
Default of 20 should be more than enough for any real-world query while still catching adversarial nesting. The option is there if someone has a legitimate deep schema and wants to raise it. Also exported the |
Summary
mergeASTutility function that merges multipleDocumentNodes into a single document by combining their selection setsCloses #1428
Motivation
As discussed in #1428, there are many use cases where dynamically merging GraphQL AST trees is needed, particularly:
The implementation follows the suggestion from @benjie to migrate/adapt the approach from GraphiQL's
merge-astutility, while providing a simpler, focused API that fits naturally alongside the existingconcatASTutility.API
Test plan
concatASTtests still pass